Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added caron 3.x support #6591

Draft
wants to merge 13 commits into
base: master
Choose a base branch
from
42 changes: 37 additions & 5 deletions .github/workflows/test-components.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,38 @@ on:
env:
SW_VERSION: 'v5.1.2'
jobs:
crontab:
name: Test for Crontab
runs-on: 'ubuntu-latest'
env:
PHP_VERSION: ${{ matrix.php-version }}
strategy:
matrix:
php-version: [ '8.3', '8.2', '8.1' ]
carbon-version: [ '^2.0', '^3.0' ]
max-parallel: 2
fail-fast: false
steps:
- name: Checkout
uses: actions/checkout@v4
- name: Setup PHP
uses: shivammathur/setup-php@v2
with:
php-version: ${{ matrix.php-version }}
tools: phpize
extensions: redis, pdo, pdo_mysql, bcmath, swoole
ini-values: opcache.enable_cli=1
coverage: none
- name: Setup Swoole
run: ./.travis/swoole.install.sh
- name: Setup Carbon ${{ matrix.carbon-version }}
run: composer require "nesbot/carbon:${{ matrix.carbon-version }}" --no-update
- name: Setup Packages
run: ./.travis/requirement.install.sh
- name: Run Test Cases
run: |
composer test src/crontab -- --exclude-group NonCoroutine
vendor/bin/phpunit src/crontab --group NonCoroutine
database:
name: Test for Database
runs-on: 'ubuntu-latest'
Expand Down Expand Up @@ -260,22 +292,22 @@ jobs:
composer remove bandwidth-throttle/token-bucket --dev -W
composer remove friendsofphp/php-cs-fixer --dev -W
composer remove zircote/swagger-php --dev -W
composer remove "symfony/*" --dev -W
composer remove "symfony/*" --dev --no-update
- name: Run Test Cases
run: |
./.travis/requirement.install.sh
if [ ${{ matrix.log-version }} == '^1.0' ]
then
composer require "monolog/monolog:^2.0"
fi
composer require "psr/log:${{ matrix.log-version }}"
composer require "psr/log:${{ matrix.log-version }}" --no-update
if [ ${{ matrix.log-version }} == '^3.0' ]
then
composer require "symfony/console:^6.0" --dev -W
composer require "symfony/console:^6.0" --dev --no-update
else
composer require "symfony/console:^5.0" --dev -W
composer require "symfony/console:^5.0" --dev --no-update
fi
composer require symfony/event-dispatcher --dev -W
composer require symfony/event-dispatcher --dev --no-update
composer update -oW
composer analyse src/framework
composer analyse src/server/src/Entry/Logger.php
Expand Down
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@
"mockery/mockery": "^1.0",
"monolog/monolog": "^2.7|^3.1",
"multiplex/socket": "^1.0",
"nesbot/carbon": "^2.0",
"nesbot/carbon": "^2.0|^3.0",
"nikic/fast-route": "^1.3",
"nikic/php-parser": "^4.1",
"opentracing/opentracing": "^1.0",
Expand Down
2 changes: 1 addition & 1 deletion src/crontab/composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
"hyperf/utils": "~3.1.0",
"hyperf/support": "~3.1.0",
"hyperf/tappable": "~3.1.0",
"nesbot/carbon": "^2.0"
"nesbot/carbon": "^2.0|^3.0"
},
"autoload": {
"psr-4": {
Expand Down
2 changes: 1 addition & 1 deletion src/crontab/src/Strategy/Executor.php
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ public function __construct(protected ContainerInterface $container)
public function execute(Crontab $crontab)
{
try {
$diff = Carbon::now()->diffInRealSeconds($crontab->getExecuteTime(), false);
$diff = Carbon::now()->floatDiffInRealSeconds($crontab->getExecuteTime(), false); // @phpstan-ignore-line
$runnable = null;

switch ($crontab->getType()) {
Expand Down
Binary file modified src/crontab/tests/ExecutorTest.php
Binary file not shown.
82 changes: 38 additions & 44 deletions src/crontab/tests/ParserTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,13 +27,7 @@ class ParserTest extends TestCase

protected function setUp(): void
{
$this->timezone = ini_get('date.timezone');
ini_set('date.timezone', 'Asia/Shanghai');
}

protected function tearDown(): void
{
ini_set('date.timezone', $this->timezone);
$this->timezone = 'Asia/Shanghai';
}

public function testIsValid(): void
Expand All @@ -59,116 +53,116 @@ public function testParseSecondLevel()
{
$crontabString = '*/11 * * * * *';
$parser = new Parser();
$startTime = Carbon::createFromTimestamp(1561052867)->startOfMinute();
$result = $parser->parse($crontabString, $startTime->getTimestamp());
$startTime = Carbon::createFromTimestamp(1561052867, $this->timezone)->startOfMinute();
$result = $parser->parse($crontabString, $startTime->getTimestamp(), $this->timezone);
$this->assertSame([
'2019-06-21 01:47:00',
'2019-06-21 01:47:11',
'2019-06-21 01:47:22',
'2019-06-21 01:47:33',
'2019-06-21 01:47:44',
'2019-06-21 01:47:55',
], $this->toDatatime($result));
], $this->toDateTime($result));
/** @var Carbon $last */
$last = end($result);
$result = $parser->parse($crontabString, $last->getTimestamp());
$result = $parser->parse($crontabString, $last->getTimestamp(), $this->timezone);
$this->assertSame([
'2019-06-21 01:47:55',
'2019-06-21 01:48:06',
'2019-06-21 01:48:17',
'2019-06-21 01:48:28',
'2019-06-21 01:48:39',
'2019-06-21 01:48:50',
], $this->toDatatime($result));
], $this->toDateTime($result));
}

public function testParseSecondLevelBetween(): void
{
$crontabString = '10-15/1 * * * * *';
$parser = new Parser();
$startTime = Carbon::createFromTimestamp(1591754280)->startOfMinute();
$result = $parser->parse($crontabString, $startTime->getTimestamp());
$startTime = Carbon::createFromTimestamp(1591754280, $this->timezone)->startOfMinute();
$result = $parser->parse($crontabString, $startTime->getTimestamp(), $this->timezone);
$this->assertSame([
'2020-06-10 09:58:10',
'2020-06-10 09:58:11',
'2020-06-10 09:58:12',
'2020-06-10 09:58:13',
'2020-06-10 09:58:14',
'2020-06-10 09:58:15',
], $this->toDatatime($result));
], $this->toDateTime($result));
}

