-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #54 from MichaelCurrin/refactor-split-main-script
refactor: Split main script into modules
- Loading branch information
Showing
19 changed files
with
441 additions
and
356 deletions.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,4 @@ | ||
__pycache__/ | ||
.pytest_cache/ | ||
.mypy_cache/ | ||
|
||
venv | ||
|
||
|
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,24 +1,26 @@ | ||
#!/bin/bash | ||
# Run Unicron as a cron command. | ||
# | ||
# On success, be silent. | ||
# On error, send all output to the user's local mailbox. | ||
# Run Unicron as a scheduled cron command. | ||
# | ||
# This script can be run from anywhere. | ||
# | ||
# On success, run silently - nothing printed at all. | ||
# On error, capture all stdout and stderr so it can be sent to user's local mailbox using cron's mechanism or the mail command. | ||
# Either way, you won't see anything printed if run this command. | ||
|
||
set -e | ||
|
||
SCRIPT_DIR=$(dirname $(realpath $0)) | ||
SCRIPT_FILEPATH="$SCRIPT_DIR/../unicron/unicron.py" | ||
SCRIPT_DIR=$(dirname $(dirname $(realpath "$0"))) | ||
cd "$SCRIPT_DIR" | ||
|
||
set +e | ||
OUTPUT="$($SCRIPT_FILEPATH 2>&1)" | ||
|
||
CMD_OUTPUT="$(make run-quiet 2>&1)" | ||
|
||
if [[ $? -ne 0 ]]; then | ||
set -e | ||
echo "$OUTPUT" | mail -s 'Unicron task failed!' $USER | ||
set -e | ||
echo "$CMD_OUTPUT" | mail -s 'Unicron task failed!' $USER | ||
|
||
exit 1 | ||
exit 1 | ||
fi | ||
|
||
exit 0 |
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
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 |
---|---|---|
@@ -0,0 +1,27 @@ | ||
""" | ||
History module tests. | ||
""" | ||
# pylint: disable=missing-function-docstring | ||
import datetime | ||
|
||
from unicron import history | ||
|
||
|
||
def test_get_last_run_date(): | ||
assert history.get_last_run_date("never_run_before.sh") is None | ||
assert history.get_last_run_date("fail.sh") is None | ||
|
||
assert history.get_last_run_date("old.sh") == datetime.date( | ||
year=2020, month=1, day=1 | ||
) | ||
assert history.get_last_run_date("today.sh") == datetime.date.today() | ||
|
||
|
||
def test_check_need_to_run(): | ||
# Expect to run. | ||
assert history.check_need_to_run("never_run_before.sh") is True | ||
assert history.check_need_to_run("fail.sh") is True | ||
assert history.check_need_to_run("old.sh") is True | ||
|
||
# Expect not to run. | ||
assert history.check_need_to_run("today.sh") is False |
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,20 @@ | ||
""" | ||
Logger module tests. | ||
""" | ||
# pylint: disable=missing-function-docstring | ||
from pathlib import Path | ||
|
||
from unicron import logger | ||
|
||
|
||
APP_DIR = Path("unicron") | ||
VAR_DIR = APP_DIR / Path("_test_var") | ||
LOG_DIR = VAR_DIR / "last_run" | ||
|
||
|
||
def test_setup_logger(): | ||
app_logger = logger.setup_logger(__name__, VAR_DIR / "app.log", is_task=False) | ||
app_logger.debug("test_setup_logger", extra={"task": "pytest"}) | ||
|
||
task_logger = logger.setup_logger(__name__, LOG_DIR / "unit_task.log", is_task=True) | ||
task_logger.debug("test_setup_logger") |
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,17 @@ | ||
""" | ||
Paths module tests. | ||
""" | ||
# pylint: disable=missing-function-docstring | ||
from unicron import paths | ||
|
||
|
||
def test_mk_last_run_path(): | ||
path = paths.mk_last_run_path("foo") | ||
|
||
assert str(path).endswith("_test_var/last_run/foo.txt") | ||
|
||
|
||
def test_mk_output_path(): | ||
path = paths.mk_output_path("foo") | ||
|
||
assert str(path).endswith("_test_var/output/foo.log") |
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,21 @@ | ||
""" | ||
Run module tests. | ||
""" | ||
# pylint: disable=missing-function-docstring | ||
from unicron import run | ||
|
||
|
||
def test_run_in_shell_success(): | ||
cmd = 'echo "Test output"' | ||
success, output = run.run_in_shell(cmd) | ||
|
||
assert success | ||
assert output == "Test output" | ||
|
||
|
||
def test_run_in_shell_fail(): | ||
cmd = "echo Fail! ; exit 1" | ||
success, output = run.run_in_shell(cmd) | ||
|
||
assert not success | ||
assert output == "Fail!" |
Oops, something went wrong.