-
-
Notifications
You must be signed in to change notification settings - Fork 631
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Refactor all Celery tasks to a separate file (#1938)
- Loading branch information
1 parent
4665137
commit 0fd729b
Showing
7 changed files
with
103 additions
and
81 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 |
---|---|---|
@@ -0,0 +1,66 @@ | ||
import sh | ||
|
||
from celery import Celery | ||
from datetime import timedelta | ||
from lib import diagnostics | ||
from lib.utils import ( | ||
connect_to_redis, | ||
is_balena_app, | ||
reboot_via_balena_supervisor, | ||
shutdown_via_balena_supervisor, | ||
) | ||
from os import getenv, path | ||
from retry.api import retry_call | ||
|
||
|
||
CELERY_RESULT_BACKEND = getenv('CELERY_RESULT_BACKEND', 'redis://localhost:6379/0') | ||
CELERY_BROKER_URL = getenv('CELERY_BROKER_URL', 'redis://localhost:6379/0') | ||
CELERY_TASK_RESULT_EXPIRES = timedelta(hours=6) | ||
|
||
r = connect_to_redis() | ||
celery = Celery( | ||
'Anthias Celery Worker', | ||
backend=CELERY_RESULT_BACKEND, | ||
broker=CELERY_BROKER_URL, | ||
result_expires=CELERY_TASK_RESULT_EXPIRES | ||
) | ||
|
||
|
||
@celery.on_after_configure.connect | ||
def setup_periodic_tasks(sender, **kwargs): | ||
# Calls cleanup() every hour. | ||
sender.add_periodic_task(3600, cleanup.s(), name='cleanup') | ||
sender.add_periodic_task(60*5, get_display_power.s(), name='display_power') | ||
|
||
|
||
@celery.task(time_limit=30) | ||
def get_display_power(): | ||
r.set('display_power', diagnostics.get_display_power()) | ||
r.expire('display_power', 3600) | ||
|
||
|
||
@celery.task | ||
def cleanup(): | ||
sh.find(path.join(getenv('HOME'), 'screenly_assets'), '-name', '*.tmp', '-delete') | ||
|
||
|
||
@celery.task | ||
def reboot_anthias(): | ||
""" | ||
Background task to reboot Anthias | ||
""" | ||
if is_balena_app(): | ||
retry_call(reboot_via_balena_supervisor, tries=5, delay=1) | ||
else: | ||
r.publish('hostcmd', 'reboot') | ||
|
||
|
||
@celery.task | ||
def shutdown_anthias(): | ||
""" | ||
Background task to shutdown Anthias | ||
""" | ||
if is_balena_app(): | ||
retry_call(shutdown_via_balena_supervisor, tries=5, delay=1) | ||
else: | ||
r.publish('hostcmd', 'shutdown') |
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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Large diffs are not rendered by default.
Oops, something went wrong.
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