Skip to main content

Overview

The Job class represents a single unit of work in a workflow. It wraps an action (either an ActionInterface, a class string, or a Closure) and manages its arguments, dependencies, execution conditions, and retry policy.
Jobs should be created using the sync() or async() helper functions rather than instantiating the Job class directly.

Class signature

final class Job implements JobInterface
Implements: Chevere\Workflow\Interfaces\JobInterface Location: src/Job.php:39

Constructor

public function __construct(
    ActionInterface|string|callable $_,
    mixed ...$argument
)
Internal method - Do not use this constructor directly. Use sync() or async() functions instead.
$_
ActionInterface|class-string<ActionInterface>|callable
required
The action to run. Can be an action instance, an action class string, or a callable.
...$argument
mixed
Action arguments for its run method. Can be raw values, response references, or variables.

Public methods

parameters()

Provides access to the invocable action parameters for this job.
public function parameters(): ParametersInterface
return
ParametersInterface
The parameters interface containing all parameter definitions for the job’s action.
Location: src/Job.php:142

return()

Provides access to the return type parameter for this job.
public function return(): ParameterInterface
return
ParameterInterface
The parameter interface describing the job’s return type.
Location: src/Job.php:147

action()

Returns the action associated with this job.
public function action(): ActionInterface|string|Closure
return
ActionInterface|string|Closure
The action to be executed by this job.
Location: src/Job.php:208

arguments()

Returns the arguments that will be passed to the action.
public function arguments(): array
return
array<string, mixed>
An associative array of argument names to values.
Location: src/Job.php:213

dependencies()

Returns the job dependencies.
public function dependencies(): VectorInterface
return
VectorInterface<string>
A vector containing the names of jobs that this job depends on.
Location: src/Job.php:218

runIf()

Returns the run-if conditions for this job.
public function runIf(): VectorInterface
return
VectorInterface<ResponseReferenceInterface|VariableInterface|callable|bool>
A vector of conditions that must be truthy for the job to execute.
Location: src/Job.php:223

runIfNot()

Returns the run-if-not conditions for this job.
public function runIfNot(): VectorInterface
return
VectorInterface<ResponseReferenceInterface|VariableInterface|callable|bool>
A vector of conditions that must be falsy for the job to execute.
Location: src/Job.php:228

isSync()

Determines if the job is synchronous (blocking).
public function isSync(): bool
return
bool
Returns true if the job is synchronous, false if asynchronous.
Location: src/Job.php:233

caller()

Provides access to the caller who created this job.
public function caller(): CallerInterface
return
CallerInterface
Information about where the job was created (file and line number).
Location: src/Job.php:137

retryPolicy()

Provides access to the job retry policy.
public function retryPolicy(): RetryPolicyInterface
return
RetryPolicyInterface
The retry policy configuration for this job.
Location: src/Job.php:152

withArguments()

Returns an instance with the specified arguments.
public function withArguments(mixed ...$argument): JobInterface
...$argument
mixed
Arguments to set for the job. Can be raw values, response references, or variables.
return
JobInterface
A new instance with the specified arguments. The current instance is not modified.
Location: src/Job.php:157 Example:
$job = sync(new MyAction(), name: 'John');
$updatedJob = $job->withArguments(name: 'Jane', age: 30);

withRunIf()

Returns an instance with the specified run-if condition.
public function withRunIf(
    ResponseReferenceInterface|VariableInterface|callable|bool ...$context
): JobInterface
...$context
ResponseReferenceInterface|VariableInterface|callable|bool
Conditions that must be truthy for the job to execute. Multiple conditions are combined with AND logic.
return
JobInterface
A new instance with the specified run-if conditions. The current instance is not modified.
Location: src/Job.php:165 Example:
$job = sync(new ProcessAction())
    ->withRunIf(response('validate', 'isValid'));

withRunIfNot()

Returns an instance with the specified run-if-not condition.
public function withRunIfNot(
    ResponseReferenceInterface|VariableInterface|callable|bool ...$context
): JobInterface
...$context
ResponseReferenceInterface|VariableInterface|callable|bool
Conditions that must be falsy for the job to execute. Multiple conditions are combined with AND logic.
return
JobInterface
A new instance with the specified run-if-not conditions. The current instance is not modified.
Location: src/Job.php:173 Example:
$job = sync(new FallbackAction())
    ->withRunIfNot(response('validate', 'isValid'));

withIsSync()

Returns an instance with the specified sync flag.
public function withIsSync(bool $flag = true): JobInterface
$flag
bool
default:"true"
Set to true for synchronous execution, false for asynchronous.
return
JobInterface
A new instance with the specified sync flag. The current instance is not modified.
Location: src/Job.php:181 Example:
$asyncJob = async(new MyAction());
$syncJob = $asyncJob->withIsSync(true); // Convert to sync

withDepends()

Returns an instance with the specified job dependencies.
public function withDepends(string ...$jobs): JobInterface
...$jobs
string
Names of jobs that this job depends on. The job will not execute until all dependencies complete.
return
JobInterface
A new instance with the specified dependencies. The current instance is not modified.
Location: src/Job.php:189 Example:
$job = sync(new FinalizeAction())
    ->withDepends('validate', 'process', 'transform');

withRetry()

Returns an instance with the specified retry policy.
public function withRetry(
    int $timeout = 0,
    int $maxAttempts = 1,
    int $delay = 0
): JobInterface
$timeout
int<0, max>
default:"0"
Timeout in seconds across all attempts. Use 0 for unlimited.
$maxAttempts
int<1, max>
default:"1"
Number of execution attempts (minimum 1).
$delay
int<0, max>
default:"0"
Retry delay in seconds between attempts. Use 0 for no delay.
return
JobInterface
A new instance with the specified retry policy. The current instance is not modified.
Location: src/Job.php:197 Example:
$job = sync(new ApiCallAction())
    ->withRetry(
        timeout: 30,      // 30 seconds total timeout
        maxAttempts: 3,   // Try up to 3 times
        delay: 5          // Wait 5 seconds between retries
    );

Usage example

use function Chevere\Workflow\sync;
use function Chevere\Workflow\response;
use function Chevere\Workflow\variable;

// Create a job using the sync() helper
$validateJob = sync(
    new ValidateAction(),
    data: variable('inputData')
)->withRetry(
    timeout: 10,
    maxAttempts: 2
);

// Create a dependent job with conditional execution
$processJob = sync(
    new ProcessAction(),
    validatedData: response('validate', 'data')
)
    ->withDepends('validate')
    ->withRunIf(response('validate', 'isValid'));

See also

Build docs developers (and LLMs) love