Skip to content

Commit

Permalink
fix worker
Browse files Browse the repository at this point in the history
  • Loading branch information
dp-ifacesoft committed Oct 2, 2020
1 parent d292307 commit cb4442f
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 12 deletions.
18 changes: 9 additions & 9 deletions source/Ice/Core/Action/Worker.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

namespace Ice\Core;

use Ebs\DataProvider\Redis_Twins;
use Ice\DataProvider\Cli;
use Ice\DataProvider\Redis;
use Ice\Exception\Config_Error;
Expand Down Expand Up @@ -124,6 +123,7 @@ private function getWorkerKey($async = null)
/**
* @param array $dispatchWorker
* @param array $params
* @return array|mixed|null
* @throws Config_Error
* @throws Error
* @throws Exception
Expand Down Expand Up @@ -170,30 +170,30 @@ private function dispatch(array $dispatchWorker, array $params)

usleep((int)$worker['delay']);

while (count($provider->getKeys($this->getTaskKey($workerKey))) >= (int)$worker['max'] && (int)$worker['max'] !== 0) {
while (($activeTasks = count($provider->getKeys($this->getTaskKey($workerKey)))) >= (int)$worker['max'] && (int)$worker['max'] !== 0) {
Logger::log('worker wait... (' . $activeTasks . '/' . $worker['max'] . ')', get_class($this));
usleep((int)$worker['delay']);
}

$task = array_merge($params, $task);

$hash = crc32(Json::encode($task));

$provider->hSet($this->getTaskKey($workerKey, $hash), ['started_at' => time(), 'task' => $task], $worker['ttl']);

/** @var Action_Worker $class */
$class = get_class($this);

// $this->getLogger()->info($taskCount . '/' . $totalTasks . ': #' . $hash . ' ' . Type_String::printR($task));

try {
$avgTime = round((microtime(true) - $startTime) / $i, 3);
$estimateTime = Profiler::getPrettyTime(microtime(true) - $worker['started_at']);
$leftTime = Profiler::getPrettyTime(($dispatchWorker['tasks'] - $i) * $avgTime);
$perSec = round(1 / $avgTime, 3);

$taskLog = Type_String::printR($task, false);

Logger::log(get_class($this) . ' (' . $i . '/' . $dispatchWorker['tasks'] . ') #' . $hash . ' ' . $taskLog . ' [perSec: ' . $perSec . ' avg: ' . $avgTime . ' left: ' . $leftTime . ']', 'job');
Logger::log('[ '. $i . '/' . $dispatchWorker['tasks'] .' : ' . ($dispatchWorker['tasks'] - $i) . ' ] #' . $hash .' ' . $taskLog . ' [left: ' . $leftTime . ']', get_class($this));
$runLog = '[ ' . $i . '/' . $dispatchWorker['tasks'] . ' (' . ($dispatchWorker['tasks'] - $i) . ') - ' . $activeTasks . '/' . $worker['max'] . ' : ' . $estimateTime . ']';

Logger::log($runLog . ' #' . $hash .' ' . $taskLog . ' [ timePerTask: ' . $avgTime . ' | tasksPerSec: ' . $perSec . ' | leftTime: ' . $leftTime . ' ]', get_class($this));

$class::call(['workerKey' => $workerKey, 'hash' => $hash, 'task' => $taskLog], 0, $bg);
} catch (\Exception $e) {
$this->getLogger()->error(['Worker {$0}: Task #{$1} failed - {$2}', [get_class($this), $hash, Type_String::printR($task)]], __FILE__, __LINE__, $e);
Expand All @@ -203,7 +203,7 @@ private function dispatch(array $dispatchWorker, array $params)
}

$worker['finish_datetime'] = Date::get();
$worker['time'] = (microtime(true) - $worker['started_at']) . ' ms.';
$worker['time'] = Profiler::getPrettyTime(microtime(true) - $worker['started_at']);

$this->getLogger()->info('Worker ' . get_class($this) . ' complete! ' . Type_String::printR($worker));

Expand Down
6 changes: 3 additions & 3 deletions source/Ice/Helper/Profiler.php
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ public static function getPrettyTime($time)
{
$seconds = (int)$time;

$miliseconds = round(($time - $seconds) * 1000, 3);
$miliseconds = round(($time - $seconds) * 1000, 0);

$diff = (new DateTime('@0'))->diff(new DateTime("@$seconds"));

Expand All @@ -90,11 +90,11 @@ public static function getPrettyTime($time)
}

if ($diff->format('%i')) {
$date .= $diff->format(' %i minutes');
$date .= $diff->format(' %i min.');
}

if ($diff->format('%s')) {
$date .= $diff->format(' %s seconds');
$date .= $diff->format(' %s sec.');
}

return trim($date . ' ' . $miliseconds . ' ms');
Expand Down

0 comments on commit cb4442f

Please sign in to comment.