Documentation

Job
in package
uses IntervalTrait

Class Job

Table of Contents

Properties

$chainedJobs  : array<string|int, mixed>
$dependencies  : array<string|int, mixed>
$executionDuration  : float
$executionStartTime  : float
$maxAttempts  : int
$metadata  : array<string|int, mixed>
$onFailure  : callable|null
$onRetry  : callable|null
$onSuccess  : callable|null
$priority  : string
$queueId  : string|null
$retryCount  : int
$retryDelay  : int
$retryStrategy  : string
$tags  : array<string|int, mixed>
$timeout  : int
$after  : callable|null
$args  : array<string|int, mixed>
$at  : string
$backlink  : string|null
$before  : callable|null
$command  : callable|string
$creationTime  : DateTime
$emailConfig  : array<string|int, mixed>
$emailTo  : array<string|int, mixed>
$enabled  : bool
$executionTime  : CronExpression
$id  : string
$lockFile  : string
$output  : string
$outputMode  : string
$outputTo  : array<string|int, mixed>
$process  : Process|null
$returnCode  : int
$runInBackground  : bool
$successful  : bool
$tempDir  : string
$truthTest  : bool
$whenOverlapping  : callable

Methods

__construct()  : mixed
Create a new Job instance.
april()  : self
Set the execution time to every April.
at()  : self
Set the Job execution time.
august()  : self
Set the execution time to every August.
backlink()  : string|null
Sets/Gets an option backlink
before()  : self
Set function to be called before job execution Job object is injected as a parameter to callable function.
chain()  : self
Chain another job to run after this one
configure()  : self
Configure the job.
daily()  : self
Set the execution time to once a day.
december()  : self
Set the execution time to every December.
dependsOn()  : self
Add job dependency
email()  : self
Set the emails where the output should be sent to.
everyMinute()  : self
Set the execution time to every minute.
february()  : self
Set the execution time to every February.
finalize()  : void
Finish up processing the job
friday()  : self
Set the execution time to every Friday.
fromArray()  : self
Create job from array
getArguments()  : string|null
Get optional arguments
getAt()  : string
Get the cron 'at' syntax for this job
getCommand()  : Closure|string
Get the command
getCronExpression()  : CronExpression
getEnabled()  : bool
Get the status of this job
getExecutionTime()  : float
Get execution time in seconds
getId()  : string
Get the Job id.
getMaxAttempts()  : int
Get maximum retry attempts
getMetadata()  : mixed
Get job metadata
getOutput()  : mixed
Get the job output.
getPriority()  : string
Get job priority
getProcess()  : Process|null
Get process (for background jobs)
getQueueId()  : string|null
Get queue ID
getRawArguments()  : array<string|int, mixed>|string
Get raw arguments (array or string)
getRetryCount()  : int
Get current retry count
getTags()  : array<string|int, mixed>
Get job tags
hasTag()  : bool
Check if job has a specific tag
hourly()  : self
Set the execution time to every hour.
inForeground()  : $this
Force the Job to run in foreground.
isDue()  : bool
Check if the Job is due to run.
isOverlapping()  : bool
Check if the Job is overlapping.
isSuccessful()  : bool
Get the status of the last run for this job
january()  : self
Set the execution time to every January.
july()  : self
Set the execution time to every July.
june()  : self
Set the execution time to every June.
march()  : self
Set the execution time to every March.
maxAttempts()  : self
Set maximum retry attempts
may()  : self
Set the execution time to every May.
monday()  : self
Set the execution time to every Monday.
monthly()  : self
Set the execution time to once a month.
november()  : self
Set the execution time to every November.
october()  : self
Set the execution time to every October.
onFailure()  : self
Set failure callback
onlyOne()  : self
This will prevent the Job from overlapping.
onRetry()  : self
Set retry callback
onSuccess()  : self
Set success callback
output()  : self
Set the file/s where to write the output of the job.
priority()  : self
Set job priority
retryDelay()  : self
Set retry delay
run()  : bool
Run the job.
runInBackground()  : bool
Check if the Job can run in background.
runWithRetry()  : bool
Run the job with retry support
saturday()  : self
Set the execution time to every Saturday.
september()  : self
Set the execution time to every September.
setQueueId()  : self
Set queue ID
sunday()  : self
Set the execution time to every Sunday.
then()  : self
Set a function to be called after job execution.
thursday()  : self
Set the execution time to every Thursday.
timeout()  : self
Set job timeout
toArray()  : array<string|int, mixed>
Convert job to array for serialization
tuesday()  : self
Set the execution time to every Tuesday.
wednesday()  : self
Set the execution time to every Wednesday.
weekly()  : self
Set the execution time to once a week.
when()  : self
Truth test to define if the job should run if due.
withMetadata()  : self
Add metadata to the job
withTags()  : self
Add tags to the job
calculateRetryDelay()  : int
Calculate retry delay based on strategy
checkDependencies()  : bool
Check if dependencies are met
runChainedJobs()  : void
Run chained jobs
createLockFile()  : void
Create the job lock file.
emailOutput()  : bool
Email the output of the job, if any.
exec()  : string
Execute a callable job.
postRun()  : void
Things to run after job has run
removeLockFile()  : void
Remove the job lock file.
validateCronRange()  : mixed
Validate sequence of cron expression.
validateCronSequence()  : array<string|int, mixed>
Validate sequence of cron expression.

