Package Data | |
---|---|
Maintainer Username: | masnun |
Maintainer Contact: | masnun@transcendio.net (Abu Ashraf Masnun) |
Package Create Date: | 2014-09-22 |
Package Last Update: | 2014-11-25 |
Language: | PHP |
License: | Unknown |
Last Refreshed: | 2025-01-25 15:15:08 |
Package Statistics | |
---|---|
Total Downloads: | 52 |
Monthly Downloads: | 0 |
Daily Downloads: | 0 |
Total Stars: | 3 |
Total Watchers: | 3 |
Total Forks: | 0 |
Total Open Issues: | 0 |
A Laravel package that allows scheduling certain actions upon job completion or queue exhaustion.
This package allows you to register a class or closure to be executed when an individual job completes. You can also register a class or closure when each and every job from a batch of jobs has finished running.
The package was sponsored by the good people at Pure SEO
The package is available from the Packagist composer repository. Add these to your composer.json
file and run composer install
:
"masnun/joinjobs": "dev-master"
Once you have installed the package, you need to register the service provider. Open app/config/app.php
and add Masnun\Joinjobs\JoinjobsServiceProvider
to the providers
array.
Please consult the appropriate sections of the Laravel Docs to set them up properly.
The package uses certain database tables to keep track of jobs and queues. We need to run the migrations on the package to make sure the database schema is created properly.
php artisan migrate --package=masnun/joinjobs
The process is simple:
Create a new join, pass it a handler. Choose whether it should be auto deleted or not. The handler could be a closure or fully qualified class name that has a run()
method. The second parameter is for auto deletion.
We add jobs to the Join. Each job can take an optional closure/class to be executed.
We have to let the system know when all jobs have been dispatched. We can pass the optional 3rd argument to a job as true
to mark it as the last job. Or alternatively we can call the setFullyDispatched($joinId)
method on the JoinJobsManager.
Eg. $joinjobsManager->setFullyDispatched($joinId)
This is a requirement to make sure that our JoinHandler is not executed before all jobs have been dispatched while using a sync
driver.
// Create a new manager
$joinjobsManager = new JoinJobsManager();
// We create a new join, set auto deletion to true
$joinId = $joinjobsManager->createJoin(null, true);
$joinjobsManager->addHandlerToJoin($joinId, function() {
echo "The Join successfully completed!";
});
// addJob()
$jobId = $joinjobsManager->addJob($joinId);
Queue::push('DemoJob', ['jobId' => $jobId, 'sleepDuration' => 5]);
$joinjobsManager->addHandlerToJob($jobId, function() use($jobId) {
echo "Completed Job ID: {$jobId}";
});
// This job is the last one, we are not adding any more jobs
$jobId = $joinjobsManager->addJob($joinId, "\\Masnun\\Joinjobs\\JoinHandler", true);
Queue::push('DemoJob', ['jobId' => $jobId, 'sleepDuration' => 10]);
return "Added two jobs!";
Once a job is complete, we must let our JoinJobsManager
know that one of the job has finished executing. Here's a sample DemoJob
class:
class DemoJob
{
public function fire($job, $data)
{
// We could put this in the DI containter
// if we use this a lot
$joinjobsManager = new JoinJobsManager();
$jobId = $data['jobId'];
$sleepDuration = $data['sleepDuration'];
sleep($sleepDuration);
// Mark the job as complete
$joinjobsManager->completeJob($jobId);
// Remove the job from Queue
$job->delete();
}
}
The createJoin()
method takes a second optional argument. If we set it true, the join and all jobs related to it will be deleted.
$joinjobsManager->deleteJoin($joinId);
That should delete the join and all the jobs belonging to it.