-
Notifications
You must be signed in to change notification settings - Fork 9
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
Cron / Updated cli #234
Merged
Merged
Cron / Updated cli #234
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
79ca232
Update container from buster to bookworm
MelissaAutumn 191ebde
Add typer for cli routing, and start work on a basic cron system.
MelissaAutumn 62b95ac
Merge branch 'main' into features/230-cron
MelissaAutumn e109133
Add a cron_lock helper function, and fix filename for cli routes.
MelissaAutumn 06db20a
Adjust entry scripts for updated cli routing.
MelissaAutumn 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 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 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 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 |
---|---|---|
@@ -1,10 +1,13 @@ | ||
#!/bin/sh | ||
|
||
run-command update-db | ||
run-command main update-db | ||
|
||
# Start up fake mail server | ||
python -u -m smtpd -n -c DebuggingServer localhost:8050 & | ||
|
||
# Start cron | ||
service cron start | ||
|
||
# Start up real webserver | ||
uvicorn --factory appointment.main:server --reload --host 0.0.0.0 --port 5173 | ||
|
This file contains 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 |
---|---|---|
@@ -1,5 +1,5 @@ | ||
#!/bin/sh | ||
|
||
run-command update-db | ||
run-command main update-db | ||
|
||
uvicorn --factory appointment.main:server --host 0.0.0.0 --port 5000 |
This file contains 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 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,31 @@ | ||
"""This file handles routing for console commands""" | ||
from contextlib import contextmanager | ||
import os | ||
|
||
import typer | ||
from ..commands import update_db | ||
|
||
router = typer.Typer() | ||
|
||
|
||
@contextmanager | ||
def cron_lock(lock_name): | ||
"""Context manager helper to create a cron lockfile or error out with FileExistsError.""" | ||
lock_file_name = f'/tmp/{lock_name}.lock' | ||
|
||
# Lock file exists? Don't run | ||
if os.path.isfile(lock_file_name): | ||
raise FileExistsError | ||
|
||
fh = open(lock_file_name, 'w+') | ||
try: | ||
yield | ||
finally: | ||
fh.close() | ||
os.remove(lock_file_name) | ||
|
||
|
||
@router.command('update-db') | ||
def update_database(): | ||
update_db.run() | ||
|
This file contains 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,31 @@ | ||
import os | ||
|
||
import pytest | ||
|
||
from appointment.routes.commands import cron_lock | ||
|
||
|
||
def test_cron_lock(): | ||
"""Test our cron lock function, this does use disk io but should clean itself up after.""" | ||
test_lock_name = 'test_cron_lock_run' | ||
test_lock_file_name = f'/tmp/{test_lock_name}.lock' | ||
|
||
# Clean up in case the lock file previously exists | ||
if os.path.isfile(test_lock_file_name): | ||
os.remove(test_lock_file_name) | ||
|
||
# Test that the lock works | ||
with cron_lock(test_lock_name): | ||
assert os.path.isfile(test_lock_file_name) | ||
|
||
# And cleans itself up | ||
assert not os.path.isfile(test_lock_file_name) | ||
|
||
# Test a lock already exists case with way too many withs. | ||
with open(test_lock_file_name, 'w'): | ||
with pytest.raises(FileExistsError): | ||
with cron_lock(test_lock_name): | ||
pass | ||
|
||
# Remove the lock file we manually created | ||
os.remove(test_lock_file_name) |
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This cron file is currently empty, right? Should we add an example entry here for convenience? Or add some documentation how to use it? Or is this handled by Typer automatically?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Oh yea sorry, the cron is empty. Once we want to add stuff we'll just stuff it in that file and it should work.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nice, fine by me 👍🏻