-
Notifications
You must be signed in to change notification settings - Fork 548
Add GitHub Actions health check workflow #23036
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
a902687
Enable docker compose logs when inputs.log is true
KevinMind 8b491c0
Decouple data seeding from data migration in initialize.py:
KevinMind 5d1b77f
Add GitHub Actions health check workflow
KevinMind e67ac81
Revert __healthcheck__ endpoint to separate endpoints
KevinMind 24cf5bc
Merge branch 'master' into kevinmind/addons/15308
KevinMind 66319d4
TMP: raise error on last attempt
KevinMind 5cdb956
Fix incorrect URL in services monitor check
KevinMind File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
name: Health Check | ||
|
||
on: | ||
# Run the workflow test on push events | ||
push: | ||
# Run the main workflow on workflow_dispatch or schedule | ||
workflow_dispatch: | ||
schedule: | ||
# Every 5 minutes | ||
- cron: '*/5 * * * *' | ||
|
||
jobs: | ||
health_check: | ||
runs-on: ubuntu-latest | ||
strategy: | ||
fail-fast: false | ||
matrix: | ||
environment: ${{fromJson(github.event_name == 'push' && '["local"]' || '["dev","stage","prod"]')}} | ||
|
||
steps: | ||
- uses: actions/checkout@v4 | ||
|
||
- uses: ./.github/actions/run-docker | ||
with: | ||
target: development | ||
version: local | ||
run: ./scripts/health_check.py --env ${{ matrix.environment }} --verbose | ||
|
||
|
||
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,110 @@ | ||
#!/usr/bin/env python3 | ||
|
||
import argparse | ||
import json | ||
import time | ||
from enum import Enum | ||
|
||
import requests | ||
|
||
|
||
ENV_ENUM = Enum( | ||
'ENV', | ||
[ | ||
('dev', 'https://addons-dev.allizom.org'), | ||
('stage', 'https://addons.allizom.org'), | ||
('prod', 'https://addons.mozilla.org'), | ||
# For local environments hit the nginx container as set in docker-compose.yml | ||
('local', 'http://nginx'), | ||
], | ||
) | ||
|
||
|
||
class Fetcher: | ||
def __init__(self, env: ENV_ENUM, verbose: bool = False): | ||
self.environment = ENV_ENUM[env] | ||
self.verbose = verbose | ||
|
||
def _fetch(self, path: str) -> dict[str, str] | None: | ||
url = f'{self.environment.value}/{path}' | ||
if self.verbose: | ||
print(f'Requesting {url} for {self.environment.name}') | ||
|
||
data = None | ||
# We return 500 if any of the monitors are failing. | ||
# So instead of raising, we should try to form valid JSON | ||
# and determine if we should raise later based on the json values. | ||
try: | ||
response = requests.get(url, allow_redirects=False) | ||
data = response.json() | ||
except (requests.exceptions.HTTPError, json.JSONDecodeError) as e: | ||
if self.verbose: | ||
print( | ||
{ | ||
'error': e, | ||
'data': data, | ||
'response': response, | ||
} | ||
) | ||
|
||
if self.verbose and data is not None: | ||
print(json.dumps(data, indent=2)) | ||
|
||
return data | ||
|
||
def version(self): | ||
return self._fetch('__version__') | ||
|
||
def heartbeat(self): | ||
return self._fetch('__heartbeat__') | ||
|
||
def monitors(self): | ||
return self._fetch('services/__heartbeat__') | ||
|
||
|
||
def main(env: ENV_ENUM, verbose: bool = False): | ||
fetcher = Fetcher(env, verbose) | ||
|
||
version_data = fetcher.version() | ||
heartbeat_data = fetcher.heartbeat() | ||
monitors_data = fetcher.monitors() | ||
|
||
if version_data is None: | ||
raise ValueError('Error fetching version data') | ||
|
||
if heartbeat_data is None: | ||
raise ValueError('Error fetching heartbeat data') | ||
|
||
if monitors_data is None: | ||
raise ValueError('Error fetching monitors data') | ||
|
||
combined_data = {**heartbeat_data, **monitors_data} | ||
failing_monitors = [ | ||
name for name, monitor in combined_data.items() if monitor['state'] is False | ||
] | ||
|
||
if len(failing_monitors) > 0: | ||
raise ValueError(f'Some monitors are failing {failing_monitors}') | ||
|
||
|
||
if __name__ == '__main__': | ||
args = argparse.ArgumentParser() | ||
args.add_argument( | ||
'--env', type=str, choices=list(ENV_ENUM.__members__.keys()), required=True | ||
) | ||
args.add_argument('--verbose', action='store_true') | ||
args.add_argument('--retries', type=int, default=3) | ||
args = args.parse_args() | ||
|
||
attempt = 1 | ||
|
||
while attempt <= args.retries: | ||
try: | ||
main(args.env, args.verbose) | ||
break | ||
except Exception as e: | ||
print(f'Error: {e}') | ||
if attempt == args.retries: | ||
raise | ||
time.sleep(2**attempt) | ||
attempt += 1 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.