Define a Job to Run on a Background Worker Process.
Define a Job to Run on a Background Worker Process.
Active bindings
expr
Get the expression that will be run by this Job.
vars
Get or set the variables that will be placed into the expression's environment before evaluation.
reformat
Get or set the
function (job, output)
for transforming rawcallr
output to the Job's result.cpus
Get or set the number of CPUs to reserve for evaluating
expr
.tmax
Get or set the time limits to apply to this Job.
proxy
Get or set the Job to proxy in place of running
expr
.state
Get or set the Job's state (setting will trigger callbacks).
output
Get or set the Job's raw
callr
output (setting will change the Job's state to 'done').result
Get the result of
expr
. Will block until Job is finished.hooks
Get all currently registered callback hooks - a named list of functions.
is_done
Returns TRUE or FALSE depending on if the Job's result is ready.
uid
Returns a short string, e.g. 'J16', that uniquely identifies this Job.
Methods
Method new()
Creates a Job object defining how to run an expression on a background worker process.
Usage
Job$new(
expr,
vars = NULL,
tmax = NULL,
hooks = NULL,
reformat = TRUE,
cpus = 1L
)
Arguments
expr
A call or R expression wrapped in curly braces to evaluate on a worker. Will have access to any variables defined by
vars
, as well as the Worker'sglobals
,packages
, andinit
configuration.vars
A list of named variables to make available to
expr
during evaluation.tmax
A named numeric vector indicating the maximum number of seconds allowed for each state the job passes through, or 'total' to apply a single timeout from 'submitted' to 'done'. Example:
tmax = c(total = 2.5, running = 1)
will force-stop a job 2.5 seconds after it is submitted, and also limits its time in the running state to just 1 second.hooks
A list of functions to run when the Job state changes, of the form
hooks = list(created = function (job) {...}, done = ~{...})
. The names of these functions should becreated
,submitted
,queued
,dispatched
,starting
,running
,done
, or'*'
.'*'
will be run every time the state changes, whereas the others will only be run when the Job enters that state. Duplicate names are allowed.reformat
The underlying call to
callr::r_session$call()
returns information on stdout, stderr, etc. Whenreformat=TRUE
(the default), only the result of the expression is returned. Setreformat=FALSE
to return the entire callr output, orreformat=function(job,output)
to use a function of your own to post-process the output from callr.cpus
How many CPU cores to reserve for this Job. The Queue uses this number to limit the number of simultaneously running Jobs; it does not prevent a Job from using more CPUs than reserved.
Method print()
Print method for a Job.
Method on()
Attach a callback function.
Arguments
state
The Job state that will trigger this function. Typically one of:
'*'
-Every time the state changes.
'.next'
-Only one time, the next time the state changes.
'created'
-After
Job$new()
initialization.'submitted'
-Before
stop_id
andcopy_id
are resolved.'queued'
-After
<Job>$queue
is assigned.'dispatched'
-After
<Job>$worker
is assigned.'starting'
-Before evaluation begins.
'running'
-After evaluation begins.
'done'
-After
<Job>$output
is assigned.
func
A function that accepts a Job object as input. You can call
<Job>$stop()
or edit its values and the changes will be persisted (since Jobs are reference class objects). You can also edit/stop other queued jobs by modifying the Jobs in<Job>$queue$jobs
. Return value is ignored.
Method stop()
Stop this Job. If the Job is running, the worker process will be rebooted.