Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
"""
Append and save a non-fatal error for the currently executing task.
Fatal errors should not use this. Instead they should raise an Exception,
preferably one that inherits from :class: `pulpcore.server.exception.PulpException`.
This is saved in a structured way to the :attr: `~pulpcore.app.models.Task.non_fatal_errors`
attribute on the :class: `~pulpcore.app.models.Task` model.
Args:
error (Exception): The non fatal error to be appended.
Raises:
pulpcore.app.models.Task.DoesNotExist: If not currently running inside a task.
"""
task = models.Task.objects.get(id=self.job.id)
serialized_error = exception_to_dict(error)
task.non_fatal_errors.append(serialized_error)
task.save()
find_worker function.
Args:
func (basestring): The function to be called
inner_task_id (basestring): The UUID to be set on the task being called. By providing
the UUID, the caller can have an asynchronous reference to the inner task
that will be dispatched.
resources (basestring): The urls of the resource you wish to reserve for your task.
The system will ensure that no other tasks that want that same reservation will run
concurrently with yours.
inner_args (tuple): The positional arguments to pass on to the task.
inner_kwargs (dict): The keyword arguments to pass on to the task.
options (dict): For all options accepted by enqueue see the RQ docs
"""
redis_conn = connection.get_redis_connection()
task_status = Task.objects.get(pk=inner_task_id)
task_name = func.__module__ + '.' + func.__name__
while True:
if task_name == "pulpcore.app.tasks.orphan.orphan_cleanup":
if ReservedResource.objects.exists():
# wait until there are no reservations
time.sleep(0.25)
continue
else:
task_status.state = TASK_STATES.RUNNING
task_status.save()
q = Queue('resource_manager', connection=redis_conn, is_async=False)
q.enqueue(func, args=inner_args, kwargs=inner_kwargs, job_id=inner_task_id,
timeout=TASK_TIMEOUT, **options)
task_status.state = TASK_STATES.COMPLETED
task_status.save()
return
"""
Append and save a non-fatal error for the currently executing task.
Fatal errors should not use this. Instead they should raise an Exception,
preferably one that inherits from :class: `pulpcore.server.exception.PulpException`.
This is saved in a structured way to the :attr: `~pulpcore.app.models.Task.non_fatal_errors`
attribute on the :class: `~pulpcore.app.models.Task` model.
Args:
error (Exception): The non fatal error to be appended.
Raises:
pulpcore.app.models.Task.DoesNotExist: If not currently running inside a task.
"""
task = models.Task.objects.get(id=self.job.id)
serialized_error = exception_to_dict(error)
task.non_fatal_errors.append(serialized_error)
task.save()
def handle_job_failure(self, job, **kwargs):
"""
Set the :class:`pulpcore.app.models.Task` to failed and record the exception.
This method is called by rq to handle a job failure.
Args:
job (rq.job.Job): The job that experienced the failure
kwargs (dict): Unused parameters
"""
try:
task = Task.objects.get(job_id=job.get_id())
except Task.DoesNotExist:
pass
else:
exc_type, exc, tb = sys.exc_info()
task.set_failed(exc, tb)
return super().handle_job_failure(job, **kwargs)
"""
try:
task = Task.objects.get(pk=task_id, state=TASK_STATES.RUNNING)
except Task.DoesNotExist:
pass
else:
msg = _(
"The task {task_id} exited immediately for some reason. Marking as "
"failed. Check the logs for more details"
)
_logger.error(msg.format(task_id=task.pk))
exc = RuntimeError(msg.format(task_id=task.pk))
task.set_failed(exc, None)
Task.objects.get(pk=task_id).release_resources()
def handle_job_success(self, job, queue, started_job_registry):
"""
Set the :class:`pulpcore.app.models.Task` to completed.
This method is called by rq to handle a job success.
Args:
job (rq.job.Job): The job that experienced the success
queue (rq.queue.Queue): The Queue associated with the job
started_job_registry (rq.registry.StartedJobRegistry): The RQ registry of started jobs
"""
try:
task = Task.objects.get(pk=job.get_id())
except Task.DoesNotExist:
pass
else:
task.set_completed()
return super().handle_job_success(job, queue, started_job_registry)
def perform_job(self, job, queue):
"""
Set the :class:`pulpcore.app.models.Task` to running and install a kill monitor Thread
This method is called by the worker's work horse thread (the forked child) just before the
task begins executing. It creates a Thread which monitors a special Redis key which if
created should kill the task with SIGKILL.
Args:
job (rq.job.Job): The job to perform
queue (rq.queue.Queue): The Queue associated with the job
"""
try:
task = Task.objects.get(pk=job.get_id())
except Task.DoesNotExist:
pass
else:
task.set_running()
def check_kill(conn, id, interval=1):
while True:
res = conn.srem(TASKING_CONSTANTS.KILL_KEY, id)
if res > 0:
os.kill(os.getpid(), signal.SIGKILL)
time.sleep(interval)
t = threading.Thread(target=check_kill, args=(self.connection, job.get_id()))
t.start()
return super().perform_job(job, queue)
def handle_job_failure(self, job, **kwargs):
"""
Set the :class:`pulpcore.app.models.Task` to failed and record the exception.
This method is called by rq to handle a job failure.
Args:
job (rq.job.Job): The job that experienced the failure
kwargs (dict): Unused parameters
"""
try:
task = Task.objects.get(pk=job.get_id())
except Task.DoesNotExist:
pass
else:
exc_type, exc, tb = sys.exc_info()
task.set_failed(exc, tb)
return super().handle_job_failure(job, **kwargs)
Args:
task_id (basestring): The UUID of the task that requested the reservation
"""
try:
task = Task.objects.get(pk=task_id, state=TASK_STATES.RUNNING)
except Task.DoesNotExist:
pass
else:
msg = _('The task {task_id} exited immediately for some reason. Marking as '
'failed. Check the logs for more details')
_logger.error(msg.format(task_id=task_id))
exc = RuntimeError(msg.format(task_id=task_id))
task.set_failed(exc, None)
Task.objects.get(pk=task_id).release_resources()