perf: task进程添加执行记录

This commit is contained in:
kuaifan 2022-11-16 11:27:55 +08:00
parent 99fc12e8c4
commit c1be894d35
14 changed files with 127 additions and 5 deletions

View File

@ -185,6 +185,7 @@ class IndexController extends InvokeController
Task::deliver(new AppPushTask()); Task::deliver(new AppPushTask());
// 删除过期的临时表数据 // 删除过期的临时表数据
Task::deliver(new DeleteTmpTask('wg_tmp_msgs', 1)); Task::deliver(new DeleteTmpTask('wg_tmp_msgs', 1));
Task::deliver(new DeleteTmpTask('task_worker', 12));
Task::deliver(new DeleteTmpTask('tmp', 24)); Task::deliver(new DeleteTmpTask('tmp', 24));
// 周期任务 // 周期任务
Task::deliver(new LoopTask()); Task::deliver(new LoopTask());

37
app/Models/TaskWorker.php Normal file
View File

@ -0,0 +1,37 @@
<?php
namespace App\Models;
use Illuminate\Database\Eloquent\SoftDeletes;
/**
* App\Models\TaskWorker
*
* @property int $id
* @property string|null $args
* @property string|null $error
* @property string|null $start_at 开始时间
* @property string|null $end_at 结束时间
* @property \Illuminate\Support\Carbon|null $created_at
* @property \Illuminate\Support\Carbon|null $updated_at
* @property \Illuminate\Support\Carbon|null $deleted_at
* @method static \Illuminate\Database\Eloquent\Builder|TaskWorker newModelQuery()
* @method static \Illuminate\Database\Eloquent\Builder|TaskWorker newQuery()
* @method static \Illuminate\Database\Query\Builder|TaskWorker onlyTrashed()
* @method static \Illuminate\Database\Eloquent\Builder|TaskWorker query()
* @method static \Illuminate\Database\Eloquent\Builder|TaskWorker whereArgs($value)
* @method static \Illuminate\Database\Eloquent\Builder|TaskWorker whereCreatedAt($value)
* @method static \Illuminate\Database\Eloquent\Builder|TaskWorker whereDeletedAt($value)
* @method static \Illuminate\Database\Eloquent\Builder|TaskWorker whereEndAt($value)
* @method static \Illuminate\Database\Eloquent\Builder|TaskWorker whereError($value)
* @method static \Illuminate\Database\Eloquent\Builder|TaskWorker whereId($value)
* @method static \Illuminate\Database\Eloquent\Builder|TaskWorker whereStartAt($value)
* @method static \Illuminate\Database\Eloquent\Builder|TaskWorker whereUpdatedAt($value)
* @method static \Illuminate\Database\Query\Builder|TaskWorker withTrashed()
* @method static \Illuminate\Database\Query\Builder|TaskWorker withoutTrashed()
* @mixin \Eloquent
*/
class TaskWorker extends AbstractModel
{
use SoftDeletes;
}

View File

@ -1,6 +1,9 @@
<?php <?php
namespace App\Tasks; namespace App\Tasks;
use App\Models\TaskWorker;
use App\Module\Base;
use Carbon\Carbon;
use Hhxsv5\LaravelS\Swoole\Task\Task; use Hhxsv5\LaravelS\Swoole\Task\Task;
/** /**
@ -9,6 +12,20 @@ use Hhxsv5\LaravelS\Swoole\Task\Task;
*/ */
abstract class AbstractTask extends Task abstract class AbstractTask extends Task
{ {
protected int $twid = 0;
public function __construct(...$params)
{
$row = TaskWorker::createInstance([
'args' => [
'params' => $params,
'class' => get_class($this)
],
]);
if ($row->save()) {
$this->twid = $row->id;
}
}
/** /**
* 开始执行任务 * 开始执行任务
@ -25,11 +42,12 @@ abstract class AbstractTask extends Task
*/ */
final public function handle() final public function handle()
{ {
TaskWorker::whereId($this->twid)->update(['start_at' => Carbon::now()]);
//
try { try {
$this->start(); $this->start();
} catch (\Throwable $e) { } catch (\Throwable $e) {
$this->failed("start", $e); $this->failed("start", $e);
} }
} }
@ -38,8 +56,11 @@ abstract class AbstractTask extends Task
*/ */
final public function finish() final public function finish()
{ {
TaskWorker::whereId($this->twid)->update(['end_at' => Carbon::now()]);
//
try { try {
$this->end(); $this->end();
TaskWorker::whereId($this->twid)->delete();
} catch (\Throwable $e) { } catch (\Throwable $e) {
$this->failed("end", $e); $this->failed("end", $e);
} }
@ -54,5 +75,13 @@ abstract class AbstractTask extends Task
{ {
info($type); info($type);
info($e); info($e);
//
TaskWorker::whereId($this->twid)->update(['error' => Base::array2json([
'time' => Carbon::now(),
'type' => $type,
'code' => $e->getCode(),
'file' => $e->getFile(),
'message' => $e->getMessage(),
])]);
} }
} }

View File

@ -17,7 +17,7 @@ class AppPushTask extends AbstractTask
public function __construct() public function __construct()
{ {
// parent::__construct();
} }
public function start() public function start()

View File

@ -18,7 +18,7 @@ class AutoArchivedTask extends AbstractTask
public function __construct() public function __construct()
{ {
// parent::__construct();
} }
public function start() public function start()

View File

@ -2,6 +2,7 @@
namespace App\Tasks; namespace App\Tasks;
use App\Models\TaskWorker;
use App\Models\Tmp; use App\Models\Tmp;
use App\Models\WebSocketTmpMsg; use App\Models\WebSocketTmpMsg;
use Carbon\Carbon; use Carbon\Carbon;
@ -18,6 +19,7 @@ class DeleteTmpTask extends AbstractTask
public function __construct(string $data, int $hours) public function __construct(string $data, int $hours)
{ {
parent::__construct(...func_get_args());
$this->data = $data; $this->data = $data;
$this->hours = $hours; $this->hours = $hours;
} }
@ -54,6 +56,18 @@ class DeleteTmpTask extends AbstractTask
}); });
} }
break; break;
/**
* 表pre_task_worker
*/
case 'tw':
{
TaskWorker::onlyTrashed()
->where('deleted_at', '<', Carbon::now()->subHours($this->hours)->toDateTimeString())
->orderBy('id')
->forceDelete();
}
break;
} }
} }

View File

@ -19,7 +19,7 @@ class EmailNoticeTask extends AbstractTask
{ {
public function __construct() public function __construct()
{ {
// parent::__construct();
} }
public function start() public function start()

View File

@ -28,6 +28,7 @@ class IhttpTask extends AbstractTask
*/ */
public function __construct($url, $post = [], $extra = []) public function __construct($url, $post = [], $extra = [])
{ {
parent::__construct(...func_get_args());
$this->url = $url; $this->url = $url;
$this->post = $post; $this->post = $post;
$this->extra = $extra; $this->extra = $extra;

View File

@ -25,6 +25,7 @@ class LineTask extends AbstractTask
*/ */
public function __construct($userid, bool $online) public function __construct($userid, bool $online)
{ {
parent::__construct(...func_get_args());
$this->userid = $userid; $this->userid = $userid;
$this->online = $online; $this->online = $online;
} }

View File

@ -18,7 +18,7 @@ class LoopTask extends AbstractTask
{ {
public function __construct() public function __construct()
{ {
parent::__construct();
} }
public function start() public function start()

View File

@ -27,6 +27,7 @@ class PushTask extends AbstractTask
*/ */
public function __construct($params = [], $retryOffline = true) public function __construct($params = [], $retryOffline = true)
{ {
parent::__construct(...func_get_args());
$this->params = $params; $this->params = $params;
$this->retryOffline = $retryOffline; $this->retryOffline = $retryOffline;
} }

View File

@ -20,6 +20,7 @@ class PushUmengMsg extends AbstractTask
*/ */
public function __construct($userid, $array = []) public function __construct($userid, $array = [])
{ {
parent::__construct(...func_get_args());
$this->userid = $userid; $this->userid = $userid;
$this->array = is_array($array) ? $array : []; $this->array = is_array($array) ? $array : [];
} }

View File

@ -34,6 +34,7 @@ class WebSocketDialogMsgTask extends AbstractTask
*/ */
public function __construct($id, $ignoreFd = null) public function __construct($id, $ignoreFd = null)
{ {
parent::__construct(...func_get_args());
$this->id = $id; $this->id = $id;
$this->ignoreFd = $ignoreFd === null ? Request::header('fd') : $ignoreFd; $this->ignoreFd = $ignoreFd === null ? Request::header('fd') : $ignoreFd;
} }

View File

@ -0,0 +1,36 @@
<?php
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
class CreateTaskWorkersTable extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::create('task_workers', function (Blueprint $table) {
$table->bigIncrements('id');
$table->longText('args')->nullable();
$table->longText('error')->nullable();
$table->timestamp('start_at')->nullable()->comment('开始时间');
$table->timestamp('end_at')->nullable()->comment('结束时间');
$table->timestamps();
$table->softDeletes();
});
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::dropIfExists('task_workers');
}
}