Skip to content

Commit 7d159df

Browse files
authored
TaskResults: Improve cleanup & fetching (#320)
* Task\autoCleanup: Delete in chunks * TaskResults: Add index to created_at
1 parent 9e93d1c commit 7d159df

File tree

2 files changed

+49
-8
lines changed

2 files changed

+49
-8
lines changed
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
<?php
2+
3+
use Illuminate\Database\Schema\Blueprint;
4+
use Illuminate\Support\Facades\Schema;
5+
use Studio\Totem\Database\TotemMigration;
6+
7+
class AlterTaskResultsTableAddIndexOnCreatedAt extends TotemMigration
8+
{
9+
/**
10+
* Run the migrations.
11+
*
12+
* @return void
13+
*/
14+
public function up()
15+
{
16+
Schema::connection(TOTEM_DATABASE_CONNECTION)
17+
->table(TOTEM_TABLE_PREFIX.'task_results', function (Blueprint $table) {
18+
$table->index('created_at');
19+
});
20+
}
21+
22+
/**
23+
* Reverse the migrations.
24+
*
25+
* @return void
26+
*/
27+
public function down()
28+
{
29+
Schema::connection(TOTEM_DATABASE_CONNECTION)
30+
->table(TOTEM_TABLE_PREFIX.'task_results', function (Blueprint $table) {
31+
$table->dropIndex('created_at');
32+
});
33+
}
34+
}

src/Task.php

Lines changed: 15 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -74,8 +74,7 @@ public function getUpcomingAttribute()
7474
/**
7575
* Convert a string of command arguments and options to an array.
7676
*
77-
* @param bool $console if true will convert arguments to non associative array
78-
*
77+
* @param bool $console if true will convert arguments to non associative array
7978
* @return array
8079
*/
8180
public function compileParameters($console = false)
@@ -198,13 +197,21 @@ public function autoCleanup()
198197
->limit($this->auto_cleanup_num)
199198
->get()
200199
->min('id');
201-
self::results()
202-
->where('id', '<', $oldest_id)
203-
->delete();
200+
do {
201+
$rowsDeleted = self::results()
202+
->where('id', '<', $oldest_id)
203+
->limit(500)
204+
->getQuery()
205+
->delete();
206+
} while ($rowsDeleted > 0);
204207
} else {
205-
self::results()
206-
->where('ran_at', '<', Carbon::now()->subDays($this->auto_cleanup_num - 1))
207-
->delete();
208+
do {
209+
$rowsDeleted = self::results()
210+
->where('ran_at', '<', Carbon::now()->subDays($this->auto_cleanup_num - 1))
211+
->limit(500)
212+
->getQuery()
213+
->delete();
214+
} while ($rowsDeleted > 0);
208215
}
209216
}
210217
}

0 commit comments

Comments
 (0)