diff --git a/database/migrations/2022_03_14_120000_alter_task_results_table_add_index_on_created_at.php b/database/migrations/2022_03_14_120000_alter_task_results_table_add_index_on_created_at.php new file mode 100644 index 00000000..b7b96278 --- /dev/null +++ b/database/migrations/2022_03_14_120000_alter_task_results_table_add_index_on_created_at.php @@ -0,0 +1,34 @@ +table(TOTEM_TABLE_PREFIX.'task_results', function (Blueprint $table) { + $table->index('created_at'); + }); + } + + /** + * Reverse the migrations. + * + * @return void + */ + public function down() + { + Schema::connection(TOTEM_DATABASE_CONNECTION) + ->table(TOTEM_TABLE_PREFIX.'task_results', function (Blueprint $table) { + $table->dropIndex('created_at'); + }); + } +} diff --git a/src/Task.php b/src/Task.php index 19b9bb93..4515d2e2 100644 --- a/src/Task.php +++ b/src/Task.php @@ -74,8 +74,7 @@ public function getUpcomingAttribute() /** * Convert a string of command arguments and options to an array. * - * @param bool $console if true will convert arguments to non associative array - * + * @param bool $console if true will convert arguments to non associative array * @return array */ public function compileParameters($console = false) @@ -198,13 +197,21 @@ public function autoCleanup() ->limit($this->auto_cleanup_num) ->get() ->min('id'); - self::results() - ->where('id', '<', $oldest_id) - ->delete(); + do { + $rowsDeleted = self::results() + ->where('id', '<', $oldest_id) + ->limit(500) + ->getQuery() + ->delete(); + } while ($rowsDeleted > 0); } else { - self::results() - ->where('ran_at', '<', Carbon::now()->subDays($this->auto_cleanup_num - 1)) - ->delete(); + do { + $rowsDeleted = self::results() + ->where('ran_at', '<', Carbon::now()->subDays($this->auto_cleanup_num - 1)) + ->limit(500) + ->getQuery() + ->delete(); + } while ($rowsDeleted > 0); } } }