BackgroundTask

Contains functions for running tasks in the background.

remapp.tools.background.get_current_task()
Returns

The associated BackgroundTask object when called in a task. If this is not executed in a background Task None will be returned.

remapp.tools.background.get_or_generate_task_uuid()
Returns

If called from within a task the task id, else a generated uuid

remapp.tools.background.get_queued_tasks(task_type=None) List[QueuedTask]

Returns all task which are currently waiting for execution

Parameters

task_type – Optionally filter by task type

Returns

List of queued tasks

remapp.tools.background.record_task_error_exit(error_msg)

Small helper that checks if we are in a task and assuming we are records error_msg as well as setting the completed_successfully to false and completed to True. Note that get_current_task will return None after a call to this.

remapp.tools.background.record_task_info(info_msg)

Small helper that checks if we are in a task and assuming we are records info_msg as info.

Tries to find the related DicomQRRspStudy object given a study instance uid and if this is running in a task will record it to the query object. This is used to later find the import tasks that were run as part of a query. Since this actually just takes the latest query if the user runs imports manually via script it may in principle wrongly associate.

remapp.tools.background.remove_task_from_queue(task_id: str)

Removes task from queue.

Parameters

task_id – task id of the task in question

remapp.tools.background.run_in_background(func, task_type, *args, **kwargs) Result

Syntactic sugar around run_in_background_with_limits.

Arguments correspond to run_in_background_with_limits. Will always run the passed function as fast as possible. This function should always be used for functions triggered from the webinterface.

remapp.tools.background.run_in_background_with_limits(func, task_type, num_proc, num_of_task_type, *args, **kwargs) Result

Runs func as background Process.

This method will create a new task which will be scheduled to be run by the Huey consumer with the specified priority (defaults to 0). The priority can be passed via a keyword argument

Internally the Huey consumer spawns a new process, which then creates a BackgroundTask object. This can be obtained via get_current_task() inside the calling process. Note that BackgroundTask objects will not be deleted onto completion - instead the complete flag will be set to True.

num_proc and num_of_task_type can be used to give conditions on the start.

Parameters
  • func – The function to run. Note that you should set the status of the task yourself and mark as completed when exiting yourself e.g. via sys.exit(). Assuming the function returns normally on success or returns with an exception on error, the status of the BackgroundTask object will be set correctly.

  • task_type – One of the strings declared in BackgroundTask.task_type. Indicates which kind of background process this is supposed to be. (E.g. move, query, …)

  • num_proc – Will wait with execution until there are less than num_proc active tasks. If num_proc == 0 will run directly

  • num_of_task_type – A dictionary from str to int, where key should be some used task_type. Will wait until there are less active tasks of task_type than specified in the value of the dict for that key.

  • args – Positional arguments. Passed to func.

  • kwargs – Keywords arguments. Passed to func.

Returns

The BackgroundTask object.

remapp.tools.background.terminate_background(task: BackgroundTask)

Terminate a background task by force. Sets complete=True on the task object.

remapp.tools.background.wait_task(task: Result)

Wait until the task has completed