1. Go to this page and download the library: Download imtigger/laravel-job-status 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/ */
imtigger / laravel-job-status example snippets
namespace App\Jobs;
use Illuminate\Bus\Queueable;
use Illuminate\Queue\SerializesModels;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Contracts\Queue\ShouldQueue;
use Imtigger\LaravelJobStatus\Trackable;
class TrackableJob implements ShouldQueue
{
use InteractsWithQueue, Queueable, SerializesModels, Trackable;
public function __construct(array $params)
{
$this->prepareStatus();
$this->params = $params; // Optional
$this->setInput($this->params); // Optional
}
/**
* Execute the job.
*
* @return void
*/
public function handle()
{
$max = mt_rand(5, 30);
$this->setProgressMax($max);
for ($i = 0; $i <= $max; $i += 1) {
sleep(1); // Some Long Operations
$this->setProgressNow($i);
}
$this->setOutput(['total' => $max, 'other' => 'parameter']);
}
}
class YourController {
use DispatchesJobs;
function go() {
$job = new TrackableJob([]);
$this->dispatch($job);
$jobStatusId = $job->getJobStatusId();
}
}
$jobStatus = JobStatus::find($jobStatusId);
YourJob::dispatch(); // Returns PendingDispatch instead of YourJob object, leaving no way to retrive `$job->getJobStatusId();`
// Job protected methods (Call from your Job)
$this->prepareStatus(); // Must be called in constructor before any other methods
$this->setProgressMax(int $v); // Update the max number of progress
$this->setProgressNow(int $v); // Update the current number progress
$this->setProgressNow(int $v, int $every); // Update the current number progress, write to database only when $v % $every == 0
$this->incrementProgress(int $offset) // Increase current number progress by $offset
$this->incrementProgress(int $offset, int $every) // Increase current number progress by $offset, write to database only when $v % $every == 0
$this->setInput(array $v); // Store input into database
$this->setOutput(array $v); // Store output into database (Typically the run result)
// Job public methods (Call from your Job dispatcher)
$job->getJobStatusId(); // Return the primary key of JobStatus (To retrieve status later)
// JobStatus object fields
var_dump($jobStatus->job_id); // String (Result varies with driver, see note)
var_dump($jobStatus->type); // String
var_dump($jobStatus->queue); // String
var_dump($jobStatus->status); // String [queued|executing|finished|retrying|failed]
var_dump($jobStatus->attempts); // Integer
var_dump($jobStatus->progress_now); // Integer
var_dump($jobStatus->progress_max); // Integer
var_dump($jobStatus->input); // Array
var_dump($jobStatus->output); // Array, ['message' => $exception->getMessage()] if job failed
var_dump($jobStatus->created_at); // Carbon object
var_dump($jobStatus->updated_at); // Carbon object
var_dump($jobStatus->started_at); // Carbon object
var_dump($jobStatus->finished_at); // Carbon object
// JobStatus generated fields
var_dump($jobStatus->progress_percentage); // Double [0-100], useful for displaying progress bar
var_dump($jobStatus->is_ended); // Boolean, true if status == finished || status == failed
var_dump($jobStatus->is_executing); // Boolean, true if status == executing
var_dump($jobStatus->is_failed); // Boolean, true if status == failed
var_dump($jobStatus->is_finished); // Boolean, true if status == finished
var_dump($jobStatus->is_queued); // Boolean, true if status == queued
var_dump($jobStatus->is_retrying); // Boolean, true if status == retrying
Loading please wait ...
Before you can download the PHP files, the dependencies should be resolved. This can take some minutes. Please be patient.