|
|
|
<?php
|
|
|
|
//推广公司模型
|
|
|
|
namespace Admin\Model;
|
|
|
|
use Think\Model;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 文档基础模型
|
|
|
|
*/
|
|
|
|
class CmdTasksModel extends Model{
|
|
|
|
protected $tablePrefix = 'tab_';
|
|
|
|
public function __construct($model)
|
|
|
|
{
|
|
|
|
if(IS_SUBSITE){
|
|
|
|
parent::__construct($model, $this->tablePrefix, SUBSITE_DB);
|
|
|
|
}else{
|
|
|
|
parent::__construct($model, $this->tablePrefix);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
//获取待执行任务
|
|
|
|
public function getTask()
|
|
|
|
{
|
|
|
|
$has_run = $this->where(['status'=>"1"])->find();
|
|
|
|
if($has_run){
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
$where = [
|
|
|
|
'status'=>"0",
|
|
|
|
'schedule_time'=>['ELT',time()]
|
|
|
|
];
|
|
|
|
$task = $this->field("id,params")->where($where)->order("id asc")->find();
|
|
|
|
if(empty($task)){
|
|
|
|
return false;
|
|
|
|
}else{
|
|
|
|
//修改状态为执行中
|
|
|
|
$this->save(['id'=>$task['id'],'status'=>"1","start_time"=>time()]);
|
|
|
|
return $task;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
//更新
|
|
|
|
public function updateTask($params)
|
|
|
|
{
|
|
|
|
$this->save($params);
|
|
|
|
}
|
|
|
|
//获取同类型任务最后一次操作时间
|
|
|
|
public function getTypeLastTask($type)
|
|
|
|
{
|
|
|
|
$task = $this->field("end_time")->where(['status'=>"2",'type'=>$type])->order("id desc")->find();
|
|
|
|
if(empty($task)){
|
|
|
|
return false;
|
|
|
|
}else{
|
|
|
|
return date("Y-m-d H:i:s",$task['end_time']);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
//插入任务
|
|
|
|
public function addTask($type,$params,$repeat=false,$need_path=true,$checkScheduleTime = 0)
|
|
|
|
{
|
|
|
|
if(!$repeat){
|
|
|
|
if(!$this->isCanAddTask($type)) return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
if($need_path){
|
|
|
|
if(IS_WIN){
|
|
|
|
$p = explode(':',ROOTTTTT);
|
|
|
|
$params = "{$p[0]}:&".'cd "'.$p[1].'"&'.$params;
|
|
|
|
}else{
|
|
|
|
$params = "cd ".ROOTTTTT.";".$params;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
$params = rtrim($params,";").' 2>&1';
|
|
|
|
$save = [
|
|
|
|
"uid"=>$_SESSION['onethink_admin']['user_auth']['uid']??0,
|
|
|
|
"created_time"=>time(),
|
|
|
|
"params"=>$params,
|
|
|
|
"type"=>$type,
|
|
|
|
"schedule_time"=>$checkScheduleTime
|
|
|
|
];
|
|
|
|
return $this->add($save);
|
|
|
|
}
|
|
|
|
public function addScheduleTask($type,$params,$checkScheduleTime = 0)
|
|
|
|
{
|
|
|
|
$this->addTask($type,$params,true,true,$checkScheduleTime);
|
|
|
|
}
|
|
|
|
|
|
|
|
//验证任务
|
|
|
|
public function isCanAddTask($type,$checkScheduleTime=false)
|
|
|
|
{
|
|
|
|
$where = [
|
|
|
|
'status'=>["in","0,1"],
|
|
|
|
'type'=>$type
|
|
|
|
];
|
|
|
|
if(!$checkScheduleTime){
|
|
|
|
$where['schedule_time'] = 0;
|
|
|
|
}
|
|
|
|
$task = $this->field("end_time")->where($where)->order("id desc")->find();
|
|
|
|
if(!empty($task)){
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|