File tree Expand file tree Collapse file tree 2 files changed +39
-3
lines changed Expand file tree Collapse file tree 2 files changed +39
-3
lines changed Original file line number Diff line number Diff line change @@ -255,6 +255,29 @@ public function prune(DateTimeInterface $before)
255
255
return $ totalDeleted ;
256
256
}
257
257
258
+ /**
259
+ * Prune all of the unfinished entries older than the given date.
260
+ *
261
+ * @param \DateTimeInterface $before
262
+ * @return int
263
+ */
264
+ public function pruneUnfinished (DateTimeInterface $ before )
265
+ {
266
+ $ query = $ this ->connection ->table ($ this ->table )
267
+ ->whereNull ('finished_at ' )
268
+ ->where ('created_at ' , '< ' , $ before ->getTimestamp ());
269
+
270
+ $ totalDeleted = 0 ;
271
+
272
+ do {
273
+ $ deleted = $ query ->take (1000 )->delete ();
274
+
275
+ $ totalDeleted += $ deleted ;
276
+ } while ($ deleted !== 0 );
277
+
278
+ return $ totalDeleted ;
279
+ }
280
+
258
281
/**
259
282
* Execute the given Closure within a storage specific transaction.
260
283
*
Original file line number Diff line number Diff line change 4
4
5
5
use Carbon \Carbon ;
6
6
use Illuminate \Bus \BatchRepository ;
7
+ use Illuminate \Bus \DatabaseBatchRepository ;
7
8
use Illuminate \Bus \PrunableBatchRepository ;
8
9
use Illuminate \Console \Command ;
9
10
@@ -14,7 +15,9 @@ class PruneBatchesCommand extends Command
14
15
*
15
16
* @var string
16
17
*/
17
- protected $ signature = 'queue:prune-batches {--hours=24 : The number of hours to retain batch data} ' ;
18
+ protected $ signature = 'queue:prune-batches
19
+ {--hours=24 : The number of hours to retain batch data}
20
+ {--unfinished= : The number of hours to retain unfinished batch data } ' ;
18
21
19
22
/**
20
23
* The console command description.
@@ -30,14 +33,24 @@ class PruneBatchesCommand extends Command
30
33
*/
31
34
public function handle ()
32
35
{
33
- $ count = 0 ;
34
-
35
36
$ repository = $ this ->laravel [BatchRepository::class];
36
37
38
+ $ count = 0 ;
39
+
37
40
if ($ repository instanceof PrunableBatchRepository) {
38
41
$ count = $ repository ->prune (Carbon::now ()->subHours ($ this ->option ('hours ' )));
39
42
}
40
43
41
44
$ this ->info ("{$ count } entries deleted! " );
45
+
46
+ if ($ unfinished = $ this ->option ('unfinished ' )) {
47
+ $ count = 0 ;
48
+
49
+ if ($ repository instanceof DatabaseBatchRepository) {
50
+ $ count = $ repository ->pruneUnfinished (Carbon::now ()->subHours ($ this ->option ('unfinished ' )));
51
+ }
52
+
53
+ $ this ->info ("{$ count } unfinished entries deleted! " );
54
+ }
42
55
}
43
56
}
You can’t perform that action at this time.
0 commit comments