123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172 |
- <?php
- /**
- * @Created by PhpStorm
- * @author: Kirin
- * @day: 2025/2/19
- * @time: 16:58
- */
- namespace app\command;
- use Exception;
- use qiniu\services\blockchain\TransactionService;
- use Swoole\Coroutine;
- use think\console\Command;
- use think\console\Input;
- use think\console\Output;
- use think\facade\Cache;
- use think\facade\Log;
- class AutoTask extends Command
- {
- private $task = [
- 'test' => ['name' => '测试', 'time' => 300],
- ];
- protected function configure()
- {
- // 指令配置
- $this->setName('autoTask')
- ->setDescription('自动程序');
- }
- protected function execute(Input $input, Output $output)
- {
- go(function () {
- while (true) {
- foreach ($this->task as $k => $v) {
- $last_time = Cache::store('redis')->get($k . '_time', 0);
- if ($last_time + $v['time'] < time()) {
- try {
- $this->$k();
- $this->log($v['name'] . '执行');
- } catch (\Throwable $e) {
- Log::error($v['name'] . '运行出错:' . $e->getMessage() . '[' . $e->getFile() . ':' . $e->getLine() . ']');
- $this->log($v['name'] . '运行出错:' . $e->getMessage());
- }
- Cache::store('redis')->set($k . '_time', time());
- }
- }
- Coroutine::sleep(1);
- }
- });
- $output->info("自动程序启动");
- }
- /**
- * @throws Exception
- */
- private function test()
- {
- }
- public function log($msg)
- {
- echo "[" . date('Y-m-d H:i:s') . "] " . $msg . PHP_EOL;
- }
- }
|