Skip to content

Commit

Permalink
Merge branch 'zacksleo-feature-on-one-server'
Browse files Browse the repository at this point in the history
  • Loading branch information
kstkn committed Nov 7, 2018
2 parents b69e0fa + d131705 commit e4d958f
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 3 deletions.
30 changes: 28 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,8 @@ This project is inspired by the Laravel's Schedule component and tries to bring
Quote from Laravel's documentation:

```
In the past, developers have generated a Cron entry for each console command they wished to schedule.
However, this is a headache. Your console schedule is no longer in source control,
In the past, developers have generated a Cron entry for each console command they wished to schedule.
However, this is a headache. Your console schedule is no longer in source control,
and you must SSH into your server to add the Cron entries. Let's make our lives easier.
```

Expand Down Expand Up @@ -153,6 +153,32 @@ $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)

**Running Tasks On One Server**

>To utilize this feature, you must config mutex in the application component, except the FileMutex: `yii\mutex\MysqlMutex`,`yii\mutex\PgsqlMutex`,`yii\mutex\OracleMutex` or `yii\redis\Mutex`. In addition, all servers must be communicating with the same central db/cache server.
Below shows the redis mutex demo:

```php
'components' => [
'mutex' => [
'class' => 'yii\redis\Mutex',
'redis' => [
'hostname' => 'localhost',
'port' => 6379,
'database' => 0,
]
],
],
```

```php
$schedule->command('report:generate')
->fridays()
->at('17:00')
->onOneServer();
```

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

Expand Down
17 changes: 16 additions & 1 deletion src/Event.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,10 @@
use yii\base\Application;
use yii\base\Component;
use yii\base\InvalidCallException;
use yii\base\InvalidConfigException;
use yii\mail\MailerInterface;
use yii\mutex\Mutex;

use yii\mutex\FileMutex;

/**
* Class Event
Expand Down Expand Up @@ -518,6 +519,20 @@ public function withoutOverlapping()
});
}

/**
* Allow the event to only run on one server for each cron expression.
*
* @return $this
*/
public function onOneServer()
{
if ($this->_mutex instanceof FileMutex) {
throw new InvalidConfigException('You must config mutex in the application component, except the FileMutex.');
}

return $this->withoutOverlapping();
}

/**
* Register a callback to further filter the schedule.
*
Expand Down

0 comments on commit e4d958f

Please sign in to comment.