dev-develop
dev-develop
The Requires
- php >=5.3.0
- illuminate/support ~4
- nesbot/carbon 1.*
by Hélder Duarte
dev-master
9999999-dev
The Requires
- php >=5.3.0
- illuminate/support ~4
- nesbot/carbon 1.*
by Hélder Duarte
Wallogit.com
2017 © Pedro Peláez
A Laravel 4 package that enables you to queue events and fire them in sequence, or at a specific time in the future., (*1)
Add the package to your composer.json and run composer update., (*2)
{
"require": {
"cossou/eventcron": "*"
}
}
Add the service provider in app/config/app.php:, (*3)
'providers' => [
…
'Cossou\EventCRON\EventCRONServiceProvider'
]
Optionally add the facade to your aliases:, (*4)
'aliases' => [
…
'EventCRON' => 'Cossou\EventCRON\Facades\EventCRON'
]
If you are using Laravel 4.0 ,perform the migration to create the database tables:, (*5)
php artisan migrate --package=cossou/eventcron
Or if you are using Laravel 4.1, publish the package migration to your application, (*6)
php artisan migrate:publish cossou/eventcron php artisan migrate
To get started, there are three ways in which you can utilize this package., (*7)
Via the facade, if you added it to your configuration file (preferred way):, (*8)
EventCRON::queue('myevent');
By using the Laravel IoC container:, (*9)
App::make('eventcron')->queue('myevent');
Directly through the class:, (*10)
$eventcron = new Cossou\EventCRON\EventCRONManager();
$eventcron->queue('myevent');
As shown, you can just queue your event and listen to it elsewhere., (*11)
EventCRON::queue('myevent');
Event::listen('myevent', function()
{
echo 'myevent just got fired!';
});
Flushing the queue for this event will fire all of them at once, since no time has been set., (*12)
You can also pass some data to your event handler in the form of an array., (*13)
EventCRON::queue('myevent', ['string', $variable, 12, new Object()]);
Laravel will then extract all of these variables and pass them to your event handler:, (*14)
Event::listen('myevent', function($string, $variable, $number, $object)
{
echo 'myevent just got fired with some neat arguments';
dd($string, variable, $number, $object);
});
Of course, the main idea of this package is to schedule your events. Just pass a Carbon instance as third parameter:, (*15)
EventCRON::queue('myevent', NULL, Carbon\Carbon::now()->addHour());
This event will only be triggered one hour from now., (*16)
Carbon is a nice extension to PHP's datetime class(es). For more info: https://github.com/briannesbitt/Carbon., (*17)
Now that you've added all these events, you'd want them to be triggered so your whole setup actually does something., (*18)
To trigger the queue for a single event:, (*19)
EventCRON::flush('myevent');
To trigger the queue of all events:, (*20)
EventCRON::flushAll();
Please note: events with an execution time set will only be triggered if that moment is in the past. In addition, if the configuration file states enabled as false or run_only_from_cli as true (and you're flushing a queue from code), nothing will happen., (*21)
The following commands are used to flush queues from the CLI:, (*22)
php artisan eventcron:trigger myevent
php artisan eventcron:trigger:all
On most occasions though, you'd trigger events in your queue with a CRON job instead of directly from code or the CLI., (*23)
Use crontab -e or sudo crontab -e to get into your CRON file and add the following line at the end to flush all queues every minute (because you never know when you've scheduled an event):, (*24)
*/1 * * * * php /var/www/myproject/artisan eventcron:trigger:all
Publish the package's configuration file with:, (*25)
php artisan config:publish cossou/eventcron
true)Simply enable or disable the package., (*26)
BOOLEAN true / false
true)Allow the flushing of queues only from your command line interface (CLI)., (*27)
BOOLEAN true / false
50)Maximum number of events to fire in one run (set it to a lower number if you don't want the server go slower)., (*28)
INTEGER number
false)Whether or not to write debug messages your log., (*29)
BOOLEAN true / false