Properties

$chainedJobs

protected array<string|int, mixed> $chainedJobs = []

$dependencies

protected array<string|int, mixed> $dependencies = []

$executionDuration

protected float $executionDuration = 0

$executionStartTime

protected float $executionStartTime

$maxAttempts

protected int $maxAttempts = 3

$metadata

protected array<string|int, mixed> $metadata = []

$onFailure

protected callable|null $onFailure

$onRetry

protected callable|null $onRetry

$onSuccess

protected callable|null $onSuccess

$priority

protected string $priority = 'normal'

$queueId

protected string|null $queueId

$retryCount

protected int $retryCount = 0

$retryDelay

protected int $retryDelay = 60

$retryStrategy

protected string $retryStrategy = 'exponential'

$tags

protected array<string|int, mixed> $tags = []

$timeout

protected int $timeout = 300

$after

private callable|null $after

$args

private array<string|int, mixed> $args = []

$at

private string $at
private string|null $backlink

$before

private callable|null $before

$command

private callable|string $command

$creationTime

private DateTime $creationTime

$emailConfig

private array<string|int, mixed> $emailConfig = []

$emailTo

private array<string|int, mixed> $emailTo = []

$enabled

private bool $enabled

$executionTime

private CronExpression $executionTime

$id

private string $id

$lockFile

private string $lockFile

$output

private string $output

$outputMode

private string $outputMode

$outputTo

private array<string|int, mixed> $outputTo = []

$process

private Process|null $process

$returnCode

private int $returnCode = 0

$runInBackground

private bool $runInBackground = true

$successful

private bool $successful = false

$tempDir

private string $tempDir

$truthTest

private bool $truthTest = true

$whenOverlapping

private callable $whenOverlapping

Methods

__construct()

Create a new Job instance.

public __construct(string|callable $command[, array<string|int, mixed> $args = [] ][, string|null $id = null ]) : mixed
Parameters
$command : string|callable
$args : array<string|int, mixed> = []
$id : string|null = null

april()

Set the execution time to every April.

public april([int|string $day = 1 ][, int|string $hour = 0 ][, int|string $minute = 0 ]) : self
Parameters
$day : int|string = 1
$hour : int|string = 0
$minute : int|string = 0
Return values
self

at()

Set the Job execution time.

public at(string $expression) : self

compo

Parameters
$expression : string
Return values
self

august()

Set the execution time to every August.

public august([int|string $day = 1 ][, int|string $hour = 0 ][, int|string $minute = 0 ]) : self
Parameters
$day : int|string = 1
$hour : int|string = 0
$minute : int|string = 0
Return values
self

Sets/Gets an option backlink

public backlink([string|null $link = null ]) : string|null
Parameters
$link : string|null = null
Return values
string|null

before()

Set function to be called before job execution Job object is injected as a parameter to callable function.

public before(callable $fn) : self
Parameters
$fn : callable
Return values
self

chain()

Chain another job to run after this one

