PHP code example of phattarachai / task-runs-laravel
1. Go to this page and download the library: Download phattarachai/task-runs-laravel library. Choose the download type require.
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
use Phattarachai\TaskRunsLaravel\TaskDispatcher;
app(TaskDispatcher::class)->dispatch('scan'); // global
app(TaskDispatcher::class)->dispatch('scan', $library); // scoped to a subject (morph)
app(TaskDispatcher::class)->dispatch('scan', options: ['all' => true], dispatchedBy: 'schedule');
use Phattarachai\TaskRunsLaravel\Concerns\InteractsWithTaskRun;
class ScanJob implements ShouldQueue
{
use InteractsWithTaskRun, Queueable;
public function handle(): void
{
if ($this->taskRunCancelledBeforeStart()) {
return;
}
$this->taskRun->markRunning($files->count());
foreach ($files as $file) {
// ... work ...
$this->taskRun->advance();
if ($this->taskRun->isCancelled()) {
$this->taskRun->markCancelled('Cancelled after '.$this->taskRun->processed.'.');
return;
}
}
$this->taskRun->markSuccess('Scanned everything.');
}
}
$this->reportProgress('เจอ Tax ID 0105558… → ค้นคู่ค้าใน DB'); // the trait's shorthand
$this->taskRun->reportProgress('ยอด 10,000 = ฐาน 9,345.79 + VAT 654.21 ✓');
use Phattarachai\TaskRunsLaravel\Jobs\Middleware\TrackedJob;
class BuildReportJob implements ShouldQueue
{
use InteractsWithTaskRun, Queueable;
public function middleware(): array
{
return [new TrackedJob];
}
public function handle(): void
{
// just the work — running/success/failed and the queued-cancel skip are handled around you
}
}
use Phattarachai\TaskRunsLaravel\Concerns\Interruptible;
class CompatJob implements ShouldQueue
{
use InteractsWithTaskRun, Interruptible, Queueable;
public int $tries = 1;
public int $timeout = 21540; // $this->requeueForYield($this->taskRun); // re-arm as queued + re-dispatch; a fresh worker resumes
return;
}
// ... work ...
$this->taskRun->advance();
}
$this->taskRun->markSuccess();
}
}