Skip to content

Commit

Permalink
fixed some bugs
Browse files Browse the repository at this point in the history
  • Loading branch information
ppanphper committed Oct 31, 2019
1 parent 92a282a commit 2aacdd4
Show file tree
Hide file tree
Showing 5 changed files with 35 additions and 29 deletions.
14 changes: 7 additions & 7 deletions agent/Libs/DbLog.php
Original file line number Diff line number Diff line change
Expand Up @@ -173,13 +173,13 @@ private static function writeLog($logPrefix)
}
// 存储日志
if (!Logs::saveLog([
'task_id' => $originLog['taskId'],
'run_id' => $originLog['runId'],
'code' => $originLog['code'],
'title' => $originLog['title'],
'msg' => $msg,
'consume_time' => $originLog['consumeTime'],
'created' => $originLog['created'],
'taskId' => $originLog['taskId'],
'runId' => $originLog['runId'],
'code' => $originLog['code'],
'title' => $originLog['title'],
'msg' => $msg,
'consumeTime' => $originLog['consumeTime'],
'created' => $originLog['created'],
])) {
if (!isset($originLog['retryCount'])) {
$originLog['retryCount'] = 0;
Expand Down
40 changes: 23 additions & 17 deletions agent/Libs/LoadTasks.php
Original file line number Diff line number Diff line change
Expand Up @@ -114,28 +114,34 @@ public static function load($loadSpecifiedIds = [])
$offset = 0;
$limit = 1000;
$agentId = isset($agentInfo['id']) ? $agentInfo['id'] : 0;
while (true) {
$keepLoop = true;
while ($keepLoop) {
// 获取当前节点需要执行的任务数据
$tasks = Crontab::getCurrentNodeTaskData($agentId, $loadSpecifiedIds, Crontab::STATUS_ENABLED, $offset, $limit);
if (empty($tasks)) break;
foreach ($tasks as $task) {
// 如果节点加载任务数量没有超限
if (count(self::$_table) <= TASK_MAX_LOAD_SIZE) {
self::$_table->set($task['id'], [
'name' => $task['name'],
'rule' => $task['rule'],
'execNum' => $task['concurrency'],
'maxTime' => $task['max_process_time'],
'timeoutOpt' => $task['timeout_opt'],
'logOpt' => $task['log_opt'],
'runUser' => $task['run_user'],
'command' => $task['command'],
'retries' => $task['retries'],
'retryInterval' => $task['retry_interval'],
'noticeWay' => $task['notice_way'],
'updateTime' => $task['update_time'],
]);

if (count(self::$_table) > TASK_MAX_LOAD_SIZE) {
// 达到最大加载任务数,就终止加载
$keepLoop = false;
break;
}

// 如果内存不够,会出现 Swoole_table::set(): unable to allocate memory
self::$_table->set($task['id'], [
'name' => $task['name'],
'rule' => $task['rule'],
'execNum' => $task['concurrency'],
'maxTime' => $task['max_process_time'],
'timeoutOpt' => $task['timeout_opt'],
'logOpt' => $task['log_opt'],
'runUser' => $task['run_user'],
'command' => $task['command'],
'retries' => $task['retries'],
'retryInterval' => $task['retry_interval'],
'noticeWay' => $task['notice_way'],
'updateTime' => $task['update_time'],
]);
}
$offset += $limit;
}
Expand Down
2 changes: 1 addition & 1 deletion agent/Libs/Server.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

namespace Libs;

use Models\DB;
use \Models\DB;
use \Swoole\Server as SwooleServer;
use \Swoole\Process as SwooleProcess;

Expand Down
6 changes: 2 additions & 4 deletions agent/Models/DB.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,15 +14,13 @@ class DB
{
private static $_dbs = [];

protected static $_defaultDBName = null;

public static function init() {
self::getInstance();
self::$_defaultDBName = configItem('default_select_db');
}

/**
* 获取DB实例
* 注意,此方法调用在worker/process/task进程中,所以每个进程都会需要调用一次
*
* @param string $name
*
Expand All @@ -32,7 +30,7 @@ public static function init() {
public static function getInstance($name='')
{
// 默认数据库
$name = strtolower($name) ?: self::$_defaultDBName;
$name = strtolower($name) ?: configItem('default_select_db');;
if (isset(self::$_dbs[$name])) {
return self::$_dbs[$name];
}
Expand Down
2 changes: 2 additions & 0 deletions agent/agent.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@

define('ROOT_PATH', __DIR__ . '/');
define('LIBS_PATH', ROOT_PATH . 'Libs/');
define('MODELS_PATH', ROOT_PATH . 'Models/');
define('CONFIG_PATH', ROOT_PATH . 'Config/');
define('TPL_PATH', ROOT_PATH . 'Tpl/');
define('ENVIRONMENT', !empty($envInit['env']) ? $envInit['env'] : 'prod');
Expand Down Expand Up @@ -46,6 +47,7 @@
}
require_once $autoloadPath;
Loader::addNameSpace('Libs', LIBS_PATH);
Loader::addNameSpace('Models', MODELS_PATH);
Loader::register();
}

Expand Down

0 comments on commit 2aacdd4

Please sign in to comment.