mirror of
https://gitee.com/ulthon/ulthon_admin.git
synced 2026-07-01 15:32:48 +08:00
109 lines
3.1 KiB
PHP
109 lines
3.1 KiB
PHP
<?php
|
|
|
|
namespace base\common\service;
|
|
|
|
use app\common\model\VirtualModel;
|
|
|
|
class TimerServiceBase
|
|
{
|
|
protected $taskList = null;
|
|
|
|
public static function generateAllTaskInstanceList()
|
|
{
|
|
$config_list = include app_file_path('common/command/timer/config.php');
|
|
$task_list = [];
|
|
foreach ($config_list as $config_item) {
|
|
if ($config_item['name'] == 'http_demo' && !env('adminsystem.is_demo', false)) {
|
|
continue;
|
|
}
|
|
|
|
$task_list = array_merge($task_list, static::generateTaskInstanceFromConfig($config_item));
|
|
}
|
|
|
|
return $task_list;
|
|
}
|
|
|
|
public function generateAllRequestList()
|
|
{
|
|
if ($this->taskList === null) {
|
|
$this->taskList = static::generateAllTaskInstanceList();
|
|
}
|
|
|
|
$request_list = array_filter($this->taskList, function ($item) {
|
|
return $item['type'] == 'site';
|
|
});
|
|
|
|
return $request_list;
|
|
}
|
|
|
|
public function generateAllCallList()
|
|
{
|
|
if ($this->taskList === null) {
|
|
$this->taskList = static::generateAllTaskInstanceList();
|
|
}
|
|
|
|
$request_list = array_filter($this->taskList, function ($item) {
|
|
return $item['type'] == 'call';
|
|
});
|
|
|
|
return $request_list;
|
|
}
|
|
|
|
public static function generateTaskInstanceFromConfig($config_item)
|
|
{
|
|
$config_item = static::initConfigItem($config_item);
|
|
$request_list = [];
|
|
$concurrency = $config_item['concurrency'];
|
|
|
|
for ($i = 0; $i < $concurrency; $i++) {
|
|
$target = $config_item['target'];
|
|
$params = [
|
|
'concurrency_id' => $i,
|
|
'concurrency_count' => $concurrency,
|
|
];
|
|
// 处理target
|
|
if ($config_item['type'] == 'site') {
|
|
$target_info = parse_url($target);
|
|
$query_params = [];
|
|
if (isset($target_info['query'])) {
|
|
parse_str($target_info['query'], $query_params);
|
|
}
|
|
$query_params = array_merge($query_params, $params);
|
|
$target_info['query'] = http_build_query($query_params);
|
|
$target = unparse_url($target_info);
|
|
} elseif ($config_item['type'] == 'call') {
|
|
$target = $config_item['target'];
|
|
}
|
|
|
|
$new_config_item = clone $config_item;
|
|
$new_config_item['target'] = $target;
|
|
$new_config_item['concurrency_id'] = $i;
|
|
|
|
$request_list[] = $new_config_item;
|
|
}
|
|
|
|
return $request_list;
|
|
}
|
|
|
|
public static function initConfigItem($config)
|
|
{
|
|
$default = [
|
|
'name' => 'http_demo',
|
|
'type' => 'site',
|
|
'target' => '',
|
|
'frequency' => 600,
|
|
'concurrency' => 1,
|
|
'run_type' => 'auto',
|
|
];
|
|
|
|
$data = array_merge($default, $config);
|
|
|
|
if ($data['frequency'] < 0) {
|
|
$data['frequency'] = 0;
|
|
}
|
|
$model_timer = new VirtualModel($data);
|
|
|
|
return $model_timer;
|
|
}
|
|
}
|