Skip to content

cossou/laravel-eventcron

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

13 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Laravel EventCron Bundle

=======================

Laravel bundle to queue Events to a database and later fire them through artisan or a CronJob.

Introduction

I created this bundle to run events in a queue every x minutes through a CronJob (or manually).

It's an extension of the original Laravel Event, where you can queue events to table to be fired later.

Some examples

Example 1:

You want to schedule an e-mail to an user 24h after the registration.

	EventCron::queueDB('email24', array('user_id' => 1, 'message' => 'welcome'), date("Y-m-d H:i:s", strtotime("+24 hours")));

Example 2:

You have a time consumption action and you don't want to block the execution (Kind of non-blocking execution :).

Example 3:

You can also think about newsletter systems.

Note: this bundle does not attempt to be a replacement for Workers. You are not off loading processes to another machine. The processes are still executed in the same server has your own app.

Installation

php artisan bundle:install eventcron

Add it to application/bundles.php:

    return array(
        ...
        'eventcron' => array(
            'auto'  => true
        ),
        ...
    );

Now migrate to create the table:

php artisan migrate eventcron

Now play with it. For example:

	Route::get('addevent', function()
	{
		...
		// In Real time: 
		// Event::fire('log_something', array('FOOBAR', array('foo' => 'bar')));

		// With EventCron:
		EventCron::queueDB('log_something', array('FOOBAR', array('foo' => 'bar')));
		...
	});
	
	// The event
	Event::listen('log_something', function($str, $arr)
	{
		// Note: If you use a bundle inside please use Bundle::start('yourbundle');
		Log::info('str => ' . $str . ' arr => ' . print_r($arr, true));
	});

Setup the CronJob:

*/1 * * * * root php /var/www/laravel/artisan Eventcron::run log_something --env=local

Or just run the command:

php artisan Eventcron::run log_something --env=local

Note: If your CLI PHP doesn't have permissions to write to the log file just add "sudo php artisan ..."

Thats it, the cron will fire all the log_something events in the queue by FIFO order.

If you want there is a method to run all the queues in the table:

php artisan Eventcron::run:runall --env=local

You can also fire the events through a route (which I don't recommend). Just change the eventcron/config/config.php file to allow it:

'run_only_from_cli' => false,

And then:

	Route::get('fire', function()
	{
		EventCron::flushDB('log_something');
	});

Notice you can pass a date to the queue:

	 EventCron::queueDB('log_something', array('FOOBAR', array('foo' => 'bar')), date("Y-m-d H:i:s", strtotime("+2 hours")));

API

Add event to a queue:

	EventCron::queueDB($name = "string", $args = array() [, $date = date("Y-m-d H:i:s")]);

Flush events from a queue:

	EventCron::flushDB($name = "string");

Flush all the events:

	EventCron::flushAllDB();

Configurations

config.php (eventcron/config/config.php)

###enabled (default true)

Well, this one is easy. Right?

BOOLEAN true / false

###run_only_from_cli (default true)

Allow the CronJob to be run only from CLI (artisan).

BOOLEAN true / false

###max_events_per_execution (default 50)

Max number of events to fire in one run (set it to a lower number if you don't want the server go slower).

INTEGER number

###log_events (default true)

BOOLEAN true / false

##Questions? Problems?

Drop me a line at cossou@gmail.com or @cossou

About

Laravel EventCron Bundle

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages