Advanced Features
Accessing Job Context
You can access the current Job instance (ID, attempts, configuration) from within your handler by using the $this->currentJob property. This is available in all standard Job types (CommandJob, ShellJob, UrlJob, ClosureJob, EventJob).
Example
// In a custom CommandJob or ShellJob
public function handle(mixed $payload): mixed
{
$jobId = $this->currentJob->getJobId();
$attempts = $this->currentJob->getAttempt();
// Pass context to the command
return command("process:data --id={$jobId} --try={$attempts}");
}
If you are creating a custom Job class, simply use the trait:
use Daycry\Jobs\Traits\InteractsWithCurrentJob;
class MyCustomJob extends Job implements JobInterface
{
use InteractsWithCurrentJob;
public function handle(mixed $payload): mixed
{
// Access $this->currentJob
}
}
Direct Queueing
Enqueue ad-hoc jobs without scheduler:
$id = Services::queueJob('command','jobs:cleanup','default', function($j){
$j->named('cleanup_run')->maxRetries(3)->timeout(120);
});
Queue Management
Using QueueManager
Access queue backends centrally:
use Daycry\Jobs\Libraries\QueueManager;
// Get default queue
$queue = QueueManager::instance()->getDefault();
// Get specific backend
$redis = QueueManager::instance()->get('redis');
$database = QueueManager::instance()->get('database');
// Enqueue directly
$job = new Job(job: 'command', payload: 'jobs:cleanup');
$job->setQueue('high-priority');
$id = $redis->enqueue($job->getDataQueue());
Instrumenting Queues with Metrics
use Daycry\Jobs\Libraries\InstrumentedQueueDecorator;
use Daycry\Jobs\Metrics\Metrics;
$queue = QueueManager::instance()->get('redis');
$instrumented = new InstrumentedQueueDecorator(
queue: $queue,
metrics: Metrics::get(),
backendName: 'redis'
);
// All operations now tracked:
// - queue_enqueue_total{backend,queue,status}
// - queue_fetch_total{backend,queue}
// - queue_ack_total / queue_nack_total
// - queue_enqueue_duration_seconds / queue_fetch_duration_seconds
Fluent Job Chaining (Enhanced)
The callback API now supports a fluent chaining syntax for complex workflows:
Basic Chaining
use Daycry\Jobs\Job;
$processPayment = new Job('command', 'process:payment');
$sendInvoice = new Job('command', 'send:invoice');
$notifyAdmin = new Job('command', 'notify:admin');
$cleanup = new Job('closure', fn() => log_message('info', 'Workflow complete'));
$processPayment
->then($sendInvoice) // Execute on success
->catch($notifyAdmin) // Execute on failure
->finally($cleanup) // Always execute
->push();
Chain Multiple Jobs
$job->chain([
new Job('command', 'step1'),
new Job('command', 'step2'),
new Job('command', 'step3'),
])->push();
Method Reference
Method |
Filter |
Description |
|---|---|---|
|
|
Enqueue after successful completion |
|
|
Enqueue on failure/error |
|
|
Always enqueue regardless of outcome |
|
Sequential |
Execute jobs in order, stopping on failure |
Advanced Example with Inheritance
$importJob = (new Job('command', 'import:users'))
->named('user_import')
->enqueue('default')
->then(
(new Job('command', 'send:welcome:emails'))
->enqueue('notifications')
)
->catch(
(new Job('closure', function($meta) {
// Access parent error via $meta['parentError']
log_message('error', 'Import failed: ' . $meta['parentError']);
}))
)->finally(
(new Job('command', 'cleanup:temp:files'))
);
$importJob->push();
Inherited Fields (inherit option in setCallbackJob):
output→parentOutputerror→parentErrorattempts→parentAttemptsname→parentNamesource→parentSource
Callbacks & Chaining
Attach a callback job run after completion:
$job->setCallbackJob(function($parent){
return (new Job(job: 'command', payload: 'jobs:notify:done'))->enqueue('notifications');
}, ['on' => 'success']);
Retry Policies
Configure backoff in config (retryBackoffStrategy, base, multiplier, jitter).
Sensitive Data Masking
Add keys to sensitiveKeys to mask in logs recursively.
Metrics
Provide a custom collector implementing MetricsCollectorInterface and set Jobs::$metricsCollector.
Next: Metrics & Monitoring