Skip to content

Commit

Permalink
Add scheduler Django app with heartbeat task
Browse files Browse the repository at this point in the history
  • Loading branch information
kacperlukawski committed Apr 9, 2020
1 parent f9f3af0 commit 4df826b
Show file tree
Hide file tree
Showing 14 changed files with 50 additions and 12 deletions.
7 changes: 6 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,8 +1,13 @@
# Python compiled files
*.pyc
__pycache__/

# IDE specific directories
.idea/

# Ignoring Heroku files in their target versions
.env

# Project specific files that should not be exposed
db.sqlite3
db.sqlite3
celerybeat-schedule
2 changes: 1 addition & 1 deletion Procfile
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
web: gunicorn application.wsgi
worker: celery worker --app=application.celery
worker: celery worker -B -l info -A application.celery
5 changes: 5 additions & 0 deletions application/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
# This will make sure the app is always imported when
# Django starts so that shared_task will use this app.
from .celery import app as celery_app

__all__ = ("celery_app", )
15 changes: 9 additions & 6 deletions application/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@
"django.contrib.sessions",
"django.contrib.messages",
"django.contrib.staticfiles",
"scheduler.apps.SchedulerConfig",
]

MIDDLEWARE = [
Expand Down Expand Up @@ -176,11 +177,13 @@

CELERY_BEAT_SCHEDULE = {
#
# A list of scheduled tasks
#
# "your-complex-task-name": {
# "task": "task_function_name",
# "schedule": 60.0,
# }
# A list of scheduled tasks which are supposed to be launched in selected
# intervals. By default, a simple heartbeat task is implemented so the
# logs should show the Celery worker is running fine.
#
"celery_heartbeat": {
"task": "celery_heartbeat",
"schedule": 60.0,
}

}
2 changes: 1 addition & 1 deletion docker/.gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
# This directory contains all the Docker volumes data and is not required
# to be included in the respository.
.data/*
.data/
6 changes: 3 additions & 3 deletions manage.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
#!/usr/bin/env python
"""Django's command-line utility for administrative tasks."""
"""Django"s command-line utility for administrative tasks."""
import os
import sys


def main():
os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'application.settings')
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "application.settings")
try:
from django.core.management import execute_from_command_line
except ImportError as exc:
Expand All @@ -17,5 +17,5 @@ def main():
execute_from_command_line(sys.argv)


if __name__ == '__main__':
if __name__ == "__main__":
main()
Empty file added scheduler/__init__.py
Empty file.
3 changes: 3 additions & 0 deletions scheduler/admin.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
from django.contrib import admin

# Register your models here.
5 changes: 5 additions & 0 deletions scheduler/apps.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
from django.apps import AppConfig


class SchedulerConfig(AppConfig):
name = "scheduler"
Empty file.
3 changes: 3 additions & 0 deletions scheduler/models.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
from django.db import models

# Create your models here.
8 changes: 8 additions & 0 deletions scheduler/tasks.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
from datetime import datetime

from celery import shared_task


@shared_task(name="celery_heartbeat")
def celery_heartbeat():
return "Celery heartbeat at {}".format(datetime.now())
3 changes: 3 additions & 0 deletions scheduler/tests.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
from django.test import TestCase

# Create your tests here.
3 changes: 3 additions & 0 deletions scheduler/views.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
from django.shortcuts import render

# Create your views here.

0 comments on commit 4df826b

Please sign in to comment.