轻易云
注册体验

目标平台查询适配器,实现dispatch,handleResponse,connect方法

1 条评论

目标平台查询适配器:实现dispatch,handleResponse,connect方法

<?php

namespace Adapter\PlatformName;

use Domain\Datahub\Instance\Storage\DataStatus;
use Domain\Datahub\Instance\Adapter\Adapter;
use Domain\Datahub\Instance\LogMessage;
use Domain\Datahub\Instance\Storage\LogStatus;

class PlatformNameExecuteAdapter extends Adapter
{
    const DIRECTION = 'target';
    private $times = 0;

    /**
     * 调度方法
     *
     * @return void
     */
    public function dispatch()
    {
        $this->times++;
        if ($this->times >= 30) {
            $this->asynTargetJobDispatch(10); // 重新激活 dt 命令
            return;
        }
        $data = $this->getDataStorage()->fetch(); // 从mongodb 获取待处理数据
        if (count($data) === 0) {
            return $this->_returnDispatch();
        }
        $request = $this->generateRequestParams($data); // 转化原始数据为目标平台写入数据
        $request = $this->removeNull($request);//清除空值
        if (!$request) {
            $this->getLogStorage()->insertOne(['text' => LogMessage::DISPATCH_TARGET_REQUEST_ERROR, 'request' => $request], LogStatus::ERROR);
            $this->dispatch();
            return;
        }
        // 标记原始数据为 队列中 插入到目标平台队列池
        $jobId = $this->getAsynTargetJobStorage()->insertOne($this->metaData['api'], [$request], $this->getDataStorage()->ids, $this->getDataStorage()->dataRange);
        $this->getDataStorage()->setFetchStatus(DataStatus::QUEUE, null, null, new \MongoDB\BSON\ObjectId($jobId));
        $this->jobs[] = $jobId;
        // 开始进行排队
        $this->asynTargetJob(round($this->asynTimes), $jobId);
        $this->asynTimes += 1.4;
        $this->dispatch();
        return true;
    }
}

目标平台写入 $adapter->dispatch();
步骤1: 递增调度次数 $times++,超过最大调度次数排队 ??秒 下一次调度
步骤2: 从Mongodb取得一批【待处理】数据
步骤3: 数据+元数据生成请求参数
步骤4:写入任务到队列存储器返回jobid
步骤5:任务开始进行排队(自定义延迟)
步骤6:标记数据正在排队
步骤7:递归循环调度下一次

目标平台写入 $adapter->handleResponse();

public function handleResponse($response, $jobId = null)
{
    $this->getLogStorage()->insertOne(['text' => 'handleResponse', 'response' => $response], LogStatus::RECORD);
    if ($response['Success'] != true) {
        return $this->handleError($response, $jobId);
    }
    $this->getAsynTargetJobStorage()->updateResponse($jobId, DataStatus::FINISHED, $response, [], null, $this->active);
    $this->handleSuccessCallback($response, $jobId);
    return $response;
}

错误日志方法

public function handleError($response, $jobId = null)
{
    $throw = new PlatformThrowable($this);
    $throw->handle($jobId, $response);
    $this->getAsynTargetJobStorage()->updateResponse($jobId, DataStatus::ERROR, $response, [], null, $this->active);
    $this->getLogStorage()->insertOne(['text' => LogMessage::INVOKE_FAIL, 'response' => $response], LogStatus::ERROR);
    return $response;
}

目标平台写入 $adapter->connect();
同源平台方法一样。