From e4c9990c22a6737944b2f991015ee1fe78bfd8e9 Mon Sep 17 00:00:00 2001 From: "steven.chiu" Date: Tue, 4 Jun 2019 17:10:56 +0800 Subject: [PATCH] Fine-tune test case and expose looper for periodic testdata submit --- src/JobScheduler.php | 21 ++++--- test/job_test.php | 36 ++++------- test/script.json | 142 +++++++++++++++++++------------------------ 3 files changed, 89 insertions(+), 110 deletions(-) diff --git a/src/JobScheduler.php b/src/JobScheduler.php index 7ea951c..444fbc1 100644 --- a/src/JobScheduler.php +++ b/src/JobScheduler.php @@ -2,6 +2,7 @@ namespace TimeWorker; +include_once __DIR__.'/../vendor/autoload.php'; include_once __DIR__ . '/RedisFactory.php'; use React\EventLoop\Factory; @@ -47,14 +48,20 @@ public function __construct( $this->subPattern = '__keyspace@' . $db . '__:'; $this->executableTime = $executableTime; $this->pollingInterval = $pollingInterval; - $this->forkChildCmd = "php -r \"include '" . __FILE__ . "'; business\ilife\JobScheduler::fork(1, '$host', $port, $db);\""; + $this->forkChildCmd = "php -r \"include '" . __FILE__ . "'; TimeWorker\JobScheduler::fork(1, '$host', $port, $db);\""; + + $looper = Factory::create(); + $this->looper = $looper; + } + + public function getLooper() + { + return $this->looper; } public function run(callable $cb = null) { $this->cb = $cb; - $looper = Factory::create(); - $this->looper = $looper; try { $redisConnect = getRedisConn($this->redisConf); @@ -71,9 +78,9 @@ public function run(callable $cb = null) } if ($this->apiIndex == 0) { - $this->performJobScheduler($redisConnect, $looper); + $this->performJobScheduler($redisConnect, $this->looper); } elseif ($this->apiIndex == 1) { - $this->monitorExpireKey($this->redisConf, $looper); + $this->monitorExpireKey($this->redisConf, $this->looper); } else { throw new Exception('Invalid api index'); } @@ -82,7 +89,7 @@ public function run(callable $cb = null) public function runProcess($cmd) { $process = new \React\ChildProcess\Process($cmd); - if (strpos($_SERVER['OS'], 'Windows') === false) { + if (!isset($_SERVER['OS']) || strpos($_SERVER['OS'], 'Windows') === false) { $process->start($this->looper); } } @@ -133,7 +140,7 @@ public function performJobScheduler($redis, $looper) $looper->stop(); }); - if (strpos($_SERVER['OS'], 'Windows') === false) { + if (!isset($_SERVER['OS']) || strpos($_SERVER['OS'], 'Windows') === false) { $process = new \React\ChildProcess\Process($this->forkChildCmd); $process->start($looper); $process->stdout->on('data', function ($data) use (&$redis, &$manager, &$looper) { diff --git a/test/job_test.php b/test/job_test.php index 65ed0f0..ef11efd 100644 --- a/test/job_test.php +++ b/test/job_test.php @@ -12,35 +12,21 @@ const VALID_INTENTS = ['morning', 'bye', 'imhome', 'night']; -if ($argc > 1) { - list($SELF, $isJobSubmitter) = $argv; -} - -$looper = Factory::create(); -if (!isset($isJobSubmitter)) { - if (strpos($_SERVER['OS'], 'Windows') === false) { - $process = new \React\ChildProcess\Process("php job_test.php true"); - $process->start($this->looper); - } else { - init($redisConf, isset($isJobSubmitter)? $isJobSubmitter: true); - } -} init($redisConf, isset($isJobSubmitter)? $isJobSubmitter: false); function init($redisConf, $isJobSubmitter = false) { global $db; try { - $redisConnect = getRedisConn($redisConf); - if ($isJobSubmitter == "true") { - testAddJob($redisConnect); - } else { - $scheduler = new JobScheduler(0, $redisConf['host'], $redisConf['port'], $db, 180, 10); - $scheduler->run(function ($key, $value, $way) { - $wayStr = $way == 0 ? 'polling' : 'notification'; - echo "Job is ready: key: $key value: $value [$wayStr]\n"; - }); - } + $scheduler = new JobScheduler(0, $redisConf['host'], $redisConf['port'], $db, 180, 30); + testAddJob($redisConf); + $scheduler->getLooper()->addPeriodicTimer(20, function () use ($redisConf) { + testAddJob($redisConf); + }); + $scheduler->run(function ($key, $value, $way) { + $wayStr = $way == 0 ? 'polling' : 'notification'; + echo "Job is ready: key: $key value: $value [$wayStr]\n"; + }); } catch (\RedisException $exception) { echo "Exception:$exception\n"; echo "Prepare to quit...\n"; @@ -52,8 +38,9 @@ function init($redisConf, $isJobSubmitter = false) } } -function testAddJob($redis) +function testAddJob($redisConf) { + $redis = getRedisConn($redisConf); echo "================AddJob================\n"; $strJsonFileContents = file_get_contents(__DIR__.'/'."script.json"); $array = json_decode($strJsonFileContents); @@ -68,6 +55,7 @@ function testAddJob($redis) $manager->addJob($redis, json_encode($command), $timeDelay, "testId", $intent, 0, $commandNo); } } + $redis->close(); echo "======================================\n"; } diff --git a/test/script.json b/test/script.json index 2160215..f04b6c4 100644 --- a/test/script.json +++ b/test/script.json @@ -1,91 +1,75 @@ { "morning": { "enabled": true, - "commands": [{ - "type": 1, - "text": "播放 Maroon 5 的 Sunday Morning", - "input": "Maroon 5 的 Sunday Morning", - "duration": 5 - }, { - "type": 5, - "text": "打開房間的燈", - "input": "打開房間的燈" - }, { - "type": 3, - "text": "今天天氣", - "input": "今天天氣" - }] + "commands": [ + { + "type": 1, + "text": "播放 Maroon 5 的 Sunday Morning", + "input": "Maroon 5 的 Sunday Morning", + "duration": 5 + }, + { + "type": 5, + "text": "打開房間的燈", + "input": "打開房間的燈" + }, + { + "type": 3, + "text": "今天天氣", + "input": "今天天氣" + } + ] }, "bye": { "enabled": true, - "commands": [{ - "type": 5, - "text": "關掉房間的燈", - "input": "關掉房間的燈" - }, { - "type": 5, - "text": "關掉房間的電扇", - "input": "關掉房間的電扇" - }] + "commands": [ + { + "type": 5, + "text": "關掉房間的燈", + "input": "關掉房間的燈" + }, + { + "type": 5, + "text": "關掉房間的電扇", + "input": "關掉房間的電扇" + } + ] }, "imhome": { "enabled": true, - "commands": [{ - "type": 5, - "text": "打開房間的燈", - "input": "打開房間的燈" - }, { - "type": 5, - "text": "打開房間的電扇", - "input": "打開房間的電扇" - }, { - "type": 1, - "text": "播放華語流行音樂", - "input": "華語流行音樂", - "duration": 120 - }] + "commands": [ + { + "type": 5, + "text": "打開房間的燈", + "input": "打開房間的燈" + }, + { + "type": 5, + "text": "打開房間的電扇", + "input": "打開房間的電扇" + }, + { + "type": 1, + "text": "播放華語流行音樂", + "input": "華語流行音樂", + "duration": 12 + } + ] }, "night": { "enabled": true, - "commands": [{ - "type": 5, - "text": "關閉房間的燈", - "input": "關閉房間的燈" - }, { - "type": 1, - "text": "播放舒眠音樂", - "input": "舒眠音樂", - "duration": 120 - }] - }, - "__examples__": [{ - "type": 1, - "text": "播放 Maroon 5 的 Sunday Morning", - "input": "Maroon 5 的 Sunday Morning", - "duration": 24 - }, { - "type": 2, - "text": "播放 ICRT", - "input": "ICRT", - "duration": 180 - }, { - "type": 3, - "text": "今天天氣", - "input": "今天天氣" - }, { - "type": 4, - "text": "播報頭條新聞", - "input": "頭條新聞", - "duration": 6 - }, { - "type": 5, - "text": "打開房間的燈", - "input": "打開房間的燈" - }, { - "type": 6, - "text": "告訴我金牛座本週運勢", - "input": "告訴我金牛座本週運勢", - "mute": false, - "duration": 6 - }] -} + "commands": [ + { + "type": 5, + "text": "關閉房間的燈", + "input": "關閉房間的燈" + }, + { + "type": 1, + "text": "播放舒眠音樂", + "input": "舒眠音樂", + "duration": 12 + } + ] + } +} \ No newline at end of file