setName('qywx:work-promotion-automation') ->setDescription('秒级消费推广欢迎语(需常驻;欢迎码仅20秒有效)') ->addOption('once', null, Option::VALUE_NONE, '只消费一轮'); } protected function execute(Input $input, Output $output): int { $running = true; if (function_exists('pcntl_async_signals')) { pcntl_async_signals(true); pcntl_signal(SIGTERM, static function () use (&$running): void { $running = false; }); pcntl_signal(SIGINT, static function () use (&$running): void { $running = false; }); } $service = new QywxPromotionAutomationService(); do { try { $result = $service->processWelcomes(100); if ($result['selected'] > 0 || $input->getOption('once')) { $output->writeln('QYWX_PROMOTION_WELCOME ' . json_encode($result)); } } catch (\Throwable) { // 不输出异常堆栈/SQL/请求,避免把短期凭证带进守护进程日志。 $output->writeln('QYWX_PROMOTION_WELCOME worker storage unavailable'); if ($input->getOption('once')) { return 1; } } if (!$input->getOption('once') && $running) { usleep(250000); } } while (!$input->getOption('once') && $running); return 0; } }