public chain(Job $job[, bool $onlyOnSuccess = true ]) : self
Parameters
$job : Job
$onlyOnSuccess : bool = true

Run only if current job succeeds

Return values
self

configure()

Configure the job.

public configure([array<string|int, mixed> $config = [] ]) : self
Parameters
$config : array<string|int, mixed> = []
Return values
self

daily()

Set the execution time to once a day.

public daily([int|string $hour = 0 ][, int|string $minute = 0 ]) : self
Parameters
$hour : int|string = 0
$minute : int|string = 0
Return values
self

december()

Set the execution time to every December.

public december([int|string $day = 1 ][, int|string $hour = 0 ][, int|string $minute = 0 ]) : self
Parameters
$day : int|string = 1
$hour : int|string = 0
$minute : int|string = 0
Return values
self

dependsOn()

Add job dependency

public dependsOn(string $jobId) : self
Parameters
$jobId : string
Return values
self

email()

Set the emails where the output should be sent to.

public email(string|array<string|int, mixed> $email) : self

The Job should be set to write output to a file for this to work.

Parameters
$email : string|array<string|int, mixed>
Return values
self

everyMinute()

Set the execution time to every minute.

public everyMinute() : self
Return values
self

february()

Set the execution time to every February.

public february([int|string $day = 1 ][, int|string $hour = 0 ][, int|string $minute = 0 ]) : self
Parameters
$day : int|string = 1
$hour : int|string = 0
$minute : int|string = 0
Return values
self

finalize()

Finish up processing the job

public finalize() : void

friday()

Set the execution time to every Friday.

public friday([int|string $hour = 0 ][, int|string $minute = 0 ]) : self
Parameters
$hour : int|string = 0
$minute : int|string = 0
Return values
self

fromArray()

Create job from array

public static fromArray(array<string|int, mixed> $data) : self
Parameters
$data : array<string|int, mixed>
Return values
self

getArguments()

Get optional arguments

public getArguments() : string|null
Return values
string|null

getAt()

Get the cron 'at' syntax for this job

public getAt() : string
Return values
string

getCommand()

Get the command

public getCommand() : Closure|string
Return values
Closure|string

getCronExpression()

public getCronExpression() : CronExpression
Return values
CronExpression

getEnabled()

Get the status of this job

public getEnabled() : bool
Return values
bool

getExecutionTime()

Get execution time in seconds

public getExecutionTime() : float
Return values
float

getId()

Get the Job id.

public getId() : string
Return values
string

getMaxAttempts()

Get maximum retry attempts

public getMaxAttempts() : int
Return values
int

getMetadata()

Get job metadata

public getMetadata([string|null $key = null ]) : mixed
Parameters
$key : string|null = null

getOutput()

Get the job output.

public getOutput() : mixed

getPriority()

Get job priority

public getPriority() : string
Return values
string

getProcess()

Get process (for background jobs)

public getProcess() : Process|null
Return values
Process|null

getQueueId()

Get queue ID

public getQueueId() : string|null
Return values
string|null

getRawArguments()

Get raw arguments (array or string)

public getRawArguments() : array<string|int, mixed>|string
Return values
array<string|int, mixed>|string

getRetryCount()

Get current retry count

public getRetryCount() : int
Return values
int

getTags()

Get job tags

public getTags() : array<string|int, mixed>
Return values
array<string|int, mixed>

hasTag()

Check if job has a specific tag

public hasTag(string $tag) : bool
Parameters
$tag : string
Return values
bool

hourly()

Set the execution time to every hour.

public hourly([int|string $minute = 0 ]) : self
Parameters
$minute : int|string = 0
Return values
self

inForeground()

Force the Job to run in foreground.

public inForeground() : $this
Return values
$this

isDue()

Check if the Job is due to run.

public isDue([DateTime|null $date = null ]) : bool

It accepts as input a DateTime used to check if the job is due. Defaults to job creation time. It also default the execution time if not previously defined.

Parameters
$date : DateTime|null = null
Return values
bool

isOverlapping()

Check if the Job is overlapping.

public isOverlapping() : bool
Return values
bool

isSuccessful()

Get the status of the last run for this job

public isSuccessful() : bool
Return values
bool

