11<?php
2- /**
3- * Created by PhpStorm.
4- * User: semenov
5- * Date: 08.07.14
6- * Time: 11:13
7- */
8-
92namespace yii2mod \cron \behaviors ;
103
114use yii \base \Behavior ;
3629 * @author Klimov Paul <klimov@zfort.com>
3730 * @author Dmitry Semenov <disemx@gmail.com>
3831 * @version $Id$
39- * @package zfort\mutex\behavior
32+ * @package yii2mod\cron\behaviors
4033 * @since 1.0
4134 */
4235class MutexConsoleCommandBehavior extends Behavior
@@ -49,11 +42,7 @@ class MutexConsoleCommandBehavior extends Behavior
4942 * @var array list of action names, which mutex should be applied to.
5043 */
5144 public $ mutexActions = array ();
52- /**
53- * @var integer exit code, which should be returned by console command in case it
54- * is terminated due to mutex lock.
55- */
56- public $ mutexExitCode = 100 ;
45+
5746
5847 /**
5948 * @inheritdoc
@@ -83,7 +72,7 @@ public function getMutex()
8372 */
8473 protected function composeMutexName ($ action )
8574 {
86- return $ this ->getOwner ()-> getName () . '- ' . $ action ;
75+ return $ this ->owner -> getUniqueId () . '- ' . $ action ;
8776 }
8877
8978 /**
@@ -98,35 +87,32 @@ public function checkIsMutexAction($action)
9887 return in_array (strtolower ($ action ), $ this ->mutexActions );
9988 }
10089
90+
10191 /**
102- * Responds to {@link CConsoleCommand::onBeforeAction} event.
103- * Override this method and make it public if you want to handle the corresponding event of the {@link CBehavior::owner owner}.
104- *
105- * @param CConsoleCommandEvent $event event parameter
92+ * @param $event
93+ * @return bool
10694 */
10795 public function beforeAction ($ event )
10896 {
109- if ($ this ->checkIsMutexAction ($ event ->action )) {
110- $ mutexName = $ this ->composeMutexName ($ event ->action );
97+ if ($ this ->checkIsMutexAction ($ event ->action -> id )) {
98+ $ mutexName = $ this ->composeMutexName ($ event ->action -> id );
11199 if (!$ this ->getMutex ()->acquire ($ mutexName )) {
112100 echo "Execution terminated: command is already running. \n" ;
113- $ event ->stopCommand = true ;
114- $ event -> exitCode = $ this -> mutexExitCode ;
101+ $ event ->isValid = false ;
102+ return false ;
115103 }
116104 }
117105 }
118106
107+
119108 /**
120- * Responds to {@link CConsoleCommand::onAfterAction} event.
121- * Override this method and make it public if you want to handle the corresponding event of the {@link CBehavior::owner owner}.
122- *
123- * @param CConsoleCommandEvent $event event parameter
109+ * @param $event
124110 */
125111 public function afterAction ($ event )
126112 {
127- if ($ this ->checkIsMutexAction ($ event ->action )) {
128- $ mutexName = $ this ->composeMutexName ($ event ->action );
113+ if ($ this ->checkIsMutexAction ($ event ->action -> id )) {
114+ $ mutexName = $ this ->composeMutexName ($ event ->action -> id );
129115 $ this ->getMutex ()->release ($ mutexName );
130116 }
131117 }
132- }
118+ }
0 commit comments