BiliHelper-personal/src/Task/Task.php
2022-06-03 23:34:44 +08:00

69 lines
1.8 KiB
PHP
Raw Blame History

This file contains invisible Unicode characters

This file contains invisible Unicode characters that are indistinguishable to humans but may be processed differently by a computer. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

<?php declare(strict_types=1);
/**
* Website: https://mudew.com/
* Author: Lkeme
* License: The MIT License
* Email: Useri@live.cn
* Updated: 2022 ~ 2023
*
* _____ _ _ _ _ _ _____ _ _____ _____ _____
* | _ \ | | | | | | | | | | | ____| | | | _ \ | ____| | _ \ & l、
* | |_| | | | | | | | | |_| | | |__ | | | |_| | | |__ | |_| | (゚、 。
* | _ { | | | | | | | _ | | __| | | | ___/ | __| | _ /   \、゙ ~ヽ *
* | |_| | | | | |___ | | | | | | | |___ | |___ | | | |___ | | \ \  じしf_, )
* |_____/ |_| |_____| |_| |_| |_| |_____| |_____| |_| |_____| |_| \_\
*/
namespace Bhp\Task;
use Amp\Loop;
use Bhp\Log\Log;
use Bhp\Plugin\Plugin;
use Bhp\TimeLock\TimeLock;
use Bhp\Util\DesignPattern\SingleTon;
use Throwable;
use function Amp\asyncCall;
class Task extends SingleTon
{
/**
* @return void
*/
public function init(): void
{
}
/**
* @param string $hook
* @param mixed $data
* @return void
*/
public static function addTask(string $hook, mixed ...$data): void
{
asyncCall(function () use ($hook, $data) {
while (true) {
try {
Plugin::getInstance()->trigger($hook, ...$data);
} catch (Throwable $e) {
// TODO 多次错误删除tasks_***.json文件
$error_msg = "MSG: {$e->getMessage()} CODE: {$e->getCode()} FILE: {$e->getFile()} LINE: {$e->getLine()}";
Log::error($error_msg);
// Notice::push('error', $error_msg);
}
yield TimeLock::Delayed();
}
});
}
/**
* @return void
*/
public static function execTasks(): void
{
Loop::run();
}
}