Skip to content

Commit

Permalink
Merge pull request #24 from VeerUP/without-overlapping
Browse files Browse the repository at this point in the history
Without overlapping
  • Loading branch information
omnilight committed Jun 12, 2018
2 parents a4e48a1 + a2dfc43 commit 31cef4e
Show file tree
Hide file tree
Showing 4 changed files with 100 additions and 5 deletions.
36 changes: 35 additions & 1 deletion CallbackEvent.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
use Yii;
use yii\base\Application;
use yii\base\InvalidParamException;
use yii\mutex\Mutex;

/**
* Class CallbackEvent
Expand All @@ -24,14 +25,19 @@ class CallbackEvent extends Event
protected $parameters;

/**
* Create a new event instance.
*
* @param Mutex $mutex
* @param string $callback
* @param array $parameters
* @param array $config
* @throws InvalidParamException
*/
public function __construct($callback, array $parameters = [], $config = [])
public function __construct(Mutex $mutex, $callback, array $parameters = [], $config = [])
{
$this->callback = $callback;
$this->parameters = $parameters;
$this->_mutex = $mutex;

if (!empty($config)) {
Yii::configure($this, $config);
Expand Down Expand Up @@ -59,6 +65,34 @@ public function run(Application $app)
$this->trigger(self::EVENT_AFTER_RUN);
return $response;
}

/**
* Do not allow the event to overlap each other.
*
* @return $this
* @throws InvalidParamException
*/
public function withoutOverlapping()
{
if (empty($this->_description)) {
throw new InvalidParamException(
"A scheduled event name is required to prevent overlapping. Use the 'description' method before 'withoutOverlapping'."
);
}

return parent::withoutOverlapping();
}

/**
* Get the mutex name for the scheduled command.
*
* @return string
*/
protected function mutexName()
{
return 'framework/schedule-' . sha1($this->_description);
}

/**
* Get the summary of the event for display.
*
Expand Down
35 changes: 34 additions & 1 deletion Event.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
use yii\base\Component;
use yii\base\InvalidCallException;
use yii\mail\MailerInterface;
use yii\mutex\Mutex;


/**
Expand Down Expand Up @@ -72,16 +73,24 @@ class Event extends Component
* @var string
*/
protected $_description;
/**
* The mutex implementation.
*
* @var \yii\mutex\Mutex
*/
protected $_mutex;

/**
* Create a new event instance.
*
* @param Mutex $mutex
* @param string $command
* @param array $config
*/
public function __construct($command, $config = [])
public function __construct(Mutex $mutex, $command, $config = [])
{
$this->command = $command;
$this->_mutex = $mutex;
$this->_output = $this->getDefaultOutput();
parent::__construct($config);
}
Expand All @@ -101,6 +110,16 @@ public function run(Application $app)
$this->trigger(self::EVENT_AFTER_RUN);
}

/**
* Get the mutex name for the scheduled command.
*
* @return string
*/
protected function mutexName()
{
return 'framework/schedule-' . sha1($this->_expression . $this->command);
}

/**
* Run the command in the foreground.
*
Expand Down Expand Up @@ -479,6 +498,20 @@ public function user($user)
return $this;
}

/**
* Do not allow the event to overlap each other.
*
* @return $this
*/
public function withoutOverlapping()
{
return $this->then(function() {
$this->_mutex->release($this->mutexName());
})->skip(function() {
return !$this->_mutex->acquire($this->mutexName());
});
}

/**
* Register a callback to further filter the schedule.
*
Expand Down
9 changes: 8 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
Schedule extension for Yii2
===========================

This extension is the port of Laravel's Schedule component (http://laravel.com/docs/master/artisan#scheduling-artisan-commands)
This extension is the port of Laravel's Schedule component (https://laravel.com/docs/master/scheduling#scheduling-artisan-commands)

Installation
------------
Expand Down Expand Up @@ -146,6 +146,13 @@ $schedule->command('foo')->monthly()->when(function()
$schedule->command('foo')->sendOutputTo($filePath)->emailOutputTo('foo@example.com');
```

**Preventing Task Overlaps**

```php
$schedule->command('foo')->withoutOverlapping();
```
Used by default yii\mutex\FileMutex or 'mutex' application component (http://www.yiiframework.com/doc-2.0/yii-mutex-mutex.html)

How to use this extension in your application?
----------------------------------------------

Expand Down
25 changes: 23 additions & 2 deletions Schedule.php
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
<?php

namespace omnilight\scheduling;

use Yii;
use yii\base\Component;
use yii\base\Application;
use yii\mutex\FileMutex;


/**
Expand All @@ -17,11 +20,29 @@ class Schedule extends Component
*/
protected $_events = [];

/**
* The mutex implementation.
*
* @var \yii\mutex\Mutex
*/
protected $_mutex;

/**
* @var string The name of cli script
*/
public $cliScriptName = 'yii';

/**
* Schedule constructor.
* @param array $config
*/
public function __construct(array $config = [])
{
$this->_mutex = Yii::$app->has('mutex') ? Yii::$app->get('mutex') : (new FileMutex());

parent::__construct($config);
}

/**
* Add a new callback event to the schedule.
*
Expand All @@ -31,7 +52,7 @@ class Schedule extends Component
*/
public function call($callback, array $parameters = array())
{
$this->_events[] = $event = new CallbackEvent($callback, $parameters);
$this->_events[] = $event = new CallbackEvent($this->_mutex, $callback, $parameters);
return $event;
}
/**
Expand All @@ -53,7 +74,7 @@ public function command($command)
*/
public function exec($command)
{
$this->_events[] = $event = new Event($command);
$this->_events[] = $event = new Event($this->_mutex, $command);
return $event;
}

Expand Down

0 comments on commit 31cef4e

Please sign in to comment.