mirror of
https://github.com/kuaifan/dootask.git
synced 2026-02-28 04:40:37 +00:00
perf: task进程添加执行记录
This commit is contained in:
parent
99fc12e8c4
commit
c1be894d35
@ -185,6 +185,7 @@ class IndexController extends InvokeController
|
||||
Task::deliver(new AppPushTask());
|
||||
// 删除过期的临时表数据
|
||||
Task::deliver(new DeleteTmpTask('wg_tmp_msgs', 1));
|
||||
Task::deliver(new DeleteTmpTask('task_worker', 12));
|
||||
Task::deliver(new DeleteTmpTask('tmp', 24));
|
||||
// 周期任务
|
||||
Task::deliver(new LoopTask());
|
||||
|
||||
37
app/Models/TaskWorker.php
Normal file
37
app/Models/TaskWorker.php
Normal 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;
|
||||
}
|
||||
@ -1,6 +1,9 @@
|
||||
<?php
|
||||
namespace App\Tasks;
|
||||
|
||||
use App\Models\TaskWorker;
|
||||
use App\Module\Base;
|
||||
use Carbon\Carbon;
|
||||
use Hhxsv5\LaravelS\Swoole\Task\Task;
|
||||
|
||||
/**
|
||||
@ -9,6 +12,20 @@ use Hhxsv5\LaravelS\Swoole\Task\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()
|
||||
{
|
||||
TaskWorker::whereId($this->twid)->update(['start_at' => Carbon::now()]);
|
||||
//
|
||||
try {
|
||||
$this->start();
|
||||
} catch (\Throwable $e) {
|
||||
$this->failed("start", $e);
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
@ -38,8 +56,11 @@ abstract class AbstractTask extends Task
|
||||
*/
|
||||
final public function finish()
|
||||
{
|
||||
TaskWorker::whereId($this->twid)->update(['end_at' => Carbon::now()]);
|
||||
//
|
||||
try {
|
||||
$this->end();
|
||||
TaskWorker::whereId($this->twid)->delete();
|
||||
} catch (\Throwable $e) {
|
||||
$this->failed("end", $e);
|
||||
}
|
||||
@ -54,5 +75,13 @@ abstract class AbstractTask extends Task
|
||||
{
|
||||
info($type);
|
||||
info($e);
|
||||
//
|
||||
TaskWorker::whereId($this->twid)->update(['error' => Base::array2json([
|
||||
'time' => Carbon::now(),
|
||||
'type' => $type,
|
||||
'code' => $e->getCode(),
|
||||
'file' => $e->getFile(),
|
||||
'message' => $e->getMessage(),
|
||||
])]);
|
||||
}
|
||||
}
|
||||
|
||||
@ -17,7 +17,7 @@ class AppPushTask extends AbstractTask
|
||||
|
||||
public function __construct()
|
||||
{
|
||||
//
|
||||
parent::__construct();
|
||||
}
|
||||
|
||||
public function start()
|
||||
|
||||
@ -18,7 +18,7 @@ class AutoArchivedTask extends AbstractTask
|
||||
|
||||
public function __construct()
|
||||
{
|
||||
//
|
||||
parent::__construct();
|
||||
}
|
||||
|
||||
public function start()
|
||||
|
||||
@ -2,6 +2,7 @@
|
||||
|
||||
namespace App\Tasks;
|
||||
|
||||
use App\Models\TaskWorker;
|
||||
use App\Models\Tmp;
|
||||
use App\Models\WebSocketTmpMsg;
|
||||
use Carbon\Carbon;
|
||||
@ -18,6 +19,7 @@ class DeleteTmpTask extends AbstractTask
|
||||
|
||||
public function __construct(string $data, int $hours)
|
||||
{
|
||||
parent::__construct(...func_get_args());
|
||||
$this->data = $data;
|
||||
$this->hours = $hours;
|
||||
}
|
||||
@ -54,6 +56,18 @@ class DeleteTmpTask extends AbstractTask
|
||||
});
|
||||
}
|
||||
break;
|
||||
|
||||
/**
|
||||
* 表pre_task_worker
|
||||
*/
|
||||
case 'tw':
|
||||
{
|
||||
TaskWorker::onlyTrashed()
|
||||
->where('deleted_at', '<', Carbon::now()->subHours($this->hours)->toDateTimeString())
|
||||
->orderBy('id')
|
||||
->forceDelete();
|
||||
}
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@ -19,7 +19,7 @@ class EmailNoticeTask extends AbstractTask
|
||||
{
|
||||
public function __construct()
|
||||
{
|
||||
//
|
||||
parent::__construct();
|
||||
}
|
||||
|
||||
public function start()
|
||||
|
||||
@ -28,6 +28,7 @@ class IhttpTask extends AbstractTask
|
||||
*/
|
||||
public function __construct($url, $post = [], $extra = [])
|
||||
{
|
||||
parent::__construct(...func_get_args());
|
||||
$this->url = $url;
|
||||
$this->post = $post;
|
||||
$this->extra = $extra;
|
||||
|
||||
@ -25,6 +25,7 @@ class LineTask extends AbstractTask
|
||||
*/
|
||||
public function __construct($userid, bool $online)
|
||||
{
|
||||
parent::__construct(...func_get_args());
|
||||
$this->userid = $userid;
|
||||
$this->online = $online;
|
||||
}
|
||||
|
||||
@ -18,7 +18,7 @@ class LoopTask extends AbstractTask
|
||||
{
|
||||
public function __construct()
|
||||
{
|
||||
|
||||
parent::__construct();
|
||||
}
|
||||
|
||||
public function start()
|
||||
|
||||
@ -27,6 +27,7 @@ class PushTask extends AbstractTask
|
||||
*/
|
||||
public function __construct($params = [], $retryOffline = true)
|
||||
{
|
||||
parent::__construct(...func_get_args());
|
||||
$this->params = $params;
|
||||
$this->retryOffline = $retryOffline;
|
||||
}
|
||||
|
||||
@ -20,6 +20,7 @@ class PushUmengMsg extends AbstractTask
|
||||
*/
|
||||
public function __construct($userid, $array = [])
|
||||
{
|
||||
parent::__construct(...func_get_args());
|
||||
$this->userid = $userid;
|
||||
$this->array = is_array($array) ? $array : [];
|
||||
}
|
||||
|
||||
@ -34,6 +34,7 @@ class WebSocketDialogMsgTask extends AbstractTask
|
||||
*/
|
||||
public function __construct($id, $ignoreFd = null)
|
||||
{
|
||||
parent::__construct(...func_get_args());
|
||||
$this->id = $id;
|
||||
$this->ignoreFd = $ignoreFd === null ? Request::header('fd') : $ignoreFd;
|
||||
}
|
||||
|
||||
@ -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');
|
||||
}
|
||||
}
|
||||
Loading…
x
Reference in New Issue
Block a user