TaskPHP是一个基于PHP的定时任务框架,它提供了一个简单、灵活且易于使用的解决方案,用于在PHP环境中执行定时任务。下面是对TaskPHP框架的简要介绍:
- 简单易用:TaskPHP的设计目标是让定时任务的创建和管理变得简单。通过简单的配置和编写任务代码,即可轻松实现定时任务的执行。
- 高性能:TaskPHP采用高效的任务调度算法,确保任务能够按照预定的时间准确执行。同时,它还支持并发执行多个任务,提高了任务的执行效率。
- 灵活配置:TaskPHP允许用户通过配置文件或代码来定义任务的执行时间、执行间隔、执行次数等参数。用户可以根据自己的需求灵活配置任务。
- 可扩展性:TaskPHP框架具有良好的扩展性,用户可以根据自己的需求添加自定义的任务类型或扩展功能。此外,它还支持与其他PHP框架或库进行集成,方便用户在现有项目中使用。
- 日志记录:TaskPHP提供了日志记录功能,可以记录任务的执行过程、执行结果以及错误信息等。这有助于用户监控任务的执行情况,及时发现问题并进行处理。
- 任务管理:框架提供了任务管理的功能,用户可以查看当前正在执行的任务、已完成的任务以及任务执行的历史记录。这有助于用户对任务进行监控和管理。
TaskPHP是一个功能强大、易于使用的PHP定时任务框架,适用于各种需要定时执行任务的场景。无论是简单的定时发送邮件、定时更新数据,还是复杂的定时爬虫、定时数据分析等任务,TaskPHP都能提供有效的解决方案。
关键环境感知任务记录
<?php
namespace cron;
use taskphp\Utils;
require 'taskDb.php';
class Service2
{
/*
* 关键环境安全-定时任务
* $service_id,定时任务服务路由ID
* */
public static function getTask($service_id)
{
global $CONF, $serviceArr;
$CONF = require '../conf/config.php';
require '../conf/config.service.php';
$serviceArr = getByServiceId($lock_service, $service_id);
Utils::log('Task-ID: ' . $service_id);
//数据信息
$school_id = $serviceArr['school_id'];
$school_type = $serviceArr['school_type'];
$school_attribute = $serviceArr['school_attribute'];
$school_short_name = $serviceArr['school_short_name'];
$service_group = $serviceArr['service_group'];
$service_protocol = $serviceArr['service_protocol'];
$service_url = $serviceArr['service_url'];
$url = $service_protocol . $service_url . '/service/page/sensor/simple.json';
$dataPramas = ["group_id" => $service_group];
$res = curlIotOnlyAPI($url, '1', '1000', $CONF['iot_token'], $dataPramas);
//Utils::log('Task-content: ' . $res);
$reArr = json_decode($res, true);
if ($reArr && $reArr["data"] && $reArr["data"]["data"] && $reArr["data"]["data"][0]) {
$keyData = $reArr["data"]["data"];
$environment_area = $reArr["data"]["data"][0]['device'];
//插入数据库;
Utils::db()->table("environment")->ifInsert(
array(
'task_code' => encodeId($school_id . '-' . $service_id),
'service_id' => $service_id,
'school_id' => $school_id,
'school_type' => $school_type,
'school_attribute' => $school_attribute,
'school_short_name' => $school_short_name,
'environment_area' => $environment_area,
'environment_temperature' => getSDataByPropertyType($keyData, '温度'),
'environment_temperature_state' => getStatusBySensorStatus($keyData, '温度'),
'environment_humidity' => getSDataByPropertyType($keyData, '湿度'),
'environment_humidity_state' => getStatusBySensorStatus($keyData, '湿度'),
'environment_air' => getSDataByPropertyType($keyData, '空质'),
'environment_air_state' => getStatusBySensorStatus($keyData, '空质'),
'environment_time' => time()
),
array(
'service_id' => $service_id,
'school_id' => $school_id,
'school_type' => $school_type,
'school_attribute' => $school_attribute,
'school_short_name' => $school_short_name,
'environment_area' => $environment_area,
'environment_temperature' => getSDataByPropertyType($keyData, '温度'),
'environment_temperature_state' => getStatusBySensorStatus($keyData, '温度'),
'environment_humidity' => getSDataByPropertyType($keyData, '湿度'),
'environment_humidity_state' => getStatusBySensorStatus($keyData, '湿度'),
'environment_air' => getSDataByPropertyType($keyData, '空质'),
'environment_air_state' => getStatusBySensorStatus($keyData, '空质'),
'environment_time' => time()
)
);
//系统日志
Utils::db()->table("task_log")->add(array(
"school_short_name" => $school_short_name,
"task_name" => '关键环境安全',
"task_status" => '服务路由ID(' . $service_id . ')API组ID(' . $service_group . ')-数据更新成功,STATUS:OK',
"task_time" => time()
));
} else {
//系统日志
Utils::db()->table("task_log")->add(array(
"school_short_name" => $school_short_name,
"task_name" => '关键环境安全',
"task_status" => 'API组ID:' . $service_group . '接口出错,无法爬取数据,STATUS:Fail',
"task_time" => time()
));
die();
}
//返回数据;
return true;
}
}
用电计量管理任务记录
<?php
namespace cron;
use taskphp\Utils;
require 'taskDb.php';
class Service3
{
/*
* 用电计量管理-定时任务
* $service_id,定时任务服务路由ID
* */
public static function getTask($service_id)
{
global $CONF, $serviceArr;
$CONF = require '../conf/config.php';
require '../conf/config.service.php';
$serviceArr = getByServiceId($lock_service, $service_id);
Utils::log('Task-ID: ' . $service_id);
//数据信息
$school_id = $serviceArr['school_id'];
$school_type = $serviceArr['school_type'];
$school_attribute = $serviceArr['school_attribute'];
$school_short_name = $serviceArr['school_short_name'];
$service_group = $serviceArr['service_group'];
$service_protocol = $serviceArr['service_protocol'];
$service_url = $serviceArr['service_url'];
$url = $service_protocol . $service_url . '/service/page/sensor/calc/data.json';
$dataPramas = ["group_id" => $service_group, 'propertyTypeVal' => '电量'];
$res = curlIotOnlyAPI($url, '1', '1000', $CONF['iot_token'], $dataPramas);
//Utils::log('Task-content: ' . $res);
$reArr = json_decode($res, true);
if ($reArr && $reArr["data"] && $reArr["data"]["data"] && $reArr["data"]["data"][0]) {
$powerData = $reArr["data"]["data"][0]["odata"];
$MonthsArr = generatePastMonths(13);//月份
$powerArr = [
formatNumber($powerData['prev_0_month_val_diff']),
formatNumber($powerData['prev_1_month_val_diff']),
formatNumber($powerData['prev_2_month_val_diff']),
formatNumber($powerData['prev_3_month_val_diff']),
formatNumber($powerData['prev_4_month_val_diff']),
formatNumber($powerData['prev_5_month_val_diff']),
formatNumber($powerData['prev_6_month_val_diff']),
formatNumber($powerData['prev_7_month_val_diff']),
formatNumber($powerData['prev_8_month_val_diff']),
formatNumber($powerData['prev_9_month_val_diff']),
formatNumber($powerData['prev_10_month_val_diff']),
formatNumber($powerData['prev_11_month_val_diff']),
formatNumber($powerData['prev_12_month_val_diff'])
];//对应月份的数据
//插入数据库;
/* Utils::db()->table("electricity")->ifInsert(
array(
'school_id' => $school_id,
'school_type' => $school_type,
'school_attribute' => $school_attribute,
'school_short_name' => $school_short_name,
'electricity_month' => $MonthsArr[0],
'electricity_norm' => $powerArr[0],
'electricity_time' => time()
),
array(
'school_type' => $school_type,
'school_attribute' => $school_attribute,
'school_short_name' => $school_short_name,
'electricity_norm' => $powerArr[0],
'electricity_time' => time()
)
);*/
for ($i = 0; $i < count($MonthsArr); $i++) {
$row_sec = Utils::db()->table("electricity")->where(array('school_id' => $school_id, 'electricity_month' => $MonthsArr[$i]))->limit(1)->order("school_id DESC")->select();
if (!$row_sec) {
//插入数据
Utils::db()->table("electricity")->add(
array(
'school_id' => $school_id,
'service_id' => $service_id,
'school_type' => $school_type,
'school_attribute' => $school_attribute,
'school_short_name' => $school_short_name,
'electricity_month' => $MonthsArr[$i],
'electricity_norm' => $powerArr[$i],
'electricity_time' => time()
));
} else {
//更新数据
Utils::db()->table("electricity")->where(array('school_id' => $school_id, 'electricity_month' => $MonthsArr[$i]))->update(
array(
'service_id' => $service_id,
'school_type' => $school_type,
'school_attribute' => $school_attribute,
'school_short_name' => $school_short_name,
'electricity_norm' => $powerArr[$i],
'electricity_time' => time()
));
}
}
//系统日志
Utils::db()->table("task_log")->add(array(
"school_short_name" => $school_short_name,
"task_name" => '用电计量管理',
"task_status" => '服务路由ID(' . $service_id . ')API组ID(' . $service_group . ')-数据更新成功,STATUS:OK',
"task_time" => time()
));
} else {
//系统日志
Utils::db()->table("task_log")->add(array(
"school_short_name" => $school_short_name,
"task_name" => '用电计量管理',
"task_status" => 'API组ID:' . $service_group . '接口出错,无法爬取数据,STATUS:Fail',
"task_time" => time()
));
die();
}
//返回数据;
return true;
}
}
用水计量管理任务记录
<?php
namespace cron;
use taskphp\Utils;
require 'taskDb.php';
class Service4
{
/*
* 用水计量管理-定时任务
* $service_id,定时任务服务路由ID
* */
public static function getTask($service_id)
{
global $CONF, $serviceArr;
$CONF = require '../conf/config.php';
require '../conf/config.service.php';
$serviceArr = getByServiceId($lock_service, $service_id);
Utils::log('Task-ID: ' . $service_id);
//数据信息
$school_id = $serviceArr['school_id'];
$school_type = $serviceArr['school_type'];
$school_attribute = $serviceArr['school_attribute'];
$school_short_name = $serviceArr['school_short_name'];
$service_group = $serviceArr['service_group'];
$service_protocol = $serviceArr['service_protocol'];
$service_url = $serviceArr['service_url'];
$url = $service_protocol . $service_url . '/service/page/sensor/calc/data.json';
$dataPramas = ["group_id" => $service_group, 'propertyTypeVal' => '总流量'];
$res = curlIotOnlyAPI($url, '1', '1000', $CONF['iot_token'], $dataPramas);
//Utils::log('Task-content: ' . $res);
$reArr = json_decode($res, true);
if ($reArr && $reArr["data"] && $reArr["data"]["data"] && $reArr["data"]["data"][0]) {
$powerData = $reArr["data"]["data"][0]["odata"];
$MonthsArr = generatePastMonths(13);//月份
$powerArr = [
formatNumber($powerData['prev_0_month_val_diff']),
formatNumber($powerData['prev_1_month_val_diff']),
formatNumber($powerData['prev_2_month_val_diff']),
formatNumber($powerData['prev_3_month_val_diff']),
formatNumber($powerData['prev_4_month_val_diff']),
formatNumber($powerData['prev_5_month_val_diff']),
formatNumber($powerData['prev_6_month_val_diff']),
formatNumber($powerData['prev_7_month_val_diff']),
formatNumber($powerData['prev_8_month_val_diff']),
formatNumber($powerData['prev_9_month_val_diff']),
formatNumber($powerData['prev_10_month_val_diff']),
formatNumber($powerData['prev_11_month_val_diff']),
formatNumber($powerData['prev_12_month_val_diff'])
];
//插入数据库;
for ($i = 0; $i < count($MonthsArr); $i++) {
$row_sec = Utils::db()->table("water")->where(array('school_id' => $school_id, 'water_month' => $MonthsArr[$i]))->limit(1)->order("school_id DESC")->select();
if (!$row_sec) {
//插入数据
Utils::db()->table("water")->add(
array(
'school_id' => $school_id,
'service_id' => $service_id,
'school_type' => $school_type,
'school_attribute' => $school_attribute,
'school_short_name' => $school_short_name,
'water_month' => $MonthsArr[$i],
'water_norm' => $powerArr[$i],
'water_time' => time()
));
} else {
//更新数据
Utils::db()->table("water")->where(array('school_id' => $school_id, 'water_month' => $MonthsArr[$i]))->update(
array(
'service_id' => $service_id,
'school_type' => $school_type,
'school_attribute' => $school_attribute,
'school_short_name' => $school_short_name,
'water_norm' => $powerArr[$i],
'water_time' => time()
));
}
}
//系统日志
Utils::db()->table("task_log")->add(array(
"school_short_name" => $school_short_name,
"task_name" => '用水计量管理',
"task_status" => '服务路由ID(' . $service_id . ')API组ID(' . $service_group . ')-数据更新成功,STATUS:OK',
"task_time" => time()
));
} else {
//系统日志
Utils::db()->table("task_log")->add(array(
"school_short_name" => $school_short_name,
"task_name" => '用水计量管理',
"task_status" => 'API组ID:' . $service_group . '接口出错,无法爬取数据,STATUS:Fail',
"task_time" => time()
));
die();
}
//返回数据;
return true;
}
}
记录日志
使用第三方爬中仅记录启动和结束任务日志
[2024-03-29 20:22:54.83456200][DEBUG]:distribute_listen daemon pid:383131 Start
[2024-03-29 20:22:55.83205600][DEBUG]:worker_listen daemon pid:383134 Start
[2024-03-29 20:22:56.83395000][DEBUG]:worker_listen daemon pid:383135 Start
[2024-03-29 20:22:57.83637100][DEBUG]:worker_listen daemon pid:383136 Start
[2024-03-29 20:22:58.83940500][DEBUG]:worker_listen daemon pid:383137 Start
[2024-03-29 20:22:59.83934000][DEBUG]:worker_listen daemon pid:383138 Start
[2024-03-29 20:23:00.84222100][DEBUG]:worker_listen daemon pid:383139 Start
[2024-03-29 20:23:01.84334700][DEBUG]:worker_listen daemon pid:383140 Start
[2024-03-29 20:23:02.84479600][DEBUG]:worker_listen daemon pid:383141 Start
[2024-03-29 20:30:01.04360000][DEBUG]:cron\Task14::run [--START--]
[2024-03-29 20:30:01.04501700][DEBUG]:Service-ID: 31
[2024-03-29 20:30:07.56411000][DEBUG]:cron\Task14::run [--END--][RunTime:6.520488s]
[2024-03-29 20:31:01.07364600][DEBUG]:cron\Task15::run [--START--]
[2024-03-29 20:31:01.07523800][DEBUG]:Service-ID: 30
[2024-03-29 20:31:01.85611800][DEBUG]:cron\Task15::run [--END--][RunTime:0.782444s]
[2024-03-29 20:32:01.12884500][DEBUG]:cron\Task6::run [--START--]
[2024-03-29 20:32:01.13076100][DEBUG]:Service-ID: 34
[2024-03-29 20:32:01.69706000][DEBUG]:cron\Task6::run [--END--][RunTime:0.568220s]
taskPHP自带Utils工具,会产生具体的日志记录
[2024-03-28 00:03:01.41235800][DEBUG]:cron\Task14::run [--START--]
[2024-03-28 00:03:01.41251000][DEBUG]:Task-ID: 31
[2024-03-28 00:03:07.83297000][DEBUG]:cron\Task14::run [--END--][RunTime:6.420764s]
[2024-03-28 00:05:00.89040200][DEBUG]:cron\Task10::run [--START--]
[2024-03-28 00:05:00.89097900][DEBUG]:Task-ID: 35
[2024-03-28 00:05:01.04386100][DEBUG]:cron\Task6::run [--START--]
[2024-03-28 00:05:01.04441500][DEBUG]:Task-ID: 34
[2024-03-28 00:05:01.42001300][DEBUG]:SQL:DESC po_environment[bind_value:null][RunTime:0.059952s]
[2024-03-28 00:05:01.49280500][DEBUG]:SQL:INSERT INTO `po_environment` SET `task_code`=:task_code,`service_id`=:service_id,`school_id`=:school_id,`school_type`=:school_type,`school_attribute`=:school_attribute,`school_short_name`=:school_short_name,`environment_area`=:environment_area,`environment_temperature`=:environment_temperature,`environment_temperature_state`=:environment_temperature_state,`environment_humidity`=:environment_humidity,`environment_humidity_state`=:environment_humidity_state,`environment_air`=:environment_air,`environment_air_state`=:environment_air_state,`environment_time`=:environment_time ON DUPLICATE KEY UPDATE `service_id`=:service_id_1,`school_id`=:school_id_2,`school_type`=:school_type_3,`school_attribute`=:school_attribute_4,`school_short_name`=:school_short_name_5,`environment_area`=:environment_area_6,`environment_temperature`=:environment_temperature_7,`environment_temperature_state`=:environment_temperature_state_8,`environment_humidity`=:environment_humidity_9,`environment_humidity_state`=:environment_humidity_state_10,`environment_air`=:environment_air_11,`environment_air_state`=:environment_air_state_12,`environment_time`=:environment_time_13[bind_value:null][RunTime:0.072542s]
@漏刻有时