This repository has been archived by the owner on Oct 5, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 127
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
restore fire_th to let the choice to use Celery or not + Fix Celery c…
…onfig for crontab
- Loading branch information
Olivier Demah
committed
Jun 15, 2015
1 parent
1db7432
commit 1b9e1ea
Showing
7 changed files
with
293 additions
and
69 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
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,28 +1,15 @@ | ||
from __future__ import absolute_import | ||
|
||
from __future__ import absolute_import | ||
import os | ||
|
||
from celery import Celery | ||
from celery.schedules import crontab | ||
from django.conf import settings | ||
|
||
# set the default Django settings module for the 'celery' program. | ||
os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'django_th.settings') | ||
|
||
app = Celery('django_th') | ||
app = Celery('th') | ||
|
||
# Using a string here means the worker will not have to | ||
# pickle the object when using Windows. | ||
app.config_from_object('django.conf:settings') | ||
app.autodiscover_tasks(lambda: settings.INSTALLED_APPS) | ||
|
||
# will start the 2 tasks, | ||
# one each 41 minutes | ||
# the other one each hour | ||
CELERYBEAT_SCHEDULE = { | ||
'add-read-data': { | ||
'task': 'tasks.read_data', | ||
'schedule': crontab(minute='*/41'), | ||
}, | ||
'add-publish-data': { | ||
'task': 'tasks.publish_data', | ||
'schedule': crontab(hour='*/1'), | ||
}, | ||
} |
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 was deleted.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,22 +1,77 @@ | ||
#!/usr/bin/env python | ||
# -*- coding: utf-8 -*- | ||
# coding: utf-8 | ||
from __future__ import unicode_literals | ||
|
||
from django.core.management.base import BaseCommand | ||
from django.utils.log import getLogger | ||
|
||
from django_th.tasks import read_data | ||
|
||
from django_th.services import default_provider | ||
from django_th.models import TriggerService | ||
from django.utils.log import getLogger | ||
# create logger | ||
logger = getLogger('django_th.trigger_happy') | ||
|
||
|
||
class Command(BaseCommand): | ||
|
||
help = 'Trigger all the services and put them in cache' | ||
help = 'Trigger all the services to put them in cache' | ||
|
||
def handle(self, *args, **options): | ||
""" | ||
get all the triggers that need to be handled | ||
run the main process | ||
""" | ||
read_data.delay() | ||
default_provider.load_services() | ||
trigger = TriggerService.objects.filter(status=True).select_related( | ||
'consumer__name', 'provider__name') | ||
|
||
for service in trigger: | ||
self.put_in_cache.delay(service) | ||
|
||
def put_in_cache(self, service): | ||
# flag to know if we have to update | ||
to_update = False | ||
# flag to get the status of a service | ||
status = False | ||
# counting the new data to store to display them in the log | ||
# provider - the service that offer data | ||
service_name = str(service.provider.name.name) | ||
service_provider = default_provider.get_service(service_name) | ||
|
||
service_name = str(service.consumer.name.name) | ||
|
||
# check if the service has already been triggered | ||
# if date_triggered is None, then it's the first run | ||
if service.date_triggered is None: | ||
logger.debug("first run for %s => %s " % ( | ||
str(service.provider.name), str(service.consumer.name.name))) | ||
to_update = True | ||
status = True | ||
# run run run | ||
else: | ||
# 1) get the data from the provider service | ||
# get a timestamp of the last triggered of the service | ||
datas = getattr(service_provider, 'read_data')( | ||
service.provider.token, | ||
service.id, | ||
service.date_triggered) | ||
if len(datas) > 0: | ||
to_update = True | ||
status = True | ||
|
||
# update the date of the trigger at the end of the loop | ||
sentence = "user: {} - provider: {} - {}" | ||
if to_update: | ||
if status: | ||
logger.info((sentence + " - {} data put in cache").format( | ||
service.user, | ||
service.provider.name.name, | ||
service.description, | ||
len(datas))) | ||
else: | ||
logger.info((sentence + " AN ERROR OCCURS ").format( | ||
service.user, | ||
service.provider.name.name, | ||
service.description)) | ||
else: | ||
logger.info((sentence + " nothing new").format( | ||
service.user, | ||
service.provider.name.name, | ||
service.description)) |
Oops, something went wrong.