<?php

namespace xmvc\test;

use xmvc\Queue;
use xmvc\ErrorClass;

/**
 * 队列测试
 */
class QueueTest
{
    /**
     * Beanstalk 队列 添加任务到队列
     * @link HOST/xmvc/test/QueueTest-beanstalkAdd.json
     * @see xmvc\interface\IQueue::
     * @example ```php
     * // 配置标准
     * 'beanstalkd' => [
     *      // 非持久化
     *      'default' => [
     *          'host' => '127.0.0.1',
     *          'port' => 11300,
     *          // 管道名称/
     *          'tubeName' => 'default',
     *          //是否持久化/
     *          'persist' => false,
     *          // 是否多任务消费/
     *          'multiTasking' => false,
     *          // 持久化数据库的配置/
     *          'dbCfg' => 'xmvc',
     *          // 持久化表/
     *          'table' => 'beanstalkd',
     *      ],
     *  ]
     * ```
     */
    public function beanstalkAdd()
    {
        $queue = Queue::create('beanstalk', 'beanstalkd>persist');
        $data = [
            'opuser' => '管理员',
            'action' => 'demo/test-import',
            'filename' => '/home/qg/1.xlsx',
            'time' => time(),
        ];
        $queue->put($data);
        return R($data);
    }

    /**
     * Beanstalk 队列 销费队列中的任务
     * @link 命令模式 php index.php xmvc/test/QueueTest-beanstalkReserve
     * @see xmvc\interface\IQueue::
     */
    public function beanstalkReserve()
    {
        $queue = Queue::create('beanstalk', 'beanstalkd>default');
        $job =  $queue->reserve();
        if ($job->status == 'WAIT') {
            try {
                $queue->touch($job);
                print_r($job);
                $queue->delete($job);
            } catch (\Exception $e) {
                $queue->release($job);
            }
        }
        exit();
    }
}
