Skip to content

Commit ad09721

Browse files
committed
Extended ScheduleRunCommand
1 parent 903b14d commit ad09721

7 files changed

+227
-131
lines changed

bin/laracron.php

+35-65
Original file line numberDiff line numberDiff line change
@@ -2,82 +2,52 @@
22

33
require __DIR__.'/../vendor/autoload.php';
44

5+
use Illuminate\Console\OutputStyle;
6+
use Symfony\Component\Console\Input\ArgvInput;
7+
use Symfony\Component\Console\Output\ConsoleOutput;
8+
use Trig\LaraCron\Exception\ExitCommandException;
59
use Trig\LaraCron\ExitCodes;
10+
use Illuminate\Console\Scheduling\Schedule;
611

7-
$io = new \Illuminate\Console\OutputStyle(
8-
new \Symfony\Component\Console\Input\ArgvInput(),
9-
new \Symfony\Component\Console\Output\ConsoleOutput()
10-
);
11-
12-
$configFile = realpath(getcwd()).'/laracron.json';
13-
14-
if (!$configFile || !file_exists($configFile)) {
15-
$io->error("Please initialize configuration with <comment>init</comment> command.");
16-
exit(ExitCodes::ERROR_CONFIG_NOT_FOUND);
17-
}
18-
19-
if (!is_readable($configFile)) {
20-
$io->error("Provided file <comment>{$configFile}</comment> is not readable.");
21-
exit(ExitCodes::ERROR_CONFIG_NOT_READABLE);
22-
}
12+
$configFile = realpath(__DIR__.'/../laracron.json');
2313

24-
$config = json_decode(file_get_contents($configFile), true);
25-
if (JSON_ERROR_NONE !== json_last_error()) {
26-
$io->error("JSON parse error in <comment>{$configFile}</comment>");
27-
exit(ExitCodes::ERROR_CONFIG_JSON_ERROR);
28-
}
29-
30-
$cronApp = new \Trig\LaraCron\CronApplication($config);
31-
32-
$cronApp->booting(
33-
function (\Trig\LaraCron\CronApplication $app) use ($config, $io) {
34-
if ('redis' === ($config['cache.default'] ?? null)) {
35-
if (!class_exists('Redis')) {
36-
$io->error('Please install Redis extension, to use with provided configuration');
37-
}
38-
}
39-
}
14+
$io = new OutputStyle(
15+
new ArgvInput(),
16+
new ConsoleOutput()
4017
);
4118

42-
$cronApp->booted(
43-
function (\Trig\LaraCron\CronApplication $app) use ($io, $config) {
44-
$scheduler = $app->get(\Illuminate\Console\Scheduling\Schedule::class);
45-
foreach ($config['scheduledJobs'] ?? [] as $definition => $commands) {
46-
foreach ($commands as $command) {
47-
$event = $scheduler->exec($command)->name(
48-
implode(
49-
'.',
50-
[
51-
'laracron',
52-
crc32($definition.$command),
53-
preg_replace('/-{2,}/', '-', preg_replace('/[^\w]/', '-', $definition.'/'.$command)),
54-
]
55-
)
56-
)->onOneServer();
19+
try {
20+
$cronApp = new \Trig\LaraCron\CronApplication($configFile);
5721

58-
$isCronDefinition = false !== strpos($definition, ' ');
59-
if ($isCronDefinition) {
60-
$event->cron($definition);
61-
} elseif (method_exists($event, $definition)) {
62-
$event->{$definition}();
63-
} else {
64-
$io->writeln("<fg=red>ERROR:</> Seems that command schedule definition <comment>{$definition}</comment> is wrong for <comment>{$command}</comment> command");
65-
exit(ExitCodes::ERROR_CMD_DEFINITION);
66-
}
22+
$cronApp->booting(
23+
function (\Trig\LaraCron\CronApplication $app) use ($io) {
24+
if ('redis' === ($app->get('config')['cache.default'] ?? null) && !class_exists('Redis')) {
25+
$io->writeln('<fg=red>ERROR:</> Please install Redis extension, to use with provided configuration');
6726
}
6827
}
28+
);
29+
30+
$cronApp->booted(
31+
function (\Trig\LaraCron\CronApplication $app)use ($cronApp) {
32+
$console = $app->get(\Illuminate\Console\Application::class);
33+
$console->add(new \Trig\LaraCron\Command\InitCommand());
34+
$console->add(new \Trig\LaraCron\Command\BuildPharCommand());
35+
$scheduleRun = new \Trig\LaraCron\Command\ScheduleRunCommand($app->get(Schedule::class));
36+
$console->add($scheduleRun);
37+
$scheduleRun->setLaravel($cronApp);
38+
}
39+
);
6940

70-
$console = $app->get(\Illuminate\Console\Application::class);
71-
$console->add(new \Trig\LaraCron\Command\InitCommand());
72-
$console->add(new \Trig\LaraCron\Command\BuildPharCommand());
73-
}
74-
);
75-
76-
try {
7741
$cronApp->boot();
78-
$cronApp->get(\Illuminate\Console\Application::class)->run();
42+
$exitCode = $cronApp->get(\Illuminate\Console\Application::class)->run();
43+
exit($exitCode);
44+
7945
} catch (\Throwable $e) {
80-
$io->error($e->getMessage());
46+
$io->error(sprintf('[%s] %s', get_class($e), $e->getMessage()));
8147
$io->listing(explode("\n", $e->getTraceAsString()));
48+
49+
if ($e instanceof ExitCommandException) {
50+
exit($e->getCode());
51+
}
8252
exit(ExitCodes::ERROR_GENERAL);
8353
}

composer.json

+23-20
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,27 @@
11
{
2-
"name": "trig/laracron",
3-
"require": {
4-
"illuminate/console": "^5.7",
5-
"illuminate/container": "^5.7",
6-
"illuminate/events": "^5.7",
7-
"illuminate/cache": "^5.7",
8-
"illuminate/filesystem": "^5.7",
9-
"dragonmantank/cron-expression": "^2.2",
10-
"symfony/console": "^4.1",
11-
"symfony/process": "^4.1"
2+
"name": "trig/laracron",
3+
"require": {
4+
"illuminate/console": "^5.7",
5+
"illuminate/container": "^5.7",
6+
"illuminate/events": "^5.7",
7+
"illuminate/cache": "^5.7",
8+
"illuminate/filesystem": "^5.7",
9+
"dragonmantank/cron-expression": "^2.2",
10+
"symfony/console": "^4.1",
11+
"symfony/process": "^4.1"
12+
},
13+
"autoload": {
14+
"psr-4": {
15+
"Trig\\": "src//"
1216
},
13-
"autoload": {
14-
"psr-4":{
15-
"Trig\\": "src//"
16-
}
17-
},
18-
"authors": [
19-
{
20-
"name": "Andrey",
21-
"email": "[email protected]"
22-
}
17+
"files": [
18+
"src/functions.php"
2319
]
20+
},
21+
"authors": [
22+
{
23+
"name": "Andrey",
24+
"email": "[email protected]"
25+
}
26+
]
2427
}

laracron.json

-1
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@
1010
},
1111

1212
"log file": "php://stdout",
13-
"basePath": ".",
1413
"cache.default": "file",
1514
"cache.prefix": "laracron",
1615
"cache.stores.file": {
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
<?php
2+
3+
namespace Trig\LaraCron\Command;
4+
5+
use Illuminate\Console\Scheduling\Schedule;
6+
use Illuminate\Console\Scheduling\ScheduleRunCommand as ParentCommand;
7+
use Trig\LaraCron\Exception\ExitCommandException;
8+
9+
class ScheduleRunCommand extends ParentCommand
10+
{
11+
12+
public function handle()
13+
{
14+
foreach ($this->getLaravel()->get('config')['scheduledJobs'] ?? [] as $definition => $commands) {
15+
foreach ($commands as $command) {
16+
$event = $this->schedule->exec($command)
17+
->name($this->getUniqueName($definition, $command))
18+
->onOneServer();
19+
20+
$isCronDefinition = false !== strpos($definition, ' ');
21+
22+
if ($isCronDefinition) {
23+
$event->cron($definition);
24+
} elseif (method_exists($event, $definition)) {
25+
$event->{$definition}();
26+
} else {
27+
$this->output->writeln("<fg=red>ERROR:</> Seems that command schedule definition <comment>{$definition}</comment> is wrong for <comment>{$command}</comment> command");
28+
throw new ExitCommandException('ERROR_CMD_DEFINITION', ExitCodes::ERROR_CMD_DEFINITION);
29+
}
30+
}
31+
}
32+
33+
parent::handle();
34+
}
35+
36+
/**
37+
* @param string $definition
38+
* @param string $command
39+
* @return string
40+
*/
41+
private function getUniqueName(string $definition, string $command): string
42+
{
43+
return implode(
44+
'.',
45+
[
46+
'laracron',
47+
crc32($definition.$command),
48+
preg_replace('/-{2,}/', '-', preg_replace('/[^\w]/', '-', $definition.'/'.$command)),
49+
]
50+
);
51+
}
52+
}

0 commit comments

Comments
 (0)