Skip to contents

The Job object encapsulates an expression and its evaluation parameters. It also provides a way to check for and retrieve the result.

Active bindings

expr

R expression that will be run by this Job.

vars

Get or set - List of variables that will be placed into the expression's environment before evaluation.

reformat

Get or set - function (job) for defining <Job>$result.

signal

Get or set - Conditions to signal.

cpus

Get or set - Number of CPUs to reserve for evaluating expr.

timeout

Get or set - Time limits to apply to this Job.

proxy

Get or set - Job to proxy in place of running expr.

state

Get or set - The Job's state: 'created', 'submitted', 'queued', 'dispatched', 'starting', 'running', or 'done'. Assigning to <Job>$state will trigger callback hooks.

output

Get or set - Job's raw output. Assigning to <Job>$output will change the Job's state to 'done'.

result

Result of expr. Will block until Job is finished.

hooks

Currently registered callback hooks as a named list of functions. Set new hooks with <Job>$on().

is_done

TRUE or FALSE depending on if the Job's result is ready.

uid

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.

Typically you won't need to call Job$new(). Instead, create a Queue and use <Queue>$run() to generate Job objects.

Usage

Job$new(
  expr,
  vars = NULL,
  timeout = NULL,
  hooks = NULL,
  reformat = NULL,
  signal = FALSE,
  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's globals, packages, and init configuration. See vignette('eval').

vars

A named list of variables to make available to expr during evaluation. Alternatively, an object that can be coerced to a named list with as.list(), e.g. named vector, data.frame, or environment.

timeout

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: timeout = c(total = 2.5, running = 1). See vignette('stops').

hooks

A named list of functions to run when the Job state changes, of the form hooks = list(created = function (worker) {...}). Names of worker hooks are typically 'created', 'submitted', 'queued', 'dispatched', 'starting', 'running', 'done', or '*' (duplicates okay). See vignette('hooks').

reformat

Set reformat = function (job) to define what <Job>$result should return. The default, reformat = NULL passes <Job>$output to <Job>$result unchanged. See vignette('results').

signal

Should calling <Job>$result signal on condition objects? When FALSE, <Job>$result will return the object without taking additional action. Setting to TRUE or a character vector of condition classes, e.g. c('interrupt', 'error', 'warning'), will cause the equivalent of stop(<condition>) to be called when those conditions are produced. See vignette('results').

cpus

How many CPU cores to reserve for this Job. Used to limit the number of Jobs running simultaneously to respect <Queue>$max_cpus. Does not prevent a Job from using more CPUs than reserved.

...

Arbitrary named values to add to the returned Job object.

Returns

A Job object.


Method print()

Print method for a Job.

Usage

Job$print(...)

Arguments

...

Arguments are not used currently.

Returns

This Job, invisibly.


Method on()

Attach a callback function to execute when the Job enters state.

Usage

Job$on(state, func)

Arguments

state

The name of a Job state. Typically one of:

  • '*' - Every time the state changes.

  • '.next' - Only one time, the next time the state changes.

  • 'created' - After Job$new() initialization.

  • 'submitted' - After <Job>$queue is assigned.

  • 'queued' - After stop_id and copy_id are resolved.

  • 'dispatched' - After <Job>$worker is assigned.

  • 'starting' - Before evaluation begins.

  • 'running' - After evaluation begins.

  • 'done' - After <Job>$output is assigned.

Custom states can also be specified.

func

A function that accepts a Job object as input. You can call <Job>$stop() or edit <Job>$ 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.

Returns

A function that when called removes this callback from the Job.


Method wait()

Blocks until the Job enters the given state.

Usage

Job$wait(state = "done")

Arguments

state

The name of a Job state. Typically one of:

  • '*' - Every time the state changes.

  • '.next' - Only one time, the next time the state changes.

  • 'created' - After Job$new() initialization.

  • 'submitted' - After <Job>$queue is assigned.

  • 'queued' - After stop_id and copy_id are resolved.

  • 'dispatched' - After <Job>$worker is assigned.

  • 'starting' - Before evaluation begins.

  • 'running' - After evaluation begins.

  • 'done' - After <Job>$output is assigned.

Custom states can also be specified.

Returns

This Job, invisibly.


Method stop()

Stop this Job. If the Job is running, its Worker will be restarted.

Usage

Job$stop(reason = "job stopped by user", cls = NULL)

Arguments

reason

A message to include in the 'interrupt' condition object that will be returned as the Job's result.

cls

Character vector of additional classes to prepend to c('interrupt', 'condition').

Returns

This Job, invisibly.