1:定义适配器命名空间
2:定义适配器类名
3:引入使用的类和SDK
4:依次定义调度方法: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;
/**
* 调度方法 function
*
* @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;
}
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;
}
}