public function testParseSecondLevelForComma(): void
{
$crontabString = '10-12/1,14-15/1 * * * * *';
$parser = new Parser();
$startTime = Carbon::createFromTimestamp(1591754280)->startOfMinute();
$result = $parser->parse($crontabString, $startTime->getTimestamp());
$startTime = Carbon::createFromTimestamp(1591754280, $this->timezone)->startOfMinute();
$result = $parser->parse($crontabString, $startTime->getTimestamp(), $this->timezone);
$this->assertSame([
'2020-06-10 09:58:10',
'2020-06-10 09:58:11',
'2020-06-10 09:58:12',
'2020-06-10 09:58:14',
'2020-06-10 09:58:15',
], $this->toDatatime($result));
], $this->toDateTime($result));
}

public function testParseSecondLevelWithoutBackslash(): void
{
$crontabString = '10-12,14-15/1 * * * * *';
$parser = new Parser();
$startTime = Carbon::createFromTimestamp(1591754280)->startOfMinute();
$result = $parser->parse($crontabString, $startTime->getTimestamp());
$startTime = Carbon::createFromTimestamp(1591754280, $this->timezone)->startOfMinute();
$result = $parser->parse($crontabString, $startTime->getTimestamp(), $this->timezone);
$this->assertSame([
'2020-06-10 09:58:10',
'2020-06-10 09:58:11',
'2020-06-10 09:58:12',
'2020-06-10 09:58:14',
'2020-06-10 09:58:15',
], $this->toDatatime($result));
], $this->toDateTime($result));
}

public function testParseSecondLevelWithEmptyString()
{
$crontabString = '10,14,,15, * * * * *';
$parser = new Parser();
$startTime = Carbon::createFromTimestamp(1591754280)->startOfMinute();
$result = $parser->parse($crontabString, $startTime->getTimestamp());
$startTime = Carbon::createFromTimestamp(1591754280, $this->timezone)->startOfMinute();
$result = $parser->parse($crontabString, $startTime->getTimestamp(), $this->timezone);
$this->assertSame([
'2020-06-10 09:58:10',
'2020-06-10 09:58:14',
'2020-06-10 09:58:15',
], $this->toDatatime($result));
], $this->toDateTime($result));
}

public function testParseMinuteLevelBetween(): void
{
$crontabString = '10-15/1 10-12/1 10 * * *';
$parser = new Parser();
$startTime = Carbon::createFromTimestamp(1591755010)->startOfMinute();
$result = $parser->parse($crontabString, $startTime->getTimestamp());
$startTime = Carbon::createFromTimestamp(1591755010, $this->timezone)->startOfMinute();
$result = $parser->parse($crontabString, $startTime->getTimestamp(), $this->timezone);
$this->assertSame([
'2020-06-10 10:10:10',
'2020-06-10 10:10:11',
'2020-06-10 10:10:12',
'2020-06-10 10:10:13',
'2020-06-10 10:10:14',
'2020-06-10 10:10:15',
], $this->toDatatime($result));
], $this->toDateTime($result));

$last = end($result);
$result = $parser->parse($crontabString, $last->addMinute()->startOfMinute());
$result = $parser->parse($crontabString, $last->addMinute()->startOfMinute(), $this->timezone);
$this->assertSame([
'2020-06-10 10:11:10',
'2020-06-10 10:11:11',
'2020-06-10 10:11:12',
'2020-06-10 10:11:13',
'2020-06-10 10:11:14',
'2020-06-10 10:11:15',
], $this->toDatatime($result));
], $this->toDateTime($result));

$last = end($result);
$result = $parser->parse($crontabString, $last->addMinute()->startOfMinute());
$result = $parser->parse($crontabString, $last->addMinute()->startOfMinute(), $this->timezone);

$this->assertSame([
'2020-06-10 10:12:10',
Expand All @@ -177,59 +171,59 @@ public function testParseMinuteLevelBetween(): void
'2020-06-10 10:12:13',
'2020-06-10 10:12:14',
'2020-06-10 10:12:15',
], $this->toDatatime($result));
], $this->toDateTime($result));

$last = end($result);
$result = $parser->parse($crontabString, $last->addMinute()->startOfMinute());
$result = $parser->parse($crontabString, $last->addMinute()->startOfMinute(), $this->timezone);

$this->assertSame([], $this->toDatatime($result));
$this->assertSame([], $this->toDateTime($result));
}

public function testParseSecondLevelWithCarbonStartTime()
{
$crontabString = '*/11 * * * * *';
$parser = new Parser();
$startTime = Carbon::createFromTimestamp(1561052867)->startOfMinute();
$result = $parser->parse($crontabString, $startTime);
$startTime = Carbon::createFromTimestamp(1561052867, $this->timezone)->startOfMinute();
$result = $parser->parse($crontabString, $startTime, $this->timezone);
$this->assertSame([
'2019-06-21 01:47:00',
'2019-06-21 01:47:11',
'2019-06-21 01:47:22',
'2019-06-21 01:47:33',
'2019-06-21 01:47:44',
'2019-06-21 01:47:55',
], $this->toDatatime($result));
], $this->toDateTime($result));
/** @var Carbon $last */
$last = end($result);
$result = $parser->parse($crontabString, $last);
$result = $parser->parse($crontabString, $last, $this->timezone);
$this->assertSame([
'2019-06-21 01:47:55',
'2019-06-21 01:48:06',
'2019-06-21 01:48:17',
'2019-06-21 01:48:28',
'2019-06-21 01:48:39',
'2019-06-21 01:48:50',
], $this->toDatatime($result));
], $this->toDateTime($result));
}

public function testParseMinuteLevel()
{
$crontabString = '*/11 * * * *';
$parser = new Parser();
$startTime = Carbon::createFromTimestamp(1561052867)->startOfMinute();
$result = $parser->parse($crontabString, $startTime->getTimestamp());
$this->assertSame([], $this->toDatatime($result));
$startTime = Carbon::createFromTimestamp(1561052867, $this->timezone)->startOfMinute();
$result = $parser->parse($crontabString, $startTime->getTimestamp(), $this->timezone);
$this->assertSame([], $this->toDateTime($result));

$startTime->minute(33);
$result = $parser->parse($crontabString, $startTime->getTimestamp());
$this->assertSame(['2019-06-21 01:33:00'], $this->toDatatime($result));
$result = $parser->parse($crontabString, $startTime->getTimestamp(), $this->timezone);
$this->assertSame(['2019-06-21 01:33:00'], $this->toDateTime($result));
}

/**
* @param Carbon[] $result
* @return string[]
*/
protected function toDatatime(array $result)
protected function toDateTime(array $result)
{
$dates = [];
foreach ($result as $date) {
Expand Down
Binary file added src/crontab/tests/Stub/cron.data
Binary file not shown.
2 changes: 1 addition & 1 deletion src/database/composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
"hyperf/support": "~3.1.0",
"hyperf/tappable": "~3.1.0",
"hyperf/utils": "~3.1.0",
"nesbot/carbon": "^2.0",
"nesbot/carbon": "^2.0|^3.0",
"psr/container": "^1.0|^2.0",
"psr/event-dispatcher": "^1.0"
},
Expand Down