Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Enhance onOneServer & withoutOverlapping #43

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -163,6 +163,7 @@ Below shows the redis mutex demo:
'components' => [
'mutex' => [
'class' => 'yii\redis\Mutex',
'autoRelease' => false, // You should disable autoRelease here
'redis' => [
'hostname' => 'localhost',
'port' => 6379,
Expand Down
5 changes: 3 additions & 2 deletions src/CallbackEvent.php
Original file line number Diff line number Diff line change
Expand Up @@ -69,18 +69,19 @@ public function run(Application $app)
/**
* Do not allow the event to overlap each other.
*
* @param int $expiresAt
* @return $this
* @throws InvalidParamException
*/
public function withoutOverlapping()
public function withoutOverlapping($expiresAt = 1440)
{
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();
return parent::withoutOverlapping($expiresAt);
}

/**
Expand Down
20 changes: 14 additions & 6 deletions src/Event.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
use yii\base\InvalidConfigException;
use yii\mail\MailerInterface;
use yii\mutex\Mutex;
use yii\mutex\FileMutex;
use yii\redis\Mutex as RedisMutex;

/**
* Class Event
Expand Down Expand Up @@ -508,10 +508,14 @@ public function user($user)
/**
* Do not allow the event to overlap each other.
*
* @param int $expiresAt
* @return $this
*/
public function withoutOverlapping()
public function withoutOverlapping($expiresAt = 1440)
{
if ($this->_mutex instanceof RedisMutex) {
$this->_mutex->expire = $expiresAt * 60;
}
return $this->then(function() {
$this->_mutex->release($this->mutexName());
})->skip(function() {
Expand All @@ -526,11 +530,15 @@ public function withoutOverlapping()
*/
public function onOneServer()
{
if ($this->_mutex instanceof FileMutex) {
throw new InvalidConfigException('You must config mutex in the application component, except the FileMutex.');
if (!$this->_mutex instanceof RedisMutex) {
throw new InvalidConfigException('You must config redis mutex in the application component.');
}

return $this->withoutOverlapping();
$time = new \DateTime('now');
$name = $this->mutexName() . $time->format('Hi');
$this->_mutex->expire = 3600;
return $this->skip(function() use ($name) {
return !$this->_mutex->acquire($name);
});
}

/**
Expand Down