Beispiel #1
0
def run_job(job):
    """ Run this job immediately if this job doesn't have a schedule,
        If this job has a schedule, schedule it to run.
    """

    spider = Spider.get(Spider.name == job.spider_name)
    project_name = spider.project_name

    if job.schedule:
        minute, hour, day_of_week, day_of_month, month_of_year = job.schedule.split(
            ' ')
        utc_hour = crontab_hour_to_utc(hour, config.CRONTAB_TIMEZONE)
        interval = crontab(minute, utc_hour, day_of_week, day_of_month,
                           month_of_year)

        entry = RedBeatSchedulerEntry(job.job_id,
                                      'server.tasks.spiders.crawl',
                                      interval,
                                      kwargs={
                                          'project': project_name,
                                          'spider': job.spider_name,
                                          'job_id': job.job_id,
                                          'parameters': job.parameters,
                                      },
                                      app=app)

        entry.save().reschedule()

    else:
        crawl.delay(project_name, job.spider_name, job.job_id, job.parameters)
Beispiel #2
0
def create_task(mapper, connection, target):
    assert target.id is not None
    # TODO(boertel) somehow target isn't the object from the database and the
    # deserialization of schedule isn't done
    job = Job.query.filter_by(id=target.id).first()
    if job:
        celery_app = celery.get_celery_app()
        task_name = 'cuckoo.tasks.webhook.webhook'
        args = [
            str(job.id),
            str(job.application_id),
        ]
        kwargs = {
            'url': job.url,
            'data': job.params,
        }
        entry = RedBeatSchedulerEntry(
            str(job.id),
            task_name,
            job.schedule,
            args=args,
            kwargs=kwargs,
            app=celery_app,
        )
        entry.save()
Beispiel #3
0
 def create_schedule(self, app):
     data = {
         'classname': self.classname,
         'params': self.kwargs
     }
     interval = schedules.schedule(run_every=self.interval)
     entry = RedBeatSchedulerEntry(
         self.taskname,
         'kryptobot.workers.harvester.tasks.launch_harvester',
         interval,
         kwargs=data,
         app=app
     )
     entry.save()
Beispiel #4
0
def start_timelapse_task(header: str, run_every: float, expire_at: str,
                         data: dict) -> str:
    try:
        interval = celery.schedules.schedule(run_every=run_every)  # seconds
        entry = RedBeatSchedulerEntry('timelapse',
                                      'cam_task.capture',
                                      interval,
                                      args=[header, data],
                                      app=app)
        entry.save()
        return entry.key
    except Exception as e:
        logger.error(traceback.format_exc())
        raise TaskError(e)
Beispiel #5
0
    def __init__(self, worker, **options):
        """Initialize class instance.

        Passes User Options defined by the worker to :class:`GraphApiTask`
        base class and schedules periodic data extraction from MS Graph API
        into the post-processing queue.

        Note
        ----
        By default, no config file is expected by the app. All options are taken
        either from the command-line interface (CLI) or the execution environment
        (ENV).

        Config file takes precedence over the CLI, which in turn takes precedence
        over ENV.

        """
        from ms_graph_exporter.celery.graph_api_base import GraphApiTask

        GraphApiTask.config_update(**options)

        if options["graph_app_config"]:
            GraphApiTask.config_update_from_file()

        logger = get_logger(__name__)

        map_interval = (GraphApiTask._config["graph_streams"] *
                        GraphApiTask._config["graph_stream_frame"])

        entry = RedBeatSchedulerEntry("periodic_map_streams",
                                      "graph.map_streams",
                                      map_interval,
                                      app=worker.app)

        logger.info("adding schedule entry, %s", entry)
        entry.save()
from celery.schedules import schedule, crontab, solar
from redbeat import RedBeatSchedulerEntry

import configs

celery = Celery(__name__)
celery.config_from_object(configs)


@celery.task
def my_add(a, b):
    result = a + b
    print('{} + {} = {}'.format(a, b, result))
    return result


@celery.task
def my_subtraction(a, b):
    result = a - b
    print('{} - {} = {}'.format(a, b, result))
    return result


interval = schedule(run_every=5)  # seconds
cron = crontab()
entry = RedBeatSchedulerEntry('my_add', '{}.my_add'.format(__name__), interval, args=[5, 2], app=celery)
entry.save()

interval = schedule(run_every=5)  # seconds
entry = RedBeatSchedulerEntry('my_subtraction', 'celery_redbeat_demo.my_subtraction', interval, args=[5, 2], app=celery)
entry.save()
from redbeat import RedBeatSchedulerEntry
from celery_redbeat_demo import celery, my_subtraction
from celery.schedules import schedule

interval = schedule(run_every=5)  # seconds
entry = RedBeatSchedulerEntry('my_subtraction',
                              'celery_redbeat_demo.my_add',
                              interval,
                              args=[5, 2],
                              app=celery)
entry.save()