PHP code example of spatie / laravel-long-running-tasks
1. Go to this page and download the library: Download spatie/laravel-long-running-tasks 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/ */
spatie / laravel-long-running-tasks example snippets
use Spatie\LongRunningTasks\LongRunningTask;
use Spatie\LongRunningTasks\Enums\TaskResult;
use Spatie\LongRunningTasks\LongRunningTask;
class MyTask extends LongRunningTask
{
public function check(LongRunningTaskLogItem $logItem): TaskResult
{
// get some information about this task
$meta = $logItem->meta
// do some work here
$allWorkIsDone = /* ... */
// return wheter we should continue the task in a new run
return $allWorkIsDone
? TaskResult::StopChecking
: TaskResult::ContinueChecking
}
}
MyTask::make()->meta($anArray)->start();
return [
/*
* Behind the scenes, this packages use a queue to call tasks.
* Here you can choose the queue that should be used by default.
*/
'queue' => 'default',
/*
* If a task determines that it should be continued, it will
* be called again after this amount of time
*/
'default_check_frequency_in_seconds' => 10,
/*
* The default class that implements a strategy for determining the check frequency in seconds.
* - `DefaultCheckStrategy` will check the task every `LongRunningTaskLogItem::check_frequency_in_seconds` seconds.
* - `StandardBackoffCheckStrategy` will check the task every `LongRunningTaskLogItem::check_frequency_in_seconds` seconds,
* but will increase the frequency with the multipliers 1, 6, 12, 30, 60, with the maximum being 60 times the original frequency.
* With the default check frequency, this translates to 10, 60, 120, 300, and 600 seconds between checks.
* - `LinearBackoffCheckStrategy` will check the task every `LongRunningTaskLogItem::check_frequency_in_seconds` seconds,
* but will increase the frequency linearly with each attempt, up to a maximum multiple of 6 times the original frequency.
* - `ExponentialBackoffCheckStrategy` will check the task every `LongRunningTaskLogItem::check_frequency_in_seconds` seconds,
* but will increase the frequency exponentially with each attempt, up to a maximum of 6 times the original frequency.
*/
'default_check_strategy_class' => Spatie\LongRunningTasks\Strategies\DefaultCheckStrategy::class,
/*
* When a task is not completed in this amount of time,
* it will not run again, and marked as `didNotComplete`.
*/
'keep_checking_for_in_seconds' => 60 * 5,
/*
* The model that will be used by default to track
* the status of all tasks.
*/
'log_model' => Spatie\LongRunningTasks\Models\LongRunningTaskLogItem::class,
/*
* The job responsible for calling tasks.
*/
'task_job' => Spatie\LongRunningTasks\Jobs\RunLongRunningTaskJob::class,
];
use Spatie\LongRunningTasks\LongRunningTask;
use Spatie\LongRunningTasks\Enums\TaskResult;
class MyTask extends LongRunningTask
{
public function check(LongRunningTaskLogItem $logItem): TaskResult
{
// get some information about this task
$meta = $logItem->meta // returns an array
// do some work here
$allWorkIsDone = /* ... */
// return wheter we should continue the task in a new run
return $allWorkIsDone
? TaskResult::StopChecking
: TaskResult::ContinueChecking
}
}
class MyTask extends LongRunningTask
{
public function check(LongRunningTaskLogItem $logItem): TaskResult
{
// get some information about this task
$meta = $logItem->meta // returns an array
// rest of method
}
}
class MyTask extends LongRunningTask
{
public int $checkFrequencyInSeconds = 20;
}
use Spatie\LongRunningTasks\Strategies\StandardBackoffCheckStrategy;
class MyTask extends LongRunningTask
{
public string $checkStrategy = StandardBackoffCheckStrategy::class;
}
use Spatie\LongRunningTasks\LongRunningTask;
use Spatie\LongRunningTasks\Enums\TaskResult;
class MyTask extends LongRunningTask
{
public function check(LongRunningTaskLogItem $logItem): TaskResult
{
throw new Exception('Something went wrong');
}
public function onFail(LongRunningTaskLogItem $logItem, Exception $exception): ?TaskResult
{
// handle the exception
}
}
namespace App\Models;
use Spatie\LongRunningTasks\Models\LongRunningTaskLogItem as BaseLongRunningTaskLogItem;
class LongRunningTaskLogItem extends BaseLongRunningTaskLogItem
{
// your custom functionality
}
namespace App\Models;
use Spatie\LongRunningTasks\Models\LongRunningTaskLogItem as BaseLongRunningTaskLogItem;
class LongRunningTaskLogItem extends BaseLongRunningTaskLogItem
{
protected static function booted()
{
static::creating(function ($logItem) {
$customValue = $logItem->meta['some_key'];
// optionally, you could unset the custom value from the meta array
unset($logItem->meta['some_key']);
$logItem->custom_field = $customValue;
});
}
}
namespace App\Jobs;
use Spatie\LongRunningTasks\Jobs\RunLongRunningTaskJob as BaseRunLongRunningTaskJob;
class RunLongRunningTaskJob extends BaseRunLongRunningTaskJob
{
// your custom functionality
}