Skip to content

Commit

Permalink
Fine-tune test case and expose looper for periodic testdata submit
Browse files Browse the repository at this point in the history
  • Loading branch information
steven.chiu committed Jun 4, 2019
1 parent dd0e011 commit e4c9990
Show file tree
Hide file tree
Showing 3 changed files with 89 additions and 110 deletions.
21 changes: 14 additions & 7 deletions src/JobScheduler.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace TimeWorker;

include_once __DIR__.'/../vendor/autoload.php';
include_once __DIR__ . '/RedisFactory.php';

use React\EventLoop\Factory;
Expand Down Expand Up @@ -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);
Expand All @@ -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');
}
Expand All @@ -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);
}
}
Expand Down Expand Up @@ -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) {
Expand Down
36 changes: 12 additions & 24 deletions test/job_test.php
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand All @@ -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);
Expand All @@ -68,6 +55,7 @@ function testAddJob($redis)
$manager->addJob($redis, json_encode($command), $timeDelay, "testId", $intent, 0, $commandNo);
}
}
$redis->close();
echo "======================================\n";
}

Expand Down
142 changes: 63 additions & 79 deletions test/script.json
Original file line number Diff line number Diff line change
@@ -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
}
]
}
}

0 comments on commit e4c9990

Please sign in to comment.