123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103 |
- <?php
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
- namespace app\common\command;
-
- use Cron\CronExpression;
- use think\console\Command;
- use think\console\Input;
- use think\console\Output;
- use think\facade\Console;
- use think\facade\Db;
- use think\facade\Log;
-
-
- class Crontab extends Command
- {
- protected function configure()
- {
- $this->setName('crontab')
- ->setDescription('定时任务');
- }
-
-
-
- protected function execute(Input $input, Output $output)
- {
- Log::close();
-
- $time = time();
-
- $crons = Db::name('dev_crontab')
- ->where(['status' => 1])
- ->select();
- if (empty($crons)) {
- return;
- }
-
- foreach ($crons as $cron) {
-
- if (CronExpression::isValidExpression($cron['expression']) === false) {
- continue;
- }
-
- $cron_expression = CronExpression::factory($cron['expression']);
- $next_time = $cron_expression->getNextRunDate(date('Y-m-d H:i:s', $cron['last_time']))->getTimestamp();
- if ($next_time >= $time) {
- continue;
- }
-
-
- try {
-
- $parameter = explode(' ', $cron['parameter']);
- if (is_array($parameter) && !empty($cron['parameter'])) {
- Console::call($cron['command'], $parameter);
- } else {
- Console::call($cron['command']);
- }
- Db::name('dev_crontab')
- ->where(['id' => $cron['id']])
- ->update(['error' => '']);
- } catch (\Exception $e) {
- Db::name('dev_crontab')
- ->where(['id' => $cron['id']])
- ->update(['error' => $e->getMessage(), 'status' => 3]);
- } finally {
-
-
-
- Db::name('dev_crontab')
- ->where(['id' => $cron['id']])
- ->update(['last_time' => $time]);
- }
- }
- }
-
-
- }
|