|
2 | 2 |
|
3 | 3 | require __DIR__.'/../vendor/autoload.php';
|
4 | 4 |
|
| 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; |
5 | 9 | use Trig\LaraCron\ExitCodes;
|
| 10 | +use Illuminate\Console\Scheduling\Schedule; |
6 | 11 |
|
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'); |
23 | 13 |
|
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() |
40 | 17 | );
|
41 | 18 |
|
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); |
57 | 21 |
|
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'); |
67 | 26 | }
|
68 | 27 | }
|
| 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 | + ); |
69 | 40 |
|
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 { |
77 | 41 | $cronApp->boot();
|
78 |
| - $cronApp->get(\Illuminate\Console\Application::class)->run(); |
| 42 | + $exitCode = $cronApp->get(\Illuminate\Console\Application::class)->run(); |
| 43 | + exit($exitCode); |
| 44 | + |
79 | 45 | } catch (\Throwable $e) {
|
80 |
| - $io->error($e->getMessage()); |
| 46 | + $io->error(sprintf('[%s] %s', get_class($e), $e->getMessage())); |
81 | 47 | $io->listing(explode("\n", $e->getTraceAsString()));
|
| 48 | + |
| 49 | + if ($e instanceof ExitCommandException) { |
| 50 | + exit($e->getCode()); |
| 51 | + } |
82 | 52 | exit(ExitCodes::ERROR_GENERAL);
|
83 | 53 | }
|
0 commit comments