january()

Set the execution time to every January.

public january([int|string $day = 1 ][, int|string $hour = 0 ][, int|string $minute = 0 ]) : self
Parameters
$day : int|string = 1
$hour : int|string = 0
$minute : int|string = 0
Return values
self

july()

Set the execution time to every July.

public july([int|string $day = 1 ][, int|string $hour = 0 ][, int|string $minute = 0 ]) : self
Parameters
$day : int|string = 1
$hour : int|string = 0
$minute : int|string = 0
Return values
self

june()

Set the execution time to every June.

public june([int|string $day = 1 ][, int|string $hour = 0 ][, int|string $minute = 0 ]) : self
Parameters
$day : int|string = 1
$hour : int|string = 0
$minute : int|string = 0
Return values
self

march()

Set the execution time to every March.

public march([int|string $day = 1 ][, int|string $hour = 0 ][, int|string $minute = 0 ]) : self
Parameters
$day : int|string = 1
$hour : int|string = 0
$minute : int|string = 0
Return values
self

maxAttempts()

Set maximum retry attempts

public maxAttempts(int $attempts) : self
Parameters
$attempts : int
Return values
self

may()

Set the execution time to every May.

public may([int|string $day = 1 ][, int|string $hour = 0 ][, int|string $minute = 0 ]) : self
Parameters
$day : int|string = 1
$hour : int|string = 0
$minute : int|string = 0
Return values
self

monday()

Set the execution time to every Monday.

public monday([int|string $hour = 0 ][, int|string $minute = 0 ]) : self
Parameters
$hour : int|string = 0
$minute : int|string = 0
Return values
self

monthly()

Set the execution time to once a month.

public monthly([int|string $month = '*' ][, int|string $day = 1 ][, int|string $hour = 0 ][, int|string $minute = 0 ]) : self
Parameters
$month : int|string = '*'
$day : int|string = 1
$hour : int|string = 0
$minute : int|string = 0
Return values
self

november()

Set the execution time to every November.

public november([int|string $day = 1 ][, int|string $hour = 0 ][, int|string $minute = 0 ]) : self
Parameters
$day : int|string = 1
$hour : int|string = 0
$minute : int|string = 0
Return values
self

october()

Set the execution time to every October.

public october([int|string $day = 1 ][, int|string $hour = 0 ][, int|string $minute = 0 ]) : self
Parameters
$day : int|string = 1
$hour : int|string = 0
$minute : int|string = 0
Return values
self

onFailure()

Set failure callback

public onFailure(callable $callback) : self
Parameters
$callback : callable
Return values
self

onlyOne()

This will prevent the Job from overlapping.

public onlyOne([string|null $tempDir = null ][, callable|null $whenOverlapping = null ]) : self

It prevents another instance of the same Job of being executed if the previous is still running. The job id is used as a filename for the lock file.

Parameters
$tempDir : string|null = null

The directory path for the lock files

$whenOverlapping : callable|null = null

A callback to ignore job overlapping

Return values
self

onRetry()

Set retry callback

public onRetry(callable $callback) : self
Parameters
$callback : callable
Return values
self

onSuccess()

Set success callback

public onSuccess(callable $callback) : self
Parameters
$callback : callable
Return values
self

output()

Set the file/s where to write the output of the job.

public output(string|array<string|int, mixed> $filename[, bool $append = false ]) : self
Parameters
$filename : string|array<string|int, mixed>
$append : bool = false
Return values
self

priority()

Set job priority

public priority(string $priority) : self
Parameters
$priority : string

'high', 'normal', or 'low'

Return values
self

retryDelay()

Set retry delay

public retryDelay(int $seconds[, string $strategy = 'exponential' ]) : self
Parameters
$seconds : int
$strategy : string = 'exponential'

'linear' or 'exponential'

Return values
self

run()

Run the job.

public run() : bool
Return values
bool

runInBackground()

Check if the Job can run in background.

public runInBackground() : bool
Return values
bool

runWithRetry()

Run the job with retry support

public runWithRetry() : bool
Return values
bool

saturday()

Set the execution time to every Saturday.

public saturday([int|string $hour = 0 ][, int|string $minute = 0 ]) : self
Parameters
$hour : int|string = 0
$minute : int|string = 0
Return values
self

september()

Set the execution time to every September.

public september([int|string $day = 1 ][, int|string $hour = 0 ][, int|string $minute = 0 ]) : self
Parameters
$day : int|string = 1
$hour : int|string = 0
$minute : int|string = 0
Return values
self

setQueueId()

Set queue ID

public setQueueId(string $queueId) : self
Parameters
$queueId : string
Return values
self

sunday()

Set the execution time to every Sunday.

public sunday([int|string $hour = 0 ][, int|string $minute = 0 ]) : self
Parameters
$hour : int|string = 0
$minute : int|string = 0
Return values
self

then()

Set a function to be called after job execution.

public then(callable $fn[, bool $runInBackground = false ]) : self

By default this will force the job to run in foreground because the output is injected as a parameter of this function, but it could be avoided by passing true as a second parameter. The job will run in background if it meets all the other criteria.

Parameters
$fn : callable
$runInBackground : bool = false
Return values
self

thursday()

Set the execution time to every Thursday.

public thursday([int|string $hour = 0 ][, int|string $minute = 0 ]) : self
Parameters
$hour : int|string = 0
$minute : int|string = 0
Return values
self

timeout()

Set job timeout

public timeout(int $seconds) : self
Parameters
$seconds : int
Return values
self

toArray()

Convert job to array for serialization

public toArray() : array<string|int, mixed>
Return values
array<string|int, mixed>

tuesday()

Set the execution time to every Tuesday.

public tuesday([int|string $hour = 0 ][, int|string $minute = 0 ]) : self
Parameters
$hour : int|string = 0
$minute : int|string = 0
Return values
self

wednesday()

Set the execution time to every Wednesday.

public wednesday([int|string $hour = 0 ][, int|string $minute = 0 ]) : self
Parameters
$hour : int|string = 0
$minute : int|string = 0
Return values
self

weekly()

Set the execution time to once a week.

public weekly([int|string $weekday = 0 ][, int|string $hour = 0 ][, int|string $minute = 0 ]) : self
Parameters
$weekday : int|string = 0
$hour : int|string = 0
$minute : int|string = 0
Return values
self

when()

Truth test to define if the job should run if due.

public when(callable $fn) : self
Parameters
$fn : callable
Return values
self

withMetadata()

Add metadata to the job

public withMetadata(string $key, mixed $value) : self
Parameters
$key : string
$value : mixed
Return values
self

withTags()

Add tags to the job

public withTags(array<string|int, mixed> $tags) : self
Parameters
$tags : array<string|int, mixed>
Return values
self

calculateRetryDelay()

Calculate retry delay based on strategy

protected calculateRetryDelay(int $attempt) : int
Parameters
$attempt : int
Return values
int

checkDependencies()

Check if dependencies are met

protected checkDependencies() : bool
Return values
bool

runChainedJobs()

Run chained jobs

protected runChainedJobs(bool $success) : void
Parameters
$success : bool

Whether the current job succeeded

createLockFile()

Create the job lock file.

private createLockFile([mixed $content = null ]) : void
Parameters
$content : mixed = null

emailOutput()

Email the output of the job, if any.

private emailOutput() : bool
Return values
bool

exec()

Execute a callable job.

private exec() : string
Tags
throws
RuntimeException
Return values
string

postRun()

Things to run after job has run

private postRun() : void

removeLockFile()

Remove the job lock file.

private removeLockFile() : void

validateCronRange()

Validate sequence of cron expression.

private validateCronRange(int|string|null $value, int $min, int $max) : mixed
Parameters
$value : int|string|null
$min : int
$max : int

validateCronSequence()

Validate sequence of cron expression.

private validateCronSequence([int|string|null $minute = null ][, int|string|null $hour = null ][, int|string|null $day = null ][, int|string|null $month = null ][, int|string|null $weekday = null ]) : array<string|int, mixed>
Parameters
$minute : int|string|null = null
$hour : int|string|null = null
$day : int|string|null = null
$month : int|string|null = null
$weekday : int|string|null = null
Return values
array<string|int, mixed>

        
On this page

Search results