Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion CHANGELOG-2.0.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,5 @@

## Added

- [#53](https://github.com/mineadmin/components/pull/53) Splitting components http-server
- [#53](https://github.com/mineadmin/components/pull/53) Splitting components http-server
- [#55](https://github.com/mineadmin/components/pull/55) Splitting the crontab component
3 changes: 2 additions & 1 deletion CHANGELOG-2.0.zh_CN.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,5 @@

## Added

- [#53](https://github.com/mineadmin/components/pull/53) 拆分组件 http-server
- [#53](https://github.com/mineadmin/components/pull/53) 拆分组件 http-server
- [#55](https://github.com/mineadmin/components/pull/55) 拆分优化组件 crontab
6 changes: 4 additions & 2 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,8 @@
"Mine\\Generator\\": "src/mine-generator/src",
"Xmo\\AppStore\\": "src/app-store/src",
"Mine\\NextCoreX\\": "src/next-core-x/src",
"Mine\\HttpServer\\": "src/HttpServer/src"
"Mine\\HttpServer\\": "src/HttpServer/src",
"Mine\\Crontab\\": "src/Crontab/src"
},
"files": [
"src/mine-helpers/src/functions.php"
Expand All @@ -32,7 +33,8 @@
"Xmo\\AppStore\\Tests\\": "src/app-store/tests",
"Xmo\\MineCore\\Tests\\": "src/mine-core/tests",
"Mine\\NextCoreX\\Tests\\": "src/next-core-x/tests",
"Mine\\HttpServer\\Tests\\": "src/HttpServer/tests"
"Mine\\HttpServer\\Tests\\": "src/HttpServer/tests",
"Mine\\Crontab\\Tests\\": "src/Crontab/tests"
}
},
"authors": [
Expand Down
1 change: 1 addition & 0 deletions phpunit.xml
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
<coverage/>
<testsuites>
<testsuite name="Tests">
<directory suffix="Test.php">./src/Crontab/tests</directory>
<directory suffix="Test.php">./src/HttpServer/tests</directory>
<directory suffix="Test.php">./src/app-store/tests</directory>
<directory suffix="Test.php">./src/mine-core/tests</directory>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
<?php

declare(strict_types=1);
/**
* This file is part of MineAdmin.
*
* @link https://www.mineadmin.com
* @document https://doc.mineadmin.com
* @contact root@imoi.cn
* @license https://github.com/mineadmin/MineAdmin/blob/master/LICENSE
*/
use Hyperf\Database\Migrations\Migration;
use Hyperf\Database\Schema\Blueprint;
use Hyperf\Database\Schema\Schema;

class CreateCrontab extends Migration
{
/**
* Run the migrations.
*/
public function up(): void
{
Schema::create('crontab', function (Blueprint $table) {
$table->bigIncrements('id');
/*
* name string 30
* status tinyint 1 default 0
* memo string 60 default null
* type string 10 not null
* value string longtext not null
*/
$table->string('name', 30);
$table->tinyInteger('status')->default(0);
$table->tinyInteger('is_on_one_server')->default(0);
$table->tinyInteger('is_singleton')->default(0);
$table->string('memo', 60)->default(null);
$table->string('type', 10);
$table->string('rule', 10);
$table->text('value');
$table->datetimes();
});
}

/**
* Reverse the migrations.
*/
public function down(): void
{
Schema::dropIfExists('crontab');
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
<?php

declare(strict_types=1);
/**
* This file is part of MineAdmin.
*
* @link https://www.mineadmin.com
* @document https://doc.mineadmin.com
* @contact root@imoi.cn
* @license https://github.com/mineadmin/MineAdmin/blob/master/LICENSE
*/
use Hyperf\Database\Migrations\Migration;
use Hyperf\Database\Schema\Blueprint;
use Hyperf\Database\Schema\Schema;

class CreateCrontabExecuteLog extends Migration
{
/**
* Run the migrations.
*/
public function up(): void
{
Schema::create('crontab_execute_log', function (Blueprint $table) {
$table->bigIncrements('id');
$table->bigInteger('crontab_id');
$table->string('name', 100);
$table->tinyInteger('status')->default(0);
$table->string('target', 200);
$table->text('exception_info');
$table->datetimes();
});
}

/**
* Reverse the migrations.
*/
public function down(): void
{
Schema::dropIfExists('crontab_execute_log');
}
}
3 changes: 3 additions & 0 deletions src/Crontab/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# Hyperf Crontab 加强

在原有的基础上增加了定时任务从数据库读取的配置
35 changes: 35 additions & 0 deletions src/Crontab/composer.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
{
"name": "mineadmin/crontab",
"description": "加强 Hyperf Crontab",
"type": "library",
"license": "MIT",
"authors": [
{
"name": "x.mo",
"role": "Developer"
},
{
"name": "zds",
"role": "Developer"
}
],
"require": {
"hyperf/crontab": "^3.1",
"Hyperf/database": "^3.1"
},
"autoload": {
"psr-4": {
"Mine\\Crontab\\": "src/"
}
},
"autoload-dev": {
"psr-4": {
"Mine\\Crontab\\Tests\\": "tests/"
}
},
"extra": {
"hyperf": {
"config": "\\Mine\\Crontab\\ConfigProvider"
}
}
}
53 changes: 53 additions & 0 deletions src/Crontab/src/Aspect/CrontabExecutorAspect.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
<?php

declare(strict_types=1);
/**
* This file is part of MineAdmin.
*
* @link https://www.mineadmin.com
* @document https://doc.mineadmin.com
* @contact root@imoi.cn
* @license https://github.com/mineadmin/MineAdmin/blob/master/LICENSE
*/

namespace Mine\Crontab\Aspect;

use Hyperf\Crontab\Crontab;
use Hyperf\Crontab\Strategy\Executor;
use Hyperf\DbConnection\Db;
use Hyperf\Di\Aop\AbstractAspect;
use Hyperf\Di\Aop\ProceedingJoinPoint;
use Mine\Crontab\CrontabContainer;

class CrontabExecutorAspect extends AbstractAspect
{
public array $classes = [
Executor::class . '::logResult',
];

public function process(ProceedingJoinPoint $proceedingJoinPoint)
{
/**
* @var Crontab $crontab
* @var bool $isSuccess
* @var null|\Throwable $throwable
*/
[$crontab, $isSuccess, $throwable] = $proceedingJoinPoint->getArguments();
if ($crontab instanceof \Mine\Crontab\Crontab) {
$callback = $crontab->getCallback();
if (is_array($callback)) {
$callback = json_encode($callback, JSON_THROW_ON_ERROR | JSON_UNESCAPED_UNICODE);
}
Db::connection(CrontabContainer::$connectionName)
->table('crontab_execute_log')
->insert([
'crontab_id' => $crontab->getCronId(),
'name' => $crontab->getName(),
'target' => $callback,
'status' => $isSuccess ? 1 : 0,
'exception_info' => $throwable === null ? '' : $throwable->getMessage(),
]);
}
return $proceedingJoinPoint->process();
}
}
49 changes: 49 additions & 0 deletions src/Crontab/src/Command/CrontabMigrateCommand.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
<?php

declare(strict_types=1);
/**
* This file is part of MineAdmin.
*
* @link https://www.mineadmin.com
* @document https://doc.mineadmin.com
* @contact root@imoi.cn
* @license https://github.com/mineadmin/MineAdmin/blob/master/LICENSE
*/

namespace Mine\Crontab\Command;

use Hyperf\Command\Command as Base;
use Hyperf\Database\Migrations\Migrator;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Output\NullOutput;

class CrontabMigrateCommand extends Base
{
protected ?string $name = 'crontab:migrate';

public function __construct(
private readonly Migrator $migrator
) {
parent::__construct();
}

public function __invoke()
{
$connection = $this->input->getOption('connection');
if (empty($connection)) {
$connection = 'default';
}
$migrator = $this->migrator;
$migrator->setConnection($connection);
$this->migrator
->setOutput(new NullOutput())
->run(dirname(__DIR__, 2) . '/Database/Migrations');
}

protected function getOptions(): array
{
return [
new InputOption('connection', 'connection', InputOption::VALUE_OPTIONAL, 'connection name'),
];
}
}
35 changes: 35 additions & 0 deletions src/Crontab/src/ConfigProvider.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
<?php

declare(strict_types=1);
/**
* This file is part of MineAdmin.
*
* @link https://www.mineadmin.com
* @document https://doc.mineadmin.com
* @contact root@imoi.cn
* @license https://github.com/mineadmin/MineAdmin/blob/master/LICENSE
*/

namespace Mine\Crontab;

use Mine\Crontab\Aspect\CrontabExecutorAspect;
use Mine\Crontab\Command\CrontabMigrateCommand;
use Mine\Crontab\Listener\CrontabProcessStarredListener;

class ConfigProvider
{
public function __invoke(): array
{
return [
'listener' => [
CrontabProcessStarredListener::class,
],
'aspects' => [
CrontabExecutorAspect::class,
],
'commands' => [
CrontabMigrateCommand::class,
],
];
}
}
Loading