CronJob
in package
Table of Contents
Properties
- $config : array<string, mixed>
- Configuration data injected via {@see setConfig()}.
- $heartbeatFile : string
- Path to heartbeat file that is appended with a timestamp on every heartbeat.
- $jobName : string
- Human‑readable job identifier.
- $lockFile : string
- Path to lock file that prevents concurrent execution.
- $logDir : string
- Directory where cronjob log files are written (no trailing slash).
- $startTime : float
- Timestamp captured by {@see startTimer()} (microtime true).
Methods
- __construct() : mixed
- CronJob constructor.
- acquireLock() : bool
- Attempt to acquire an exclusive lock for this job.
- endTimer() : float
- Stop the timer and return elapsed seconds since {@see startTimer()}.
- getJobName() : mixed
- loadConfigs() : array<string|int, mixed>
- Loads shop settings from a .env file.
- log() : void
- Append a timestamped line to the per‑job log file.
- memoryUsage() : string
- Return peak memory usage of this process.
- releaseLock() : void
- Release the lock previously created by {@see acquireLock()}.
- startTimer() : void
- Start execution timer.
- touchHeartbeat() : void
- Append an "alive" + timestamp to the heartbeat file.
- setConfig() : void
- Inject runtime configuration values.
Properties
$config
Configuration data injected via {@see setConfig()}.
protected
array<string, mixed>
$config
$heartbeatFile
Path to heartbeat file that is appended with a timestamp on every heartbeat.
protected
string
$heartbeatFile
$jobName
Human‑readable job identifier.
protected
string
$jobName
Used as basename for log and lock files.
$lockFile
Path to lock file that prevents concurrent execution.
protected
string
$lockFile
$logDir
Directory where cronjob log files are written (no trailing slash).
protected
string
$logDir
$startTime
Timestamp captured by {@see startTimer()} (microtime true).
protected
float
$startTime
Methods
__construct()
CronJob constructor.
public
__construct(string $jobName[, string $logDir = '/var/log/cronjobs' ][, string $lockDir = '/tmp' ][, string $heartbeatFile = '/tmp/cron_heartbeat.log' ]) : mixed
Parameters
- $jobName : string
-
Name of the cronjob like "daily-report".
- $logDir : string = '/var/log/cronjobs'
-
Directory for log files. Defaults to
/var/log/cronjobs. - $lockDir : string = '/tmp'
-
Directory for lock files. Defaults to
/tmp. - $heartbeatFile : string = '/tmp/cron_heartbeat.log'
-
File written by touchHeartbeat(). Defaults to
/tmp/cron_heartbeat.log.
acquireLock()
Attempt to acquire an exclusive lock for this job.
public
acquireLock() : bool
Return values
bool —true if the lock was successfully created, false if another instance is running.
endTimer()
Stop the timer and return elapsed seconds since {@see startTimer()}.
public
endTimer() : float
Return values
float —Seconds with millisecond precision.
getJobName()
public
getJobName() : mixed
loadConfigs()
Loads shop settings from a .env file.
public
loadConfigs(array<string|int, mixed>|string $filePaths) : array<string|int, mixed>
Parameters
- $filePaths : array<string|int, mixed>|string
-
The path to the file to be loaded.
Tags
Return values
array<string|int, mixed> —Array keys are the setting names and the values are the values. If a value contains commas, it's split into an array.
log()
Append a timestamped line to the per‑job log file.
public
log(string $message) : void
Parameters
- $message : string
-
Message to write.
memoryUsage()
Return peak memory usage of this process.
public
memoryUsage() : string
Return values
string —Formatted string like "12.34 MB".
releaseLock()
Release the lock previously created by {@see acquireLock()}.
public
releaseLock() : void
Safe to call even if the lock file has already been removed.
startTimer()
Start execution timer.
public
startTimer() : void
Called before the workload begins.
touchHeartbeat()
Append an "alive" + timestamp to the heartbeat file.
public
touchHeartbeat() : void
Call this periodically in long‑running jobs to allow external monitoring.
setConfig()
Inject runtime configuration values.
private
setConfig(array<string, mixed> $config) : void
Parameters
- $config : array<string, mixed>