<?php

namespace xmvc\daemon;

use xmvc\QueueJob;
use xmvc\daemon\lib\QueueDaemon;

/**
 * 发送邮件服务
 */
class SendMailDaemon extends QueueDaemon
{
    public function __construct()
    {
        parent::__construct('beanstalk', 'beanstalkd>persist');
    }

    /**
     * 清理资源
     */
    protected function cleanup()
    {
        parent::cleanup();
    }

    /**
     * 处理任务
     * @return bool true 删除任务, false 恢复任务延时等待下次消费
     */
    protected function handle(QueueJob $job): bool
    {
        $result = false;
        try {
            echo "PID:" . getmypid() . "\n";
            print_r($job->toArray());
            self::log("PID:" . getmypid());
            // TO-DO 发送邮件代码
            $result = true;
        } catch (\Throwable $th) {
            static::error('handle() 出错: ' . $th->getMessage(), __FILE__, __LINE__);
        }
        return $result;
    }
}
