hlacos / joboquent
Simple bundle to store queued jobs statuses and history.
dev-master
2014-10-06 13:58 UTC
Requires
- php: >=5.4.0
- illuminate/support: 4.2.*
This package is auto-updated.
Last update: 2024-11-13 23:08:49 UTC
README
Eloquent based Jobs for Laravel
It's under development, not recommended for production use!
Install steps
- add bundle to composer: "hlacos/joboquent": "dev-master"
- composer install
- add service provider to the providers list: 'Hlacos\Joboquent\JoboquentServiceProvider'
- php artisan migrate --package="hlacos/joboquent"
- php artisan db:seed --class="Hlacos\Joboquent\JobStatusTableSeeder"
Usage
Create new job and run it
Tipically it creates in the controller.
$job = new JobModel; $job->name = 'Export customers'; $job->save(); $job->run('MyJob');
The string parameter of the run method is the class name of the Worker in the next step.
Extended JobModel
Feel free to extend this model, just read the extended model section in the worker.
Create worker
Extend Job to make your own working code
use Hlacos\Joboquent\Job; class MyJob extends Job { // Callbacks public function beforeStart() {} public function beforeEnd() {} // The working code public function work() {} }
Worker use extended JobModel
Only override the $jobModelClass public attribute name to the Extended class name.
Callbacks
- beforeStart: runs before the work method. You can initialize data or clean up database...
- beforeEnd: runs before the queue job deleted. You can touch related models timestamps or move created files to their public folder...
Set the current percent
Tipically used in the work method in a cycle.
$this->jobModel->setPercent($percent);
Related models
You can set polimorphic relation to the JobModel.
public function jobs() { return $this->morphMany('Hlacos\Joboquent\JobModel', 'jobable'); }
public function job() { return $this->morphOne('Hlacos\Joboquent\JobModel', 'jobable'); }
Don't forget to save related model to the jobModel before it runs.
TODO
- Refactoring / code cleaning.