Skip to content

Commit

Permalink
ExplainCommand
Browse files Browse the repository at this point in the history
  • Loading branch information
mabar committed Mar 4, 2024
1 parent 185e732 commit 5037e23
Show file tree
Hide file tree
Showing 2 changed files with 71 additions and 0 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.

### Added

- `ExplainCommand`
- explains cron expression syntax
- `ListCommand`
- adds `--explain` option to explain whole expression

Expand Down
69 changes: 69 additions & 0 deletions src/Command/ExplainCommand.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
<?php declare(strict_types = 1);

namespace Orisai\Scheduler\Command;

use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;

final class ExplainCommand extends Command
{

public static function getDefaultName(): string
{
return 'scheduler:explain';
}

public static function getDefaultDescription(): string
{
return 'Explain cron expression';
}

protected function execute(InputInterface $input, OutputInterface $output): int
{
$this->explainSyntax($output);

return 0;
}

private function explainSyntax(OutputInterface $output): void
{
$output->writeln(
<<<'CMD'
<fg=yellow>* * * * *</>
- - - - -
| | | | |
| | | | |
| | | | +----- day of week (0-7) (Sunday = 0 or 7) (or SUN-SAT)
| | | +--------- month (1-12) (or JAN-DEC)
| | +------------- day of month (1-31)
| +----------------- hour (0-23)
+--------------------- minute (0-59)
Each part of expression can also use wildcard, lists, ranges and steps:
- wildcard - <fg=yellow>* * * * *</> - At every minute.
- lists - e.g. <fg=yellow>15,30 * * * *</> - At minute 15 and 30.
- ranges - e.g. <fg=yellow>1-9 * * * *</> - At every minute from 1 through 9.
- steps
- e.g. <fg=yellow>*/5 * * * *</> - At every 5th minute.
- e.g. <fg=yellow>0-30/5 * * * *</> - At every 5th minute from 0 through 30.
- combinations - e.g. <fg=yellow>0-14,30-44 * * * *</> - At every minute from 0 through 14 and every minute from 30 through 44.
You can also use macro instead of an expression:
- <fg=yellow>@yearly</>, <fg=yellow>@annually</> - Run once a year, midnight, Jan. 1 - <fg=yellow>0 0 1 1 *</>
- <fg=yellow>@monthly</> - Run once a month, midnight, first of month - <fg=yellow>0 0 1 * *</>
- <fg=yellow>@weekly</> - Run once a week, midnight on Sun - <fg=yellow>0 0 * * 0</>
- <fg=yellow>@daily</>, <fg=yellow>@midnight</> - Run once a day, midnight - <fg=yellow>0 0 * * *</>
- <fg=yellow>@hourly</> - Run once an hour, first minute - <fg=yellow>0 * * * *</>
Although they are not part of cron expression syntax, you can also add to job:
- seconds - repeat job every n seconds
- timezone - run only when cron expression matches within given timezone
CMD,
);
}

}

0 comments on commit 5037e23

Please sign in to comment.