Skip to content

Commit f4d6b31

Browse files
committed
Refactor HungNG_CI_Base_Queue_Worker
1 parent d2c15b9 commit f4d6b31

File tree

1 file changed

+28
-24
lines changed

1 file changed

+28
-24
lines changed

hungng/HungNG_CI_Base_Queue_Worker.php

Lines changed: 28 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -184,7 +184,7 @@ class HungNG_CI_Base_Queue_Worker extends HungNG_CI_Base_Controllers
184184
public function __construct()
185185
{
186186
// CLI only
187-
if (php_sapi_name() != "cli") {
187+
if (PHP_SAPI !== "cli") {
188188
die('Access denied');
189189
}
190190

@@ -212,15 +212,15 @@ public function listen()
212212
}
213213

214214
// Pre-work check
215-
if (!method_exists($this, 'handleListen'))
216-
throw new Exception("You need to declare `handleListen()` method in your worker controller.", 500);
217-
if (!method_exists($this, 'handleWork'))
218-
throw new Exception("You need to declare `handleWork()` method in your worker controller.", 500);
219-
if ($this->logPath && !file_exists($this->logPath)) {
220-
// Try to access or create log file
221-
if ($this->_log('')) {
222-
throw new Exception("Log file doesn't exist: `" . $this->logPath . "`.", 500);
223-
}
215+
if (!method_exists($this, 'handleListen')) {
216+
throw new \RuntimeException("You need to declare `handleListen()` method in your worker controller.", 500);
217+
}
218+
if (!method_exists($this, 'handleWork')) {
219+
throw new \RuntimeException("You need to declare `handleWork()` method in your worker controller.", 500);
220+
}
221+
// Try to access or create log file
222+
if ($this->logPath && !file_exists($this->logPath) && $this->_log('')) {
223+
throw new \RuntimeException("Log file doesn't exist: `" . $this->logPath . "`.", 500);
224224
}
225225

226226
// INI setting
@@ -233,8 +233,8 @@ public function listen()
233233
// Worker command builder
234234
// Be careful to avoid infinite loop by opening listener itself
235235
$workerAction = 'work';
236-
$route = $this->router->fetch_directory() . $this->router->fetch_class() . "/{$workerAction}";
237-
$workerCmd = "{$this->phpCommand} " . FCPATH . "index.php {$route}";
236+
$route = $this->router->fetch_directory() . $this->router->fetch_class() . "/" . $workerAction;
237+
$workerCmd = $this->phpCommand . " " . FCPATH . "index.php " . $route;
238238

239239
// Static variables
240240
$startTime = 0;
@@ -333,8 +333,9 @@ public function listen()
333333
public function work($id = 1)
334334
{
335335
// Pre-work check
336-
if (!method_exists($this, 'handleWork'))
337-
throw new Exception("You need to declare `handleWork()` method in your worker controller.", 500);
336+
if (!method_exists($this, 'handleWork')) {
337+
throw new \RuntimeException("You need to declare `handleWork()` method in your worker controller.", 500);
338+
}
338339

339340
// INI setting
340341
if ($this->debug) {
@@ -439,8 +440,9 @@ public function launch($action = 'listen')
439440
public function single($force = false)
440441
{
441442
// Pre-work check
442-
if (!method_exists($this, 'handleSingle'))
443-
throw new Exception("You need to declare `handleSingle()` method in your worker controller.", 500);
443+
if (!method_exists($this, 'handleSingle')) {
444+
throw new \RuntimeException("You need to declare `handleSingle()` method in your worker controller.", 500);
445+
}
444446

445447
// Shared lock flag builder
446448
$lockFile = sys_get_temp_dir() . "/yidas-codeiginiter-queue-worker_" . str_replace('/', '_', $this->router->fetch_directory()) . get_called_class() . '.lock';
@@ -457,7 +459,7 @@ public function single($force = false)
457459

458460
// Start Single - Set identified lock
459461
// Close Single - Release identified lock
460-
register_shutdown_function(function() use ($lockFile) {
462+
register_shutdown_function(function () use ($lockFile) {
461463
@unlink($lockFile);
462464
});
463465

@@ -593,9 +595,11 @@ protected function _log($textLine, $logPath = null)
593595

594596
$logPath = ($logPath) ? $logPath : $this->logPath;
595597

596-
if ($logPath)
597-
return file_put_contents($logPath, $this->_formatTextLine($textLine), FILE_APPEND); else
598-
return false;
598+
if ($logPath) {
599+
return file_put_contents($logPath, $this->_formatTextLine($textLine), FILE_APPEND);
600+
}
601+
602+
return false;
599603
}
600604

601605
/**
@@ -633,9 +637,9 @@ protected function _isPidAlive($pid)
633637
{
634638
if (((function_exists('posix_getpgid') && posix_getpgid($pid)) || file_exists("/proc/{$pid}"))) {
635639
return true;
636-
} else {
637-
return false;
638640
}
641+
642+
return false;
639643
}
640644

641645
/**
@@ -648,9 +652,9 @@ protected function _isLinux()
648652
// Just make sure that it's not Windows
649653
if ((strtoupper(substr(PHP_OS, 0, 3)) === 'WIN')) {
650654
return false;
651-
} else {
652-
return true;
653655
}
656+
657+
return true;
654658
}
655659

656660
/**

0 commit comments

Comments
 (0)