9
9
10
10
namespace Grav \Console \Cli ;
11
11
12
- use Dragonmantank \ Cron \CronExpression ;
12
+ use Cron \CronExpression ;
13
13
use Grav \Common \Grav ;
14
14
use Grav \Common \Utils ;
15
15
use Grav \Common \Scheduler \Scheduler ;
@@ -82,8 +82,75 @@ protected function serve(): int
82
82
$ error = 0 ;
83
83
84
84
$ run = $ input ->getOption ('run ' );
85
+ $ showDetails = $ input ->getOption ('details ' );
86
+ $ showJobs = $ input ->getOption ('jobs ' );
87
+
88
+ // Handle running jobs first if -r flag is present
89
+ if ($ run !== false ) {
90
+ if ($ run === null || $ run === '' ) {
91
+ // Run all jobs when -r is provided without a specific job ID
92
+ $ io ->title ('Force Run All Jobs ' );
93
+
94
+ $ jobs = $ scheduler ->getAllJobs ();
95
+ $ hasOutput = false ;
96
+
97
+ foreach ($ jobs as $ job ) {
98
+ if ($ job ->getEnabled ()) {
99
+ $ io ->section ('Running: ' . $ job ->getId ());
100
+ $ job ->inForeground ()->run ();
101
+
102
+ if ($ job ->isSuccessful ()) {
103
+ $ io ->success ('Job ' . $ job ->getId () . ' ran successfully ' );
104
+ } else {
105
+ $ error = 1 ;
106
+ $ io ->error ('Job ' . $ job ->getId () . ' failed to run ' );
107
+ }
108
+
109
+ $ output = $ job ->getOutput ();
110
+ if ($ output ) {
111
+ $ io ->write ($ output );
112
+ $ hasOutput = true ;
113
+ }
114
+ }
115
+ }
116
+
117
+ if (!$ hasOutput ) {
118
+ $ io ->note ('All enabled jobs completed ' );
119
+ }
120
+ } else {
121
+ // Run specific job
122
+ $ io ->title ('Force Run Job: ' . $ run );
123
+
124
+ $ job = $ scheduler ->getJob ($ run );
125
+
126
+ if ($ job ) {
127
+ $ job ->inForeground ()->run ();
128
+
129
+ if ($ job ->isSuccessful ()) {
130
+ $ io ->success ('Job ran successfully... ' );
131
+ } else {
132
+ $ error = 1 ;
133
+ $ io ->error ('Job failed to run successfully... ' );
134
+ }
135
+
136
+ $ output = $ job ->getOutput ();
137
+
138
+ if ($ output ) {
139
+ $ io ->write ($ output );
140
+ }
141
+ } else {
142
+ $ error = 1 ;
143
+ $ io ->error ('Could not find a job with id: ' . $ run );
144
+ }
145
+ }
146
+
147
+ // Add separator if we're going to show details after
148
+ if ($ showDetails ) {
149
+ $ io ->newLine ();
150
+ }
151
+ }
85
152
86
- if ($ input -> getOption ( ' jobs ' ) ) {
153
+ if ($ showJobs ) {
87
154
// Show jobs list
88
155
89
156
$ jobs = $ scheduler ->getAllJobs ();
@@ -124,7 +191,9 @@ protected function serve(): int
124
191
$ io ->newLine ();
125
192
$ io ->note ('For error details run "bin/grav scheduler -d" ' );
126
193
$ io ->newLine ();
127
- } elseif ($ input ->getOption ('details ' )) {
194
+ }
195
+
196
+ if ($ showDetails ) {
128
197
$ jobs = $ scheduler ->getAllJobs ();
129
198
$ job_states = (array )$ scheduler ->getJobStates ()->content ();
130
199
@@ -162,31 +231,9 @@ protected function serve(): int
162
231
163
232
$ table ->setRows ($ rows );
164
233
$ table ->render ();
165
- } elseif ($ run !== false && $ run !== null ) {
166
- $ io ->title ('Force Run Job: ' . $ run );
167
-
168
- $ job = $ scheduler ->getJob ($ run );
169
-
170
- if ($ job ) {
171
- $ job ->inForeground ()->run ();
172
-
173
- if ($ job ->isSuccessful ()) {
174
- $ io ->success ('Job ran successfully... ' );
175
- } else {
176
- $ error = 1 ;
177
- $ io ->error ('Job failed to run successfully... ' );
178
- }
179
-
180
- $ output = $ job ->getOutput ();
181
-
182
- if ($ output ) {
183
- $ io ->write ($ output );
184
- }
185
- } else {
186
- $ error = 1 ;
187
- $ io ->error ('Could not find a job with id: ' . $ run );
188
- }
189
- } elseif ($ input ->getOption ('install ' )) {
234
+ }
235
+
236
+ if ($ input ->getOption ('install ' )) {
190
237
$ io ->title ('Install Scheduler ' );
191
238
192
239
$ verb = 'install ' ;
@@ -207,10 +254,9 @@ protected function serve(): int
207
254
$ io ->note ("To $ verb, create a scheduled task in Windows. " );
208
255
$ io ->text ('Learn more at https://learn.getgrav.org/advanced/scheduler ' );
209
256
}
210
- } else {
211
- // Run scheduler
212
- $ force = $ run === null ;
213
- $ scheduler ->run (null , $ force );
257
+ } elseif (!$ showJobs && !$ showDetails && $ run === false ) {
258
+ // Run scheduler only if no other options were provided
259
+ $ scheduler ->run (null , true );
214
260
215
261
if ($ input ->getOption ('verbose ' )) {
216
262
$ io ->title ('Running Scheduled Jobs ' );
0 commit comments