PHP code example of itsemon245 / laravel-pausable-job
1. Go to this page and download the library: Download itsemon245/laravel-pausable-job 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/ */
itsemon245 / laravel-pausable-job example snippets
//Other imports
use Itsemon245\PausableJob\Traits\Pausable;
class EmailJob implements ShouldQueue
{
//Other traits
use Pausable;
public function __construct(public Campaign $campaign)
{
/**
* Set which model is responsible to pause the job
*/
$this->setPausedBy($campaign);
}
public function handle(): void
{
/**
* You can pause the job like this with your desired condition
*/
if ($this->campaign->paused_at) {
$this->pause();
return;
}
// Handle the actual logic of the job
}
}
//Other imports
use Itsemon245\PausableJob\Traits\HasPausableJobs;
class Campaign extends Model
{
//Other traits
use HasPausableJobs;
}
class CampaignController extends Controller{
public function pause(Campaign $campaign){
//By pausing the campaign the jobs will be automatically paused because of the condition in the EmailJob handle method
$campaign->update([
'paused_at' => now()
]);
return back();
}
public function resume(Campaign $campaign){
$campaign->update([
'paused_at' => null
]);
// To actually resume the jobs from paused state you have to call the resumeJob() method from the campaign
$campaign->resumeJobs();
}
}