Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def check_status(job: Union[str, Job], scheduler=None):
_scheduler = scheduler
if not scheduler:
_scheduler = django_rq.get_scheduler("default")
if not isinstance(job, Job):
try:
job = Job.fetch(job, connection=_scheduler.connection)
except NoSuchJobError:
return None
scheduled = scheduled_execution_time(job.get_id(), scheduler)
if scheduled:
return job.get_status() or "scheduled"
else:
return job.get_status() or "cancelled"
def restore(self, raw_data):
"""Overwrite properties with the provided values stored in Redis"""
obj = decode_redis_hash(raw_data)
try:
raw_data = obj['data']
except KeyError:
raise NoSuchJobError('Unexpected job format: {0}'.format(obj))
try:
self.data = zlib.decompress(raw_data)
except zlib.error:
# Fallback to uncompressed string
self.data = raw_data
self.created_at = str_to_date(obj.get('created_at'))
self.origin = as_text(obj.get('origin'))
self.description = as_text(obj.get('description'))
self.enqueued_at = str_to_date(obj.get('enqueued_at'))
self.started_at = str_to_date(obj.get('started_at'))
self.ended_at = str_to_date(obj.get('ended_at'))
self._result = unpickle(obj.get('result')) if obj.get('result') else None # noqa
self.timeout = parse_timeout(obj.get('timeout')) if obj.get('timeout') else None
self.result_ttl = int(obj.get('result_ttl')) if obj.get('result_ttl') else None # noqa
else:
ip_addresses = _get_ip_addresses_from_results(results)
try:
ip_address = ip_addresses[0]
except IndexError:
return
# get (and update) or create scan_summary
old_job = None
if ip_address.scan_summary:
scan_summary = ip_address.scan_summary
try:
old_job = rq.job.Job.fetch(
scan_summary.job_id,
django_rq.get_connection(),
)
except rq.exceptions.NoSuchJobError:
pass
else:
if 'messages' in old_job.meta and not job.meta['messages']:
job.meta['messages'] = old_job.meta['messages']
for plugin in old_job.meta.get('finished', []):
if plugin not in job.meta['finished']:
job.meta['finished'].append(plugin)
for plugin, status in old_job.meta.get('status', {}).iteritems():
if plugin not in job.meta['status']:
job.meta['status'][plugin] = status
job.save()
scan_summary.job_id = job.id
else:
scan_summary, created = ScanSummary.concurrent_get_or_create(
job_id=job.id,
)
"""
Fetch all of a job's dependencies. If a pipeline is supplied, and
watch is true, then set WATCH on all the keys of all dependencies.
Returned jobs will use self's connection, not the pipeline supplied.
"""
connection = pipeline if pipeline is not None else self.connection
if watch and self._dependency_ids:
connection.watch(*self._dependency_ids)
jobs = self.fetch_many(self._dependency_ids, connection=self.connection)
for i, job in enumerate(jobs):
if not job:
raise NoSuchJobError('Dependency {0} does not exist'.format(self._dependency_ids[i]))
return jobs
def get_job(self):
job_id = self.kwargs.get('job_id')
if not job_id:
job_id = self.get_job_id_from_address()
try:
return rq.job.Job.fetch(job_id, django_rq.get_connection())
except rq.exceptions.NoSuchJobError:
return
def requeue_ttlx2_job_view(job_id):
# Get the handle for the failed queue
fq = get_failed_queue()
# Fetch the job from the failed queue
job = fq.fetch_job(job_id)
# Test if the job exists
if job is None:
raise NoSuchJobError(
'Job {} does not exist in failed queue'.format(job_id)
)
# Remove the job from the failed queue
if fq.remove(job_id) == 0:
raise InvalidJobOperationError('Cannot requeue non-failed jobs')
# Reset the job state
job.set_status(JobStatus.QUEUED)
job.exc_info = None
if not job.timeout:
job.timeout = Queue.DEFAULT_TIMEOUT
# Double the timeout
job.timeout *= 2
# Get a handle for the original queue
q = Queue(job.origin, connection=fq.connection)
# Queue the job
q.enqueue_job(job)
def get_project_queue_status(redis, project_name):
key = _update_job_status_key(project_name)
job_id = redis.hmget(key, 'job_id')
if not job_id or not job_id[0]:
logger.info('No redis data for key: %s', key)
return None
job_id = job_id[0].decode('utf-8')
try:
job = Job.fetch(job_id, connection=redis)
except rq.exceptions.NoSuchJobError:
logger.info('No such job with id: %s', job_id)
return None
status = job.get_status()
if status == 'finished':
return {'status': status, 'ended_at': job.ended_at}
return {'status': status}
def is_running(model_name, bug_id):
# Check if there is a job
mapping_key = get_mapping_key(model_name, bug_id)
job_id = redis_conn.get(mapping_key)
if not job_id:
LOGGER.debug("No job ID mapping %s, False", job_id)
return False
try:
job = Job.fetch(job_id.decode("utf-8"), connection=redis_conn)
except NoSuchJobError:
LOGGER.debug("No job in DB for %s, False", job_id)
# The job might have expired from redis
return False
job_status = job.get_status()
if job_status == "started":
LOGGER.debug("Job running %s, True", job_id)
return True
# Enforce job timeout as RQ doesn't seems to do it https://github.com/rq/rq/issues/758
timeout_datetime = job.enqueued_at + timedelta(seconds=job.timeout)
utcnow = datetime.utcnow()
if timeout_datetime < utcnow:
# Remove the timeouted job so it will be requeued
job.cancel()
job.cleanup()
def rq_job(self):
"""The last RQ Job this ran on"""
if not self.rq_id or not self.rq_origin:
return
try:
return RQJob.fetch(self.rq_id, connection=get_connection(self.rq_origin))
except NoSuchJobError:
return