Skip to content

Commit 2616b0f

Browse files
committed
add cron job
1 parent d494aac commit 2616b0f

File tree

3 files changed

+82
-6
lines changed

3 files changed

+82
-6
lines changed
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
<?php
2+
/**
3+
* 创建定时任务类--毫秒级定时任务
4+
* 基于 Swoole 的毫秒定时器,封装的定时任务,取代 Linux 的 Crontab
5+
*
6+
* User: lisgroup
7+
* Date: 20-06-08
8+
* Time: 15:16
9+
*/
10+
11+
namespace App\Jobs\Timer;
12+
13+
use App\Http\Repository\ApiRepository;
14+
use Hhxsv5\LaravelS\Swoole\Timer\CronJob;
15+
16+
class FiveMinutesCronJob extends CronJob
17+
{
18+
protected $i = 0;
19+
// !!! 定时任务的`interval`和`isImmediate`有两种配置方式(二选一):一是重载对应的方法,二是注册定时任务时传入参数。
20+
// --- 重载对应的方法来返回配置:开始
21+
public function interval()
22+
{
23+
return 1000 * 300;// 每300秒运行一次
24+
}
25+
26+
public function isImmediate()
27+
{
28+
return false;// 是否立即执行第一次,false则等待间隔时间后执行第一次
29+
}
30+
31+
// --- 重载对应的方法来返回配置:结束
32+
public function run()
33+
{
34+
\Log::info(__METHOD__, ['start', $this->i, microtime(true)]);
35+
// do something
36+
ApiRepository::getInstent()->autoFailed();
37+
}
38+
}
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
<?php
2+
/**
3+
* 创建定时任务类--毫秒级定时任务
4+
* 基于 Swoole 的毫秒定时器,封装的定时任务,取代 Linux 的 Crontab
5+
*
6+
* User: lisgroup
7+
* Date: 20-06-08
8+
* Time: 15:20
9+
*/
10+
11+
namespace App\Jobs\Timer;
12+
13+
use App\Http\Repository\ApiRepository;
14+
use App\Tasks\TestTask;
15+
use Swoole\Coroutine;
16+
use Hhxsv5\LaravelS\Swoole\Task\Task;
17+
use Hhxsv5\LaravelS\Swoole\Timer\CronJob;
18+
19+
class HourlyCronJob extends CronJob
20+
{
21+
protected $i = 0;
22+
// !!! 定时任务的`interval`和`isImmediate`有两种配置方式(二选一):一是重载对应的方法,二是注册定时任务时传入参数。
23+
// --- 重载对应的方法来返回配置:开始
24+
public function interval()
25+
{
26+
return 1000 * 3600;// 每 3600 秒运行一次
27+
}
28+
29+
public function isImmediate()
30+
{
31+
return false;// 是否立即执行第一次,false则等待间隔时间后执行第一次
32+
}
33+
34+
// --- 重载对应的方法来返回配置:结束
35+
public function run()
36+
{
37+
\Log::info(__METHOD__, ['start', $this->i, microtime(true)]);
38+
// do something
39+
ApiRepository::getInstent()->handleAutoDelete();
40+
}
41+
}

laravel/config/laravels.php

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -31,15 +31,12 @@
3131
'enable' => true, // 开启定时任务类
3232
'jobs' => [
3333
// 启用LaravelScheduleJob来执行`php artisan schedule:run`,每分钟一次,替代Linux Crontab
34-
\Hhxsv5\LaravelS\Illuminate\LaravelScheduleJob::class,
34+
// \Hhxsv5\LaravelS\Illuminate\LaravelScheduleJob::class,
3535
// 两种配置参数的方式:
3636
// [\App\Jobs\Timer\TestCronJob::class, [1000, true]], // 注册时传入参数
3737
// \App\Jobs\Timer\TestCronJob::class, // 重载对应的方法来返回参数
38-
// Enable LaravelScheduleJob to run `php artisan schedule:run` every 1 minute, replace Linux Crontab
39-
//\Hhxsv5\LaravelS\Illuminate\LaravelScheduleJob::class,
40-
// Two ways to configure parameters:
41-
// [\App\Jobs\XxxCronJob::class, [1000, true]], // Pass in parameters when registering
42-
// \App\Jobs\XxxCronJob::class, // Override the corresponding method to return the configuration
38+
App\Jobs\Timer\FiveMinutesCronJob::class,
39+
App\Jobs\Timer\HourlyCronJob::class,
4340
],
4441
'pid_file' => storage_path('laravels-timer.pid'),
4542
'max_wait_time' => 5, // Reload 时最大等待时间

0 commit comments

Comments
 (0)