Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def test_get_job_nonexistent_job(self, scheduler):
scheduler._lookup_job = MagicMock(side_effect=JobLookupError('foo'))
assert scheduler.get_job('foo') is None
"""
if self.state == STATE_STOPPED:
# Check if the job is among the pending jobs
for job, alias, replace_existing in self._pending_jobs:
if job.id == job_id:
return job, None
else:
# Look in all job stores
for alias, store in self._jobstores.items():
if jobstore_alias in (None, alias):
job = store.lookup_job(job_id)
if job is not None:
return job, alias
raise JobLookupError(job_id)
def update_job(self, job):
self._ensure_paths()
node_path = os.path.join(self.path, str(job.id))
changes = {
'next_run_time': datetime_to_utc_timestamp(job.next_run_time),
'job_state': job.__getstate__()
}
data = pickle.dumps(changes, self.pickle_protocol)
try:
self.client.set(node_path, value=data)
except NoNodeError:
raise JobLookupError(job.id)
def stop_job(resource_id):
LOGGER.info('Stopping job for resource=%d' % resource_id)
# Try to remove job from scheduler
try:
scheduler.remove_job(str(resource_id))
except JobLookupError:
pass
jobstore_alias = alias
break
else:
# Otherwise, try to remove it from each store until it succeeds or we run out of
# stores to check
for alias, store in self._jobstores.items():
if jobstore in (None, alias):
try:
store.remove_job(job_id)
jobstore_alias = alias
break
except JobLookupError:
continue
if jobstore_alias is None:
raise JobLookupError(job_id)
# Notify listeners that a job has been removed
event = JobEvent(EVENT_JOB_REMOVED, job_id, jobstore_alias)
self._dispatch_event(event)
self._logger.info('Removed job %s', job_id)
def update_job(job_id):
"""Updates a job."""
data = request.get_json(force=True)
try:
current_app.apscheduler.modify_job(job_id, **data)
job = current_app.apscheduler.get_job(job_id)
return jsonify(job)
except JobLookupError:
return jsonify(dict(error_message='Job %s not found' % job_id), status=404)
except Exception as e:
return jsonify(dict(error_message=str(e)), status=500)
def delete(self, *args, **kwargs):
job_id = kwargs['job_id']
logger.info("job-id>>>{0}".format(job_id))
try:
SCHEDULER.remove_job(job_id)
except JobLookupError, e:
logger.info("job %s has been removeds".format(job_id))
self.set_status(200, 'job has been removed')
self.finish({'messege': 'job has been removed'})
except:
logger.error(traceback.format_exc())
self.set_status(500, 'failed')
self.finish({'messege': 'failed'})
self.finish({"message": 'remove job success'})
def update_job(self, job):
old_job, old_timestamp = self._jobs_index.get(job.id, (None, None))
if old_job is None:
raise JobLookupError(job.id)
# If the next run time has not changed, simply replace the job in its present index.
# Otherwise, reinsert the job to the list to preserve the ordering.
old_index = self._get_job_index(old_timestamp, old_job.id)
new_timestamp = datetime_to_utc_timestamp(job.next_run_time)
if old_timestamp == new_timestamp:
self._jobs[old_index] = (job, new_timestamp)
else:
del self._jobs[old_index]
new_index = self._get_job_index(new_timestamp, job.id)
self._jobs.insert(new_index, (job, new_timestamp))
self._jobs_index[old_job.id] = (job, new_timestamp)