def create_periodic_task(cls, name, task, every, period='seconds', args=[], kwargs={}): if cls.CELERY_BEAT_SCHEDULER == 'MONGODB': from celerybeatmongo.models import PeriodicTask PeriodicTask( name=name, task=task, enabled=True, args=args, kwargs=kwargs, interval=PeriodicTask.Interval(every=every, period=period), ).save() elif cls.CELERY_BEAT_SCHEDULER == 'REDIS': from celery.schedules import schedule from redbeat.schedulers import RedBeatSchedulerEntry if period != 'seconds': # do conversion... run_every should be a datetime.timedelta log.error("Unsupported period {} for redis beat", period) interval = schedule(run_every=every) # seconds entry = RedBeatSchedulerEntry( name, task, interval, args=args, app=CeleryExt.celery_app ) entry.save() else: log.error( "Unsupported celery-beat scheduler: {}", cls.CELERY_BEAT_SCHEDULER)
def create_crontab_task( cls, name: str, task: str, minute: str, hour: str, day_of_week: str = "*", day_of_month: str = "*", month_of_year: str = "*", args: List[Any] = None, kwargs: Dict[str, Any] = None, ) -> None: if args is None: args = [] if kwargs is None: kwargs = {} if cls.CELERYBEAT_SCHEDULER == "MONGODB": from celerybeatmongo.models import PeriodicTask PeriodicTask( name=name, task=task, enabled=True, args=args, kwargs=kwargs, crontab=PeriodicTask.Crontab( minute=minute, hour=hour, day_of_week=day_of_week, day_of_month=day_of_month, month_of_year=month_of_year, ), ).save() elif cls.CELERYBEAT_SCHEDULER == "REDIS": from celery.schedules import crontab from redbeat.schedulers import RedBeatSchedulerEntry interval = crontab( minute=minute, hour=hour, day_of_week=day_of_week, day_of_month=day_of_month, month_of_year=month_of_year, ) entry = RedBeatSchedulerEntry(name, task, interval, args=args, app=CeleryExt.celery_app) entry.save() else: raise AttributeError( f"Unsupported celery-beat scheduler: {cls.CELERYBEAT_SCHEDULER}" )
def periodic_ticker(): """add a new periodic ticker task running every minute""" interval = cly.schedules.schedule(run_every=60) # seconds entry = RedBeatSchedulerEntry('one_min_ticker:' + str(time.time()), 'bittrex.tasks.tick_task', interval, args=[], app=celery) entry.save() # import pdb; pdb.set_trace() return jsonify({"task_name": entry.name}), 202
def create_crontab_task( cls, name, task, minute, hour, day_of_week="*", day_of_month="*", month_of_year="*", args=[], kwargs={}, ): if cls.CELERY_BEAT_SCHEDULER == 'MONGODB': from celerybeatmongo.models import PeriodicTask PeriodicTask( name=name, task=task, enabled=True, args=args, kwargs=kwargs, crontab=PeriodicTask.Crontab( minute=minute, hour=hour, day_of_week=day_of_week, day_of_month=day_of_month, month_of_year=month_of_year, ), ).save() elif cls.CELERY_BEAT_SCHEDULER == 'REDIS': from celery.schedules import crontab from redbeat.schedulers import RedBeatSchedulerEntry interval = crontab( minute=minute, hour=hour, day_of_week=day_of_week, day_of_month=day_of_month, month_of_year=month_of_year ) entry = RedBeatSchedulerEntry( name, task, interval, args=args, app=CeleryExt.celery_app ) entry.save() else: log.error( "Unsupported celery-beat scheduler: {}", cls.CELERY_BEAT_SCHEDULER)
def create_periodic_task( cls, name: str, task: str, every: Union[str, int, timedelta], period: AllowedTimedeltaPeriods = "seconds", args: List[Any] = None, kwargs: Dict[str, Any] = None, ) -> None: if args is None: args = [] if kwargs is None: kwargs = {} if cls.CELERYBEAT_SCHEDULER == "MONGODB": from celerybeatmongo.models import PeriodicTask PeriodicTask( name=name, task=task, enabled=True, args=args, kwargs=kwargs, interval=PeriodicTask.Interval(every=every, period=period), ).save() elif cls.CELERYBEAT_SCHEDULER == "REDIS": from celery.schedules import schedule from redbeat.schedulers import RedBeatSchedulerEntry # convert strings and integers to timedeltas if isinstance(every, str) and every.isdigit(): every = get_timedelta(int(every), period) elif isinstance(every, int): every = get_timedelta(every, period) if not isinstance(every, timedelta): t = type(every).__name__ raise AttributeError( f"Invalid input parameter every = {every} (type {t})") interval = schedule(run_every=every) entry = RedBeatSchedulerEntry(name, task, interval, args=args, app=CeleryExt.celery_app) entry.save() else: raise AttributeError( f"Unsupported celery-beat scheduler: {cls.CELERYBEAT_SCHEDULER}" )
def get(self): """ Get the scheduler entry for the task """ try: entry = self.create() return RedBeatSchedulerEntry.from_key(entry.key, app=celery) except KeyError: return None
def get_periodic_task(cls, name: str) -> Any: if cls.CELERYBEAT_SCHEDULER == "REDIS": from redbeat.schedulers import RedBeatSchedulerEntry try: task_key = f"{REDBEAT_KEY_PREFIX}{name}" return RedBeatSchedulerEntry.from_key(task_key, app=CeleryExt.celery_app) except KeyError: return None raise AttributeError( f"Unsupported celery-beat scheduler: {cls.CELERYBEAT_SCHEDULER}")
def create(self): entry = RedBeatSchedulerEntry( name=self.name, task=self.task, schedule=schedule( run_every=self.run_every, # setting "now" to the job start datetime nowfun=lambda: self.run_at, app=celery, ), args=(self.spark_job.pk, ), kwargs={}, app=celery, ) return entry
def create_entry(self, name=None, task=None, s=None, run_every=60, **kwargs): if name is None: name = 'test' if task is None: task = 'tasks.test' if s is None: s = schedule(run_every=run_every) e = RedBeatSchedulerEntry(name, task, s, app=self.app, **kwargs) return e
def get_periodic_task(cls, name): if cls.CELERY_BEAT_SCHEDULER == 'MONGODB': from celerybeatmongo.models import PeriodicTask, DoesNotExist try: return PeriodicTask.objects.get(name=name) except DoesNotExist: return None elif cls.CELERY_BEAT_SCHEDULER == 'REDIS': from redbeat.schedulers import RedBeatSchedulerEntry try: task_key = "{}{}".format(cls.REDBEAT_KEY_PREFIX, name) return RedBeatSchedulerEntry.from_key( task_key, app=CeleryExt.celery_app) except KeyError: return None else: log.error( "Unsupported celery-beat scheduler: {}", cls.CELERY_BEAT_SCHEDULER)
def get_periodic_task(cls, name: str) -> Any: if cls.CELERYBEAT_SCHEDULER == "MONGODB": from celerybeatmongo.models import DoesNotExist, PeriodicTask try: return PeriodicTask.objects.get(name=name) except DoesNotExist: return None if cls.CELERYBEAT_SCHEDULER == "REDIS": from redbeat.schedulers import RedBeatSchedulerEntry try: task_key = f"{REDBEAT_KEY_PREFIX}{name}" return RedBeatSchedulerEntry.from_key(task_key, app=CeleryExt.celery_app) except KeyError: return None raise AttributeError( f"Unsupported celery-beat scheduler: {cls.CELERYBEAT_SCHEDULER}")
from tasks import add # ======== Test basic task ======== # result = add.delay(2,3) # time.sleep(1) # print result.get() # ======== Test adding cron by Python ======== # Need to initialize an app with same config as running Celery app app = Celery('tasks') app.config_from_object('celeryconfig') cron = celery.schedules.crontab(minute="1", hour="1") entry = RedBeatSchedulerEntry('test7', 'tasks.add', cron, args=[5, 1], app=app) entry.save() # ======== Test adding cron by Redis ======== # import redis # r = redis.StrictRedis(host='localhost', port=6379, db=1) # cron_def = { # "name": "test_direct_insert_2", # "task": "tasks.add", # "schedule": { # "__type__": "crontab", # "minute": "5", # optional, defaults to * # "hour": "1", # optional, defaults to * # "day_of_week": "*", # optional, defaults to * # "day_of_month": "*", # optional, defaults to *