def test_date_trigger_earlier(): fire_date = datetime(2009, 7, 6) trigger = SimpleTrigger(fire_date) eq_(repr(trigger), "<SimpleTrigger (run_date=datetime.datetime(2009, 7, 6, 0, 0))>") eq_(str(trigger), "date[2009-07-06 00:00:00]") start_date = datetime(2008, 12, 1) eq_(trigger.get_next_fire_time(start_date), fire_date)
def add_date_job(self, func, date, args=None, kwargs=None, **options): """ Schedules a job to be completed on a specific date and time. :param func: callable to run at the given time :param date: the date/time to run the job at :param name: name of the job :param jobstore: stored the job in the named (or given) job store :param misfire_grace_time: seconds after the designated run time that the job is still allowed to be run :type date: :class:`datetime.date` :rtype: :class:`~apscheduler.job.Job` """ trigger = SimpleTrigger(date) return self.add_job(trigger, func, args, kwargs, **options)
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 test_date_trigger_text(): trigger = SimpleTrigger('2009-7-6') start_date = datetime(2009, 7, 6) eq_(trigger.get_next_fire_time(start_date), datetime(2009, 7, 6))
def test_date_trigger_later(): fire_date = datetime(2009, 7, 6) trigger = SimpleTrigger(fire_date) start_date = datetime(2009, 7, 7) eq_(trigger.get_next_fire_time(start_date), None)
def test_date_trigger_exact(): fire_date = datetime(2009, 7, 6) trigger = SimpleTrigger(fire_date) start_date = datetime(2009, 7, 6) eq_(trigger.get_next_fire_time(start_date), fire_date)
def setup(self): self.trigger_date = datetime(2999, 1, 1) self.earlier_date = datetime(2998, 12, 31) self.trigger = SimpleTrigger(self.trigger_date) self.job = Job(self.trigger, dummy_job, [], {}, 1, False) self.job.next_run_time = self.trigger_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'] != '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