切换导航条
此项目
正在载入...
登录
何书鹏
/
recruit
·
提交
转到一个项目
GitLab
转到仪表盘
项目
活动
文件
提交
管道
0
构建
0
图表
里程碑
问题
0
合并请求
0
成员
标记
维基
派生
网络
创建新的问题
下载为
邮件补丁
差异文件
浏览文件
作者
Karson
8 years ago
提交
89e0b2e30b94f3a8e4517cb18425fece8f4020ee
1 个父辈
138ddbb2
新增定时任务处理
修复一处常量在低版本时可能出现的BUG
隐藏空白字符变更
内嵌
并排对比
正在显示
2 个修改的文件
包含
133 行增加
和
1 行删除
application/admin/view/layout/default.html
application/index/controller/Autotask.php
application/admin/view/layout/default.html
查看文件 @
89e0b2e
...
...
@@ -4,7 +4,7 @@
{include file="common/meta" /}
</head>
<body
class=
"inside-header inside-aside {
$Think.const.
IS_DIALOG ? 'is-dialog' : ''}"
>
<body
class=
"inside-header inside-aside {
:defined(IS_DIALOG) &&
IS_DIALOG ? 'is-dialog' : ''}"
>
<div
id=
"main"
role=
"main"
>
<div
class=
"tab-content tab-addtabs"
>
...
...
application/index/controller/Autotask.php
0 → 100644
查看文件 @
89e0b2e
<?php
namespace
app\index\controller
;
use
app\common\model\Crontab
;
use
fast\Date
;
use
fast\Http
;
use
think\Controller
;
use
think\Db
;
use
think\Exception
;
use
think\Log
;
/**
* 定时任务接口
*
* 以Crontab方式每分钟定时执行,且只可以Cli方式运行
* @internal
*/
class
Autotask
extends
Controller
{
/**
* 初始化方法,最前且始终执行
*/
public
function
_initialize
()
{
// 只可以以cli方式执行
if
(
!
$this
->
request
->
isCli
())
$this
->
error
(
'Autotask script only work at client!'
);
parent
::
_initialize
();
// 清除错误
error_reporting
(
0
);
// 设置永不超时
set_time_limit
(
0
);
}
/**
* 执行定时任务
*/
public
function
crontab
()
{
$time
=
time
();
$logDir
=
LOG_PATH
.
'crontab/'
;
if
(
!
is_dir
(
$logDir
))
{
mkdir
(
$logDir
);
}
//筛选未过期且未完成的任务
$crontabList
=
Crontab
::
where
(
'status'
,
'='
,
'normal'
)
->
order
(
'weigh desc,id desc'
)
->
select
();
foreach
(
$crontabList
as
$crontab
)
{
$update
=
[];
$execute
=
FALSE
;
if
(
$time
<
$crontab
[
'begintime'
])
{
//任务未开始
continue
;
}
if
(
$crontab
[
'maximums'
]
&&
$crontab
[
'executes'
]
>
$crontab
[
'maximums'
])
{
//任务已超过最大执行次数
$update
[
'status'
]
=
'finished'
;
}
else
if
(
$crontab
[
'endtime'
]
>
0
&&
$time
>
$crontab
[
'endtime'
])
{
//任务已过期
$update
[
'status'
]
=
'expired'
;
}
else
{
//重复执行
//如果未到执行时间则继续循环
if
(
!
Date
::
cron
(
$crontab
[
'schedule'
]))
continue
;
$execute
=
TRUE
;
}
// 如果允许执行
if
(
$execute
)
{
$update
[
'executetime'
]
=
$time
;
$update
[
'executes'
]
=
$crontab
[
'executes'
]
+
1
;
$update
[
'status'
]
=
(
$crontab
[
'maximums'
]
>
0
&&
$update
[
'executes'
]
>=
$crontab
[
'maximums'
])
?
'finished'
:
'normal'
;
}
// 如果需要更新状态
if
(
!
$update
)
continue
;
// 更新状态
$crontab
->
save
(
$update
);
// 将执行放在后面是为了避免超时导致多次执行
if
(
!
$execute
)
continue
;
try
{
if
(
$crontab
[
'type'
]
==
'url'
)
{
if
(
substr
(
$crontab
[
'content'
],
0
,
1
)
==
"/"
)
{
// 本地项目URL
exec
(
'nohup php '
.
ROOT_PATH
.
'public/index.php '
.
$crontab
[
'content'
]
.
' >> '
.
$logDir
.
date
(
"Y-m-d"
)
.
'.log 2>&1 &'
);
}
else
{
// 远程异步调用URL
Http
::
sendAsyncRequest
(
$crontab
[
'content'
]);
}
}
else
if
(
$crontab
[
'type'
]
==
'sql'
)
{
// 执行SQL
Db
::
getPdo
()
->
exec
(
$crontab
[
'content'
]);
}
else
if
(
$crontab
[
'type'
]
==
'shell'
)
{
// 执行Shell
exec
(
'nohup php '
.
$crontab
[
'content'
]
.
' >> '
.
$logDir
.
date
(
"Y-m-d"
)
.
'.log 2>&1 &'
);
}
}
catch
(
Exception
$e
)
{
Log
::
record
(
$e
->
getMessage
());
}
}
return
'Execute completed!'
;
}
}
...
...
请
注册
或
登录
后发表评论