1 | <?php |
||
2 | |||
3 | /** |
||
4 | * Cron.php |
||
5 | * Copyright (c) 2020 [email protected] |
||
6 | * |
||
7 | * This file is part of Firefly III (https://github.com/firefly-iii). |
||
8 | * |
||
9 | * This program is free software: you can redistribute it and/or modify |
||
10 | * it under the terms of the GNU Affero General Public License as |
||
11 | * published by the Free Software Foundation, either version 3 of the |
||
12 | * License, or (at your option) any later version. |
||
13 | * |
||
14 | * This program is distributed in the hope that it will be useful, |
||
15 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
||
16 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
||
17 | * GNU Affero General Public License for more details. |
||
18 | * |
||
19 | * You should have received a copy of the GNU Affero General Public License |
||
20 | * along with this program. If not, see <https://www.gnu.org/licenses/>. |
||
21 | */ |
||
22 | |||
23 | declare(strict_types=1); |
||
24 | |||
25 | namespace FireflyIII\Console\Commands\Tools; |
||
26 | |||
27 | use Carbon\Carbon; |
||
28 | use Exception; |
||
29 | use FireflyIII\Exceptions\FireflyException; |
||
30 | use FireflyIII\Support\Cronjobs\AutoBudgetCronjob; |
||
31 | use FireflyIII\Support\Cronjobs\RecurringCronjob; |
||
32 | use FireflyIII\Support\Cronjobs\TelemetryCronjob; |
||
33 | use Illuminate\Console\Command; |
||
34 | use InvalidArgumentException; |
||
35 | use Log; |
||
36 | |||
37 | /** |
||
38 | * Class Cron |
||
39 | * |
||
40 | * @codeCoverageIgnore |
||
41 | */ |
||
42 | class Cron extends Command |
||
43 | { |
||
44 | /** |
||
45 | * The console command description. |
||
46 | * |
||
47 | * @var string |
||
48 | */ |
||
49 | protected $description = 'Runs all Firefly III cron-job related commands. Configure a cron job according to the official Firefly III documentation.'; |
||
50 | /** |
||
51 | * The name and signature of the console command. |
||
52 | * |
||
53 | * @var string |
||
54 | */ |
||
55 | protected $signature = 'firefly-iii:cron |
||
56 | {--F|force : Force the cron job(s) to execute.} |
||
57 | {--date= : Set the date in YYYY-MM-DD to make Firefly III think that\'s the current date.} |
||
58 | '; |
||
59 | |||
60 | /** |
||
61 | * @return int |
||
62 | */ |
||
63 | public function handle(): int |
||
64 | { |
||
65 | $date = null; |
||
0 ignored issues
–
show
Unused Code
introduced
by
Loading history...
|
|||
66 | try { |
||
67 | $date = new Carbon($this->option('date')); |
||
68 | } catch (InvalidArgumentException|Exception $e) { |
||
69 | $this->error(sprintf('"%s" is not a valid date', $this->option('date'))); |
||
70 | $e->getMessage(); |
||
71 | } |
||
72 | $force = (bool) $this->option('force'); |
||
73 | |||
74 | /* |
||
75 | * Fire recurring transaction cron job. |
||
76 | */ |
||
77 | try { |
||
78 | $this->recurringCronJob($force, $date); |
||
79 | } catch (FireflyException $e) { |
||
80 | Log::error($e->getMessage()); |
||
81 | Log::error($e->getTraceAsString()); |
||
82 | $this->error($e->getMessage()); |
||
83 | } |
||
84 | |||
85 | /* |
||
86 | * Fire auto-budget cron job: |
||
87 | */ |
||
88 | try { |
||
89 | $this->autoBudgetCronJob($force, $date); |
||
90 | } catch (FireflyException $e) { |
||
91 | Log::error($e->getMessage()); |
||
92 | Log::error($e->getTraceAsString()); |
||
93 | $this->error($e->getMessage()); |
||
94 | } |
||
95 | |||
96 | /* |
||
97 | * Fire telemetry cron job (disabled): |
||
98 | */ |
||
99 | try { |
||
100 | //$this->telemetryCronJob($force, $date); |
||
101 | } catch (FireflyException $e) { |
||
102 | Log::error($e->getMessage()); |
||
103 | Log::error($e->getTraceAsString()); |
||
104 | $this->error($e->getMessage()); |
||
105 | } |
||
106 | |||
107 | $this->info('More feedback on the cron jobs can be found in the log files.'); |
||
108 | |||
109 | // app('telemetry')->feature('executed-command', $this->signature); |
||
110 | return 0; |
||
111 | } |
||
112 | |||
113 | /** |
||
114 | * @param bool $force |
||
115 | * @param Carbon|null $date |
||
116 | * |
||
117 | * @throws FireflyException |
||
118 | * @throws Exception |
||
119 | */ |
||
120 | private function autoBudgetCronJob(bool $force, ?Carbon $date): void |
||
121 | { |
||
122 | $autoBudget = new AutoBudgetCronjob; |
||
123 | $autoBudget->setForce($force); |
||
124 | // set date in cron job: |
||
125 | if (null !== $date) { |
||
126 | $autoBudget->setDate($date); |
||
127 | } |
||
128 | |||
129 | $result = $autoBudget->fire(); |
||
130 | |||
131 | if (false === $result) { |
||
132 | $this->line('The auto budget cron job did not fire.'); |
||
133 | } |
||
134 | if (true === $result) { |
||
135 | $this->line('The auto budget cron job fired successfully.'); |
||
136 | } |
||
137 | |||
138 | } |
||
139 | |||
140 | /** |
||
141 | * @param bool $force |
||
142 | * @param Carbon|null $date |
||
143 | * |
||
144 | * @throws FireflyException |
||
145 | */ |
||
146 | private function recurringCronJob(bool $force, ?Carbon $date): void |
||
147 | { |
||
148 | $recurring = new RecurringCronjob; |
||
149 | $recurring->setForce($force); |
||
150 | |||
151 | // set date in cron job: |
||
152 | if (null !== $date) { |
||
153 | $recurring->setDate($date); |
||
154 | } |
||
155 | |||
156 | $result = $recurring->fire(); |
||
157 | |||
158 | if (false === $result) { |
||
159 | $this->line('The recurring transaction cron job did not fire.'); |
||
160 | } |
||
161 | if (true === $result) { |
||
162 | $this->line('The recurring transaction cron job fired successfully.'); |
||
163 | } |
||
164 | } |
||
165 | |||
166 | /** |
||
167 | * @param bool $force |
||
168 | * @param Carbon|null $date |
||
169 | */ |
||
170 | private function telemetryCronJob(bool $force, ?Carbon $date): void |
||
0 ignored issues
–
show
|
|||
171 | { |
||
172 | if (false === config('firefly.send_telemetry') || false === config('firefly.feature_flags.telemetry')) { |
||
173 | // if not configured to do anything with telemetry, do nothing. |
||
174 | return; |
||
175 | } |
||
176 | $telemetry = new TelemetryCronJob; |
||
177 | $telemetry->setForce($force); |
||
178 | |||
179 | // set date in cron job: |
||
180 | if (null !== $date) { |
||
181 | $telemetry->setDate($date); |
||
182 | } |
||
183 | |||
184 | $result = $telemetry->fire(); |
||
185 | |||
186 | if (false === $result) { |
||
187 | $this->line('The telemetry cron job did not fire.'); |
||
188 | } |
||
189 | if (true === $result) { |
||
190 | $this->line('The telemetry cron job fired successfully.'); |
||
191 | } |
||
192 | |||
193 | } |
||
194 | |||
195 | } |
||
196 |