90 lines
2.3 KiB
PHP
90 lines
2.3 KiB
PHP
<?php
|
|
|
|
namespace app\master\service;
|
|
|
|
use think\Db;
|
|
|
|
/**
|
|
* 推送日志读写
|
|
*/
|
|
class DbPushLogger
|
|
{
|
|
const STATUS_PENDING = 0; // 待推送 / 排队中
|
|
const STATUS_SUCCESS = 1;
|
|
const STATUS_FAIL = 2;
|
|
|
|
protected $obj;
|
|
|
|
public function __construct()
|
|
{
|
|
$this->obj = Db::name('db_push_log');
|
|
}
|
|
|
|
/**
|
|
* 创建一条待推送日志,返回 log_id
|
|
*/
|
|
public function create($journalId, $stageId, $channelKey, $channelName, $operator = '', $articleId = 0)
|
|
{
|
|
$now = time();
|
|
try {
|
|
return $this->obj->insertGetId([
|
|
'journal_id' => (int) $journalId,
|
|
'journal_stage_id' => (int) $stageId,
|
|
'article_id' => (int) $articleId,
|
|
'channel' => $channelKey,
|
|
'channel_name' => $channelName,
|
|
'status' => self::STATUS_PENDING,
|
|
'attempt' => 0,
|
|
'message' => '',
|
|
'operator' => $operator,
|
|
'ctime' => $now,
|
|
'utime' => $now,
|
|
]);
|
|
} catch (\Throwable $e) {
|
|
// 日志表尚未创建时,不影响推送本身
|
|
return 0;
|
|
}
|
|
}
|
|
|
|
/**
|
|
* 标记结果
|
|
*/
|
|
public function finish($logId, $ok, $message = '')
|
|
{
|
|
if (empty($logId)) {
|
|
return;
|
|
}
|
|
try {
|
|
$this->obj->where('log_id', $logId)->update([
|
|
'status' => $ok ? self::STATUS_SUCCESS : self::STATUS_FAIL,
|
|
'message' => mb_substr((string) $message, 0, 2000),
|
|
'utime' => time(),
|
|
]);
|
|
$this->obj->where('log_id', $logId)->setInc('attempt');
|
|
} catch (\Throwable $e) {
|
|
// ignore
|
|
}
|
|
}
|
|
|
|
public function find($logId)
|
|
{
|
|
try {
|
|
return $this->obj->where('log_id', $logId)->find();
|
|
} catch (\Throwable $e) {
|
|
return null;
|
|
}
|
|
}
|
|
|
|
/**
|
|
* 某一期的推送情况
|
|
*/
|
|
public function stageStatus($stageId)
|
|
{
|
|
try {
|
|
return $this->obj->where('journal_stage_id', $stageId)->order('log_id desc')->select();
|
|
} catch (\Throwable $e) {
|
|
return [];
|
|
}
|
|
}
|
|
}
|