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 test_can_get_first_job_queued(self, defaultbackend): job1 = Job(open) job2 = Job(open) job1_id = defaultbackend.enqueue_job(job1, QUEUE) defaultbackend.enqueue_job(job2, QUEUE) assert defaultbackend.get_next_queued_job([QUEUE]).job_id == job1_id
def test_task_cancel(self, get_queue_mock): get_queue_mock.return_value.fetch_job.return_value = Job( state=State.CANCELED, func=lambda: None) response = self.client.post(reverse("kolibri:core:task-canceltask"), {"task_id": "1"}, format="json") self.assertEqual(response.data, {})
def test_enqueue_job_writes_to_storage_on_success(self, worker, mocker): mocker.spy(worker.storage, "complete_job") # this job should never fail. job = Job(id, 9) worker.storage.enqueue_job(job, QUEUE) while job.state == State.QUEUED: job = worker.storage.get_job(job.job_id) time.sleep(0.5) try: # Get the future, or pass if it has already been cleaned up. future = worker.future_job_mapping[job.job_id] future.result() except KeyError: pass # verify that we sent a message through our backend assert worker.storage.complete_job.call_count == 1 call_args = worker.storage.complete_job.call_args job_id = call_args[0][0] # verify that we're setting the correct job_id assert job_id == 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
def test_enqueue_job_runs_job(self, worker): job = Job(id, 9) worker.storage.enqueue_job(job, QUEUE) while job.state == State.QUEUED: job = worker.storage.get_job(job.job_id) time.sleep(0.5) try: # Get the future, or pass if it has already been cleaned up. future = worker.future_job_mapping[job.job_id] future.result() except KeyError: pass assert job.state == State.COMPLETED
def simplejob(): return Job("builtins.id")
def simplejob(): return Job(id)