2021-03-19 17:47:41 +08:00
|
|
|
<?php
|
|
|
|
|
|
|
|
|
|
|
2021-03-26 01:01:21 +08:00
|
|
|
namespace Snowflake\Crontab;
|
2021-03-19 17:47:41 +08:00
|
|
|
|
|
|
|
|
|
|
|
|
|
use Closure;
|
|
|
|
|
use Exception;
|
2021-03-19 19:16:12 +08:00
|
|
|
use Snowflake\Abstracts\BaseObject;
|
2021-03-26 01:01:21 +08:00
|
|
|
use Snowflake\Event;
|
2021-03-20 02:33:50 +08:00
|
|
|
use Swoole\Timer;
|
2021-03-19 17:47:41 +08:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Class Async
|
|
|
|
|
* @package Snowflake
|
|
|
|
|
*/
|
2021-03-19 19:16:12 +08:00
|
|
|
class Crontab extends BaseObject
|
2021-03-19 17:47:41 +08:00
|
|
|
{
|
|
|
|
|
|
|
|
|
|
|
2021-03-20 02:33:50 +08:00
|
|
|
private array|Closure $handler;
|
2021-03-19 18:52:30 +08:00
|
|
|
|
|
|
|
|
|
2021-03-20 02:33:50 +08:00
|
|
|
private string $name = '';
|
2021-03-19 18:52:30 +08:00
|
|
|
|
|
|
|
|
|
2021-03-26 01:53:26 +08:00
|
|
|
private mixed $params = null;
|
2021-03-20 02:33:50 +08:00
|
|
|
|
|
|
|
|
|
|
|
|
|
private int $tickTime = 1;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
private bool $isLoop = false;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
private int $timerId = -1;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
private int $max_execute_number = -1;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
private int $execute_number = 0;
|
|
|
|
|
|
2021-03-21 23:53:48 +08:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @return $this
|
|
|
|
|
*/
|
|
|
|
|
public function increment(): static
|
|
|
|
|
{
|
|
|
|
|
$this->execute_number += 1;
|
|
|
|
|
return $this;
|
|
|
|
|
}
|
|
|
|
|
|
2021-03-20 02:33:50 +08:00
|
|
|
/**
|
|
|
|
|
* @return array|Closure
|
|
|
|
|
*/
|
|
|
|
|
public function getHandler(): array|Closure
|
|
|
|
|
{
|
|
|
|
|
return $this->handler;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @return string
|
|
|
|
|
*/
|
|
|
|
|
public function getName(): string
|
|
|
|
|
{
|
|
|
|
|
return $this->name;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @return mixed
|
|
|
|
|
*/
|
|
|
|
|
public function getParams(): mixed
|
|
|
|
|
{
|
|
|
|
|
return $this->params;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @return int
|
|
|
|
|
*/
|
|
|
|
|
public function getTickTime(): int
|
|
|
|
|
{
|
2021-03-22 00:28:27 +08:00
|
|
|
return $this->tickTime;
|
2021-03-20 02:33:50 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @return bool
|
|
|
|
|
*/
|
|
|
|
|
public function isLoop(): bool
|
|
|
|
|
{
|
|
|
|
|
return $this->isLoop;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @return int
|
|
|
|
|
*/
|
|
|
|
|
public function getMaxExecuteNumber(): int
|
|
|
|
|
{
|
|
|
|
|
return $this->max_execute_number;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @return int
|
|
|
|
|
*/
|
|
|
|
|
public function getExecuteNumber(): int
|
|
|
|
|
{
|
|
|
|
|
return $this->execute_number;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @param array|Closure $handler
|
|
|
|
|
*/
|
|
|
|
|
public function setHandler(array|Closure $handler): void
|
|
|
|
|
{
|
|
|
|
|
$this->handler = $handler;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @param string $name
|
|
|
|
|
*/
|
|
|
|
|
public function setName(string $name): void
|
|
|
|
|
{
|
|
|
|
|
$this->name = $name;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @param mixed $params
|
|
|
|
|
*/
|
|
|
|
|
public function setParams(mixed $params): void
|
|
|
|
|
{
|
|
|
|
|
$this->params = $params;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @param int $tickTime
|
|
|
|
|
*/
|
|
|
|
|
public function setTickTime(int $tickTime): void
|
|
|
|
|
{
|
|
|
|
|
$this->tickTime = $tickTime;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @param bool $isLoop
|
|
|
|
|
*/
|
|
|
|
|
public function setIsLoop(bool $isLoop): void
|
|
|
|
|
{
|
|
|
|
|
$this->isLoop = $isLoop;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @param int $max_execute_number
|
|
|
|
|
*/
|
|
|
|
|
public function setMaxExecuteNumber(int $max_execute_number): void
|
|
|
|
|
{
|
|
|
|
|
$this->max_execute_number = $max_execute_number;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @param int $execute_number
|
|
|
|
|
*/
|
|
|
|
|
public function setExecuteNumber(int $execute_number): void
|
|
|
|
|
{
|
|
|
|
|
$this->execute_number = $execute_number;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @return int
|
|
|
|
|
*/
|
|
|
|
|
public function getTimerId(): int
|
|
|
|
|
{
|
|
|
|
|
return $this->timerId;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @param int $timerId
|
|
|
|
|
*/
|
|
|
|
|
public function setTimerId(int $timerId): void
|
|
|
|
|
{
|
|
|
|
|
$this->timerId = $timerId;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2021-03-26 01:47:24 +08:00
|
|
|
/**
|
|
|
|
|
*
|
|
|
|
|
* @throws Exception
|
|
|
|
|
*/
|
2021-03-20 02:33:50 +08:00
|
|
|
public function clearTimer()
|
|
|
|
|
{
|
|
|
|
|
$this->warning('crontab timer clear.');
|
|
|
|
|
if (Timer::exists($this->timerId)) {
|
|
|
|
|
Timer::clear($this->timerId);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @throws Exception
|
|
|
|
|
*/
|
2021-03-21 23:53:48 +08:00
|
|
|
public function execute(): void
|
2021-03-20 02:33:50 +08:00
|
|
|
{
|
2021-03-26 02:20:17 +08:00
|
|
|
$params = call_user_func($this->handler, $this->params, $this->name);
|
2021-04-11 15:38:23 +08:00
|
|
|
\redis()->hDel('crontab:wait:execute', $this->getName());
|
2021-03-26 02:20:17 +08:00
|
|
|
if ($params !== null) {
|
|
|
|
|
$name = date('Y_m_d_H_i_s.' . $this->name . '.log');
|
|
|
|
|
write(storage($name, '/log/crontab'), serialize($params));
|
|
|
|
|
}
|
2021-03-20 02:33:50 +08:00
|
|
|
}
|
2021-03-19 18:52:30 +08:00
|
|
|
|
|
|
|
|
|
2021-03-19 17:47:41 +08:00
|
|
|
}
|