def add_interval_job(self, func, weeks=0, days=0, hours=0, minutes=0, seconds=0, start_date=None, args=None, kwargs=None, **options): """ Schedules a job to be completed on specified intervals. Any extra keyword arguments are passed along to the constructor of the :class:`~apscheduler.job.Job` class (see :ref:`job_options`). :param func: callable to run :param weeks: number of weeks to wait :param days: number of days to wait :param hours: number of hours to wait :param minutes: number of minutes to wait :param seconds: number of seconds to wait :param start_date: when to first execute the job and start the counter (default is after the given interval) :param args: list of positional arguments to call func with :param kwargs: dict of keyword arguments to call func with :param name: name of the job :param jobstore: alias of the job store to add the job to :param misfire_grace_time: seconds after the designated run time that the job is still allowed to be run :rtype: :class:`~apscheduler.job.Job` """ interval = timedelta(weeks=weeks, days=days, hours=hours, minutes=minutes, seconds=seconds) trigger = IntervalTrigger(interval, start_date) return self.add_job(trigger, func, args, kwargs, **options)
def update_schedule(instance, raw, using, update_fields, **kwargs): if instance.status == 1: if instance.attached_job_id is not None: job = instance.attached_job instance.attached_job_id = None instance.save() # in case foreignkey not update instance.attached_job = None sched.unschedule_job(job) return from sokoban.sync_worker import sync_project if instance.attached_job_id is None: interval = timedelta job_name = '%s : %s' % (instance.owner.id, instance.owner.name) job = sched.add_interval_job(sync_project, start_date=instance.start_time, args=(instance.owner_id,), name=job_name, hours=instance.interval) instance.attached_job_id = job.id instance.save() sched._wakeup.set() return interval = timedelta(hours=instance.interval) trigger = IntervalTrigger(interval, instance.start_time) instance.attached_job.trigger = trigger instance.attached_job.compute_next_run_time(timezone.now()) instance.attached_job.save() sched._wakeup.set()
class TestInterval(object): def setUp(self): interval = timedelta(seconds=1) trigger_start_date = datetime(2009, 8, 4, second=2) self.trigger = IntervalTrigger(interval, trigger_start_date) def test_interval_repr(self): eq_(repr(self.trigger), "<IntervalTrigger (interval=datetime.timedelta(0, 1), start_date=datetime.datetime(2009, 8, 4, 0, 0, 2))>") eq_(str(self.trigger), "interval[0:00:01]") def test_interval_before(self): start_date = datetime(2009, 8, 4) correct_next_date = datetime(2009, 8, 4, second=2) eq_(self.trigger.get_next_fire_time(start_date), correct_next_date) def test_interval_within(self): start_date = datetime(2009, 8, 4, second=2, microsecond=1000) correct_next_date = datetime(2009, 8, 4, second=3) eq_(self.trigger.get_next_fire_time(start_date), correct_next_date)
class TestInterval(object): def setUp(self): interval = timedelta(seconds=1) trigger_start_date = datetime(2009, 8, 4, second=2) self.trigger = IntervalTrigger(interval, trigger_start_date) def test_interval_repr(self): eq_(repr(self.trigger), "<IntervalTrigger (interval=datetime.timedelta(0, 1), " "start_date=datetime.datetime(2009, 8, 4, 0, 0, 2))>") eq_(str(self.trigger), "interval[0:00:01]") def test_interval_before(self): start_date = datetime(2009, 8, 4) correct_next_date = datetime(2009, 8, 4, second=2) eq_(self.trigger.get_next_fire_time(start_date), correct_next_date) def test_interval_within(self): start_date = datetime(2009, 8, 4, second=2, microsecond=1000) correct_next_date = datetime(2009, 8, 4, second=3) eq_(self.trigger.get_next_fire_time(start_date), correct_next_date)
def load_jobs(self): #continue standart execution jobs = [] for job_dict in self.collection.find({'crecord_type': 'schedule'}): try: job = Job.__new__(Job) if job_dict['aaa_owner'] != 'root': if job_dict['kwargs']['task'] != 'task_reporting': raise ValueError( "User %s isn\'t allow to run task %s" % (job_dict['aaa_owner'], job_dict['kwargs']['task'])) #keep memory of id job_dict_id = job_dict['_id'] job_dict['id'] = job_dict.pop('_id') if job_dict.has_key('runs'): job_dict['runs'] = job_dict['runs'] else: job_dict['runs'] = 0 job_dict['coalesce'] = False #try to get interval try: if job_dict['interval'] != None: job_dict['trigger'] = IntervalTrigger( timedelta(**job_dict['interval'])) except Exception, err: pass #try to get simple try: if job_dict['date'] != None: job_dict['trigger'] = SimpleTrigger( datetime(*job_dict['date'])) except Exception, err: pass #try to get crontab try: if job_dict['cron'] != None: job_dict['trigger'] = CronTrigger(**job_dict['cron']) except Exception, err: pass
def add_interval_job(self, func, weeks=0, days=0, hours=0, minutes=0, seconds=0, start_date=None, repeat=0, args=None, kwargs=None): """ Adds a job to be completed on specified intervals. :param func: callable to run :param weeks: number of weeks to wait :param days: number of days to wait :param hours: number of hours to wait :param minutes: number of minutes to wait :param seconds: number of seconds to wait :param start_date: when to first execute the job and start the counter (default is after the given interval) :param repeat: number of times the job will be run (0 = repeat indefinitely) :param args: list of positional arguments to call func with :param kwargs: dict of keyword arguments to call func with """ interval = timedelta(weeks=weeks, days=days, hours=hours, minutes=minutes, seconds=seconds) trigger = IntervalTrigger(interval, repeat, start_date) return self._add_job(trigger, func, args, kwargs)
def setUp(self): interval = timedelta(seconds=1) trigger_start_date = datetime(2009, 8, 4, second=2) self.trigger = IntervalTrigger(interval, trigger_start_date)
def test_interval_invalid_interval(): IntervalTrigger('1-6')
def load_jobs(self): #continue standart execution jobs = [] for job_dict in self.collection.find({'crecord_type': 'schedule'}): try: job = Job.__new__(Job) if job_dict['aaa_owner'] != 'account.root': if job_dict['kwargs']['task'] != 'task_reporting': raise ValueError("User %s isn\'t allow to run task %s" % (job_dict['aaa_owner'],job_dict['kwargs']['task'])) #keep memory of id job_dict_id = job_dict['_id'] job_dict['id'] = job_dict.pop('_id') if job_dict.has_key('runs'): job_dict['runs'] = job_dict['runs'] else: job_dict['runs'] = 0 job_dict['coalesce'] = False #try to get interval interval = job_dict.get('interval') if interval is not None: job_dict[TRIGGER] = IntervalTrigger(timedelta(**interval)) else: #try to get simple date = job_dict.get('date') if date is not None: job_dict[TRIGGER] = SimpleTrigger( datetime(*date)) else: #try to get crontab cron = job_dict.get('cron') if cron is not None: job_dict[TRIGGER] = CronTrigger(**cron) if TRIGGER not in job_dict: raise ValueError("No interval, nor date, nor cron is given in task %s".format(job_dict['crecord_name'])) job_dict['next_run_time'] = job_dict['trigger'].get_next_fire_time(datetime.now()) job_dict['args'] = job_dict['args'] job_dict['kwargs'] = job_dict['kwargs'] job_dict['max_runs'] = None job_dict['max_instances'] = 3 job_dict['name'] = job_dict['crecord_name'] job_dict['misfire_grace_time'] = 1 job_dict['func_ref'] = 'apschedulerlibs.aps_to_celery:launch_celery_task' job.__setstate__(job_dict) jobs.append(job) #change flag to true self.collection.update({'_id':job_dict_id},{"$set":{'loaded':True, 'next_run_time': job_dict['next_run_time']}},True) except Exception: job_name = job_dict.get('name', '(unknown)') logger.exception('Unable to restore job "%s"', job_name) logger.info(' + %s jobs loaded' % len(jobs)) self.jobs = jobs