diff --git a/catch/monitor/command/ScheduleCommand.php b/catch/monitor/command/ScheduleCommand.php index ecaee21..4c32861 100644 --- a/catch/monitor/command/ScheduleCommand.php +++ b/catch/monitor/command/ScheduleCommand.php @@ -96,13 +96,14 @@ class ScheduleCommand extends Command { $executeAbleCommands = []; + Crontab::where('status', Crontab::ENABLE) - ->where('tactics', '<>', Crontab::EXECUTE_FORBIDDEN) ->select() ->each(function ($command) use (&$executeAbleCommands){ if ($command->tactics == Crontab::EXECUTE_IMMEDIATELY) { $executeAbleCommands[] = $command; - return true; + $command->tactics = Crontab::EXECUTE_NORMAL; + return $command->save(); } $can = date('Y-m-d H:i', @@ -113,9 +114,8 @@ class ScheduleCommand extends Command if ($can) { // 如果任务只执行一次 之后禁用该任务 if ($command->tactics === Crontab::EXECUTE_ONCE) { - Crontab::where('id', $command->id)->update([ - 'status' => Crontab::DISABLE, - ]); + $command->tactics = Crontab::DISABLE; + $command->save(); } $executeAbleCommands[] = $command; diff --git a/catch/monitor/controller/Crontab.php b/catch/monitor/controller/Crontab.php index 57458a6..36daa13 100644 --- a/catch/monitor/controller/Crontab.php +++ b/catch/monitor/controller/Crontab.php @@ -47,7 +47,7 @@ class Crontab extends CatchController */ public function save(Request $request) { - CronExpression::factory($request->post('cron')); + new CronExpression($request->post('cron')); return CatchResponse::success($this->model->storeBy($request->post())); } @@ -73,7 +73,7 @@ class Crontab extends CatchController */ public function update(Request $request, $id) { - CronExpression::factory($request->post('cron')); + new CronExpression($request->post('cron')); return CatchResponse::success($this->model->updateBy($id, $request->post())); } diff --git a/catch/monitor/database/migrations/20200914203553_crontab.php b/catch/monitor/database/migrations/20200914203553_crontab.php index b41ecae..1ff27f3 100644 --- a/catch/monitor/database/migrations/20200914203553_crontab.php +++ b/catch/monitor/database/migrations/20200914203553_crontab.php @@ -30,16 +30,16 @@ class Crontab extends Migrator public function change() { $table = $this->table('crontab', ['engine' => 'InnoDB', 'collation' => 'utf8mb4_general_ci', 'comment' => '定时任务' ,'id' => 'id','signed' => true ,'primary_key' => ['id']]); - $table->addColumn('name', 'string', ['limit' => 255,'null' => false,'default' => '','signed' => false,'comment' => '任务名称',]) - ->addColumn('group', 'boolean', ['null' => false,'default' => 1,'signed' => false,'comment' => '1 默认 2 系统',]) - ->addColumn('task', 'string', ['limit' => 255,'null' => false,'default' => '','signed' => false,'comment' => '任务名称',]) - ->addColumn('cron', 'string', ['limit' => 50,'null' => false,'default' => '','signed' => false,'comment' => 'cron 表达式',]) - ->addColumn('tactics', 'boolean', ['null' => false,'default' => 1,'signed' => false,'comment' => '1 立即执行 2 执行一次 3 放弃执行',]) - ->addColumn('status', 'boolean', ['null' => false,'default' => 1,'signed' => false,'comment' => '1 正常 2 禁用',]) - ->addColumn('remark', 'string', ['limit' => 1000,'null' => false,'default' => '','signed' => false,'comment' => '备注',]) - ->addColumn('creator_id', 'integer', ['limit' => MysqlAdapter::INT_REGULAR,'null' => false,'default' => 0,'signed' => true,'comment' => '创建人ID',]) - ->addColumn('created_at', 'integer', ['limit' => MysqlAdapter::INT_REGULAR,'null' => false,'default' => 0,'signed' => true,'comment' => '创建时间',]) - ->addColumn('updated_at', 'integer', ['limit' => MysqlAdapter::INT_REGULAR,'null' => false,'default' => 0,'signed' => true,'comment' => '更新时间',]) + $table->addColumn('name', 'string', ['limit' => 255,'null' => false,'default' => '','signed' => false,'comment' => '任务名称',]) + ->addColumn('group', 'boolean', ['null' => false,'default' => 1,'signed' => false,'comment' => '1 默认 2 系统',]) + ->addColumn('task', 'string', ['limit' => 255,'null' => false,'default' => '','signed' => false,'comment' => '任务名称',]) + ->addColumn('cron', 'string', ['limit' => 50,'null' => false,'default' => '','signed' => false,'comment' => 'cron 表达式',]) + ->addColumn('tactics', 'boolean', ['null' => false,'default' => 1,'signed' => false,'comment' => '1 立即执行 2 执行一次 3 正常执行',]) + ->addColumn('status', 'boolean', ['null' => false,'default' => 1,'signed' => false,'comment' => '1 正常 2 禁用',]) + ->addColumn('remark', 'string', ['limit' => 1000,'null' => false,'default' => '','signed' => false,'comment' => '备注',]) + ->addColumn('creator_id', 'integer', ['limit' => MysqlAdapter::INT_REGULAR,'null' => false,'default' => 0,'signed' => true,'comment' => '创建人ID',]) + ->addColumn('created_at', 'integer', ['limit' => MysqlAdapter::INT_REGULAR,'null' => false,'default' => 0,'signed' => true,'comment' => '创建时间',]) + ->addColumn('updated_at', 'integer', ['limit' => MysqlAdapter::INT_REGULAR,'null' => false,'default' => 0,'signed' => true,'comment' => '更新时间',]) ->addColumn('deleted_at', 'integer', ['limit' => MysqlAdapter::INT_REGULAR,'null' => false,'default' => 0,'signed' => true,'comment' => '软删除',]) ->create(); } diff --git a/catch/monitor/model/Crontab.php b/catch/monitor/model/Crontab.php index 77b6a9a..66c3445 100644 --- a/catch/monitor/model/Crontab.php +++ b/catch/monitor/model/Crontab.php @@ -38,6 +38,5 @@ class Crontab extends Model const EXECUTE_IMMEDIATELY = 1; // 立即执行 const EXECUTE_ONCE = 2; // 执行一次 - const EXECUTE_FORBIDDEN = 3; // 停止执行 - + const EXECUTE_NORMAL = 3; // 正常执行 } \ No newline at end of file