def schedule(self, dt, func, interval=0, repeat=0, *args, **kwargs): """ Add the job to the scheduler for the specified time, interval, and number of repeats. Repeat of None with a specified interval means the job will repeat forever at that interval. """ if not isinstance(dt, datetime): raise ValueError("Time argument must be a datetime object.") if not interval and repeat != 0: raise ValueError( "Must specify an interval if the task is repeating") if isinstance(func, Job): job = func # else, turn it into a job first. else: job = Job(func, *args, **kwargs) job.state = State.SCHEDULED with self.session_scope() as session: scheduled_job = ScheduledJob( id=job.job_id, queue=self.queue.name, interval=interval, repeat=repeat, scheduled_time=dt, obj=job, ) session.merge(scheduled_job) return job.job_id
def enqueue(self, func, *args, **kwargs): """ Enqueues a function func for execution. One special parameter is track_progress. If passed in and not None, the func will be passed in a keyword parameter called update_progress: def update_progress(progress, total_progress, stage=""): The running function can call the update_progress function to notify interested parties of the function's current progress. Another special parameter is the "cancellable" keyword parameter. When passed in and not None, a special "check_for_cancel" parameter is passed in. When called, it raises an error when the user has requested a job to be cancelled. The caller can also pass in any pickleable object into the "extra_metadata" parameter. This data is stored within the job and can be retrieved when the job status is queried. All other parameters are directly passed to the function when it starts running. :type func: callable or str :param func: A callable object that will be scheduled for running. :return: a string representing the job_id. """ # if the func is already a job object, just schedule that directly. if isinstance(func, Job): job = func # else, turn it into a job first. else: job = Job(func, *args, **kwargs) job.state = State.QUEUED job_id = self.storage.enqueue_job(job, self.name) return job_id