Skip to content

Commit

Permalink
crontab
Browse files Browse the repository at this point in the history
  • Loading branch information
guanhui07 committed Feb 11, 2023
1 parent c70a040 commit 2e7341f
Show file tree
Hide file tree
Showing 9 changed files with 122 additions and 29 deletions.
5 changes: 1 addition & 4 deletions app/Controller/Controller.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,6 @@
use dcr\Request\Request;
use dcr\Response\Response;
use DI\Attribute\Inject;
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Database\Eloquent\Collection;
use Illuminate\Database\Eloquent\Model;
use Middlewares\Utils\Dispatcher;
use Exception;

Expand Down Expand Up @@ -135,7 +132,7 @@ public function getUserId()
return self::$user['id'] ?? 0;
}

public function getUser(): Model|Collection|Builder|array|null
public function getUser(): \Illuminate\Database\Eloquent\Model|\Illuminate\Database\Eloquent\Collection|\Illuminate\Database\Eloquent\Builder|array|null
{
return UserModel::query()->find($this->getUserId());
}
Expand Down
9 changes: 9 additions & 0 deletions app/Crontab/Contract/CrontabInterface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<?php
declare(strict_types = 1);

namespace app\Crontab\Contract;

interface CrontabInterface
{
public function execute();
}
27 changes: 27 additions & 0 deletions app/Crontab/TestCrontab.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<?php
declare(strict_types = 1);

namespace app\Crontab;


use app\Crontab\Contract\CrontabInterface;
use app\Repository\TestRepository;

/**
*
* 需要在 /config/crontab.php 配置
* Class TestCrontab
* @package app\Crontab
*/
class TestCrontab implements CrontabInterface
{
public string $name = 'test';
public string $desc = 'just test demo';

public function execute(): void
{
echo 'test crontab'.PHP_EOL;
// di()->get(TestRepository::class)->test1();
// di()->get(TestRepository::class)->fromRepos();
}
}
8 changes: 5 additions & 3 deletions app/helpers.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,10 @@
use app\Utils\Config;
use app\Utils\Json;
use app\Utils\Str;
use dcr\Container;
use dcr\EventInstance;
use dcr\Request\Request;
use dcr\Utils\ApplicationContext;
use GatewayClient\Gateway;
use Illuminate\Contracts\Support\Arrayable;
use Illuminate\Support\Collection;
Expand Down Expand Up @@ -458,10 +460,10 @@ function real_ip()

function di($name = null): \DI\Container
{
if ($name == null) {
return \dcr\Container::instance();
if ($name === null) {
return ApplicationContext::getContainer();
}
return \dcr\Container::instance()->get($name);
return ApplicationContext::getContainer()->get($name);
}


Expand Down
31 changes: 9 additions & 22 deletions bin/crontab.php
Original file line number Diff line number Diff line change
@@ -1,24 +1,22 @@
<?php
declare(strict_types = 1);

//namespace app\Command;

require_once('./vendor/autoload.php');

use app\Repository\TestRepository;
use dcr\Boostrap;
use dcr\Container;
use Workerman\Crontab\Crontab as WmCrontab;
use Workerman\Worker;

define('PROJECT_ROOT', dirname(__DIR__).'/');
! defined('BASE_PATH') && define('BASE_PATH', dirname(__DIR__, 1));
!defined('BASE_PATH') && define('BASE_PATH', dirname(__DIR__, 1));
$container = Container::instance();

// 初始化 注册 config env db orm facade门面
/** @var Boostrap $bootstrap */
$bootstrap = $container->make(Boostrap::class);
$bootstrap->run();

/**
* @see https://github.com/walkor/crontab
* Class Crontab
Expand All @@ -28,31 +26,20 @@ class crontab
{
public function run()
{
// $container = Container::instance();
//
// // 初始化 注册 config env db orm facade门面
// //(new \dcr\Boostrap())->run();
// /** @var \dcr\Boostrap $bootstrap */
// $bootstrap = $container->make(\dcr\Boostrap::class);
// $bootstrap->run();
//
// \dcr\CommandDcr::bootstrap();


$worker = new Worker();

$worker->onWorkerStart = static function () {
// new WmCrontab('*/1 * * * * *', function(){
// echo date('Y-m-d H:i:s')."\n";
// });
new WmCrontab('*/1 * * * * *', function(){
di()->get(TestRepository::class)->test();
});

$allCrontab = config('crontab.crontab');
foreach ($allCrontab as $k => $crontab) {
new WmCrontab($crontab[0], function () use ($crontab) {
di()->get($crontab[1])->execute();
});
}
};

Worker::runAll();
}

}

(new crontab())->run();
Expand Down
26 changes: 26 additions & 0 deletions config/config.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<?php

use Hyperf\Di\Annotation\AspectCollector;

return [
// 扫描aop Aspect
'annotations' => [
'scan' => [
'paths' => [
BASE_PATH . '/app',
],
'ignore_annotations' => [
'mixin',
],
'class_map' => [
],
'collectors' => [
AspectCollector::class
],
],
],
'aspects' => [
// 这里写入对应的 Aspect
app\Aspect\DebugAspect::class,
]
];
14 changes: 14 additions & 0 deletions config/crontab.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<?php
declare(strict_types=1);

use app\Crontab\TestCrontab;
use Workerman\Crontab\Crontab as WmCrontab;

return [
// 'enable' => $_ENV['crontab_enable'] ?? true,

'crontab' => [
['*/1 * * * * *',TestCrontab::class],

],
];
2 changes: 2 additions & 0 deletions dcr/Container.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@

namespace dcr;

use dcr\Utils\ApplicationContext;
use DI\ContainerBuilder;
use Exception;

Expand Down Expand Up @@ -46,6 +47,7 @@ public static function instance(): \DI\Container
//
// $container->make($config,[]);
// }
ApplicationContext::setContainer($container);
self::$ins = $container;
return $container;
}
Expand Down
29 changes: 29 additions & 0 deletions dcr/Utils/ApplicationContext.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
<?php

declare(strict_types=1);

namespace dcr\Utils;

class ApplicationContext
{
/**
* @var \DI\Container
*/
private static $container;

public static function getContainer(): \DI\Container
{
return self::$container;
}

public static function hasContainer(): bool
{
return isset(self::$container);
}

public static function setContainer(\DI\Container $container): \DI\Container
{
self::$container = $container;
return $container;
}
}

0 comments on commit 2e7341f

Please sign in to comment.