-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Moved all listeners from Scheduler into separate listener classes
Fix for bug: SchedulerEvent::EVENT_INTERNAL_KERNEL_START is triggered multiple times Fix for bug: IpcServer does not read messages from workers Fix for bug: Scheduler catches all exceptions and ignores them silently
- Loading branch information
Showing
26 changed files
with
199 additions
and
151 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
34 changes: 34 additions & 0 deletions
34
src/Zeus/Kernel/Scheduler/Listener/KernelLoopGenerator.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
<?php | ||
|
||
namespace Zeus\Kernel\Scheduler\Listener; | ||
|
||
use Zeus\Kernel\Scheduler\SchedulerEvent; | ||
use Zeus\Kernel\Scheduler\WorkerEvent; | ||
use Zeus\Kernel\SchedulerInterface; | ||
|
||
class KernelLoopGenerator | ||
{ | ||
public function __invoke(WorkerEvent $event) | ||
{ | ||
if (!$event->getParam(SchedulerInterface::WORKER_SERVER) || $event->getParam(SchedulerInterface::WORKER_INIT)) { | ||
return; | ||
} | ||
|
||
$scheduler = $event->getScheduler(); | ||
$reactor = $scheduler->getReactor(); | ||
|
||
$terminator = function() use ($reactor, $scheduler) { | ||
$event = $scheduler->getSchedulerEvent(); | ||
$event->setName(SchedulerEvent::INTERNAL_EVENT_KERNEL_LOOP); | ||
$scheduler->getEventManager()->triggerEvent($event); | ||
if ($scheduler->isTerminating()) { | ||
$reactor->setTerminating(true); | ||
} | ||
}; | ||
do { | ||
$reactor->mainLoop( | ||
$terminator | ||
); | ||
} while (!$scheduler->isTerminating()); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
51 changes: 51 additions & 0 deletions
51
src/Zeus/Kernel/Scheduler/Listener/SchedulerTerminateListener.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
<?php | ||
|
||
namespace Zeus\Kernel\Scheduler\Listener; | ||
|
||
use Zeus\Kernel\Scheduler\Exception\SchedulerException; | ||
use Zeus\Kernel\Scheduler\SchedulerEvent; | ||
use Zeus\Kernel\Scheduler\Status\WorkerState; | ||
|
||
class SchedulerTerminateListener extends AbstractWorkerLifeCycleListener | ||
{ | ||
public function __invoke(SchedulerEvent $event) | ||
{ | ||
$scheduler = $event->getScheduler(); | ||
$scheduler->getLogger()->debug("Stopping scheduler"); | ||
$fileName = $this->getUidFile($event->getScheduler()->getConfig()); | ||
|
||
$uid = @file_get_contents($fileName); | ||
if (!$uid) { | ||
throw new SchedulerException("Scheduler not running", SchedulerException::SCHEDULER_NOT_RUNNING); | ||
} | ||
|
||
$uid = intval($uid); | ||
|
||
$scheduler->getLogger()->info("Terminating scheduler process #$uid"); | ||
/** @var WorkerState $worker */ | ||
$worker = $event->getTarget(); | ||
$worker->setUid($uid); | ||
$worker->setProcessId($uid); | ||
|
||
$globalCount = 0; | ||
foreach ([true, false] as $isSoftStop) { | ||
$this->workerLifeCycle->stop($worker, $isSoftStop); | ||
|
||
$count = 0; | ||
while (($uid = @file_get_contents($fileName)) && $count < 5) { | ||
if ($globalCount > 3 && $globalCount % 2) { | ||
$scheduler->getLogger()->debug("Waiting for scheduler to shutdown"); | ||
} | ||
sleep(1); | ||
$count++; | ||
$globalCount++; | ||
} | ||
|
||
if (!$uid) { | ||
return; | ||
} | ||
} | ||
|
||
throw new SchedulerException("Scheduler not stopped", SchedulerException::LOCK_FILE_ERROR); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
<?php | ||
|
||
namespace Zeus\Kernel\Scheduler\Listener; | ||
|
||
use Zeus\Kernel\Scheduler\WorkerEvent; | ||
|
||
class WorkerInitListener | ||
{ | ||
public function __invoke(WorkerEvent $event) | ||
{ | ||
$eventManager = $event->getScheduler()->getEventManager(); | ||
$statusSender = new WorkerStatusSender(); | ||
$events[] = $eventManager->attach(WorkerEvent::EVENT_RUNNING, $statusSender, WorkerEvent::PRIORITY_FINALIZE + 1); | ||
$events[] = $eventManager->attach(WorkerEvent::EVENT_WAITING, $statusSender, WorkerEvent::PRIORITY_FINALIZE + 1); | ||
$events[] = $eventManager->attach(WorkerEvent::EVENT_EXIT, $statusSender, WorkerEvent::PRIORITY_FINALIZE + 2); | ||
} | ||
} |
Oops, something went wrong.