Skip to content

Commit 9bd1509

Browse files
committed
Clean up finished jobs after batch processing
1 parent 2bcf209 commit 9bd1509

File tree

4 files changed

+56
-0
lines changed

4 files changed

+56
-0
lines changed

modules/apigee_edge_teams/src/Controller/TeamMemberSyncController.php

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -148,6 +148,7 @@ public static function getBatch(): array {
148148
'operations' => [
149149
[[static::class, 'batchGenerateJobs'], [$tag]],
150150
[[static::class, 'batchExecuteJobs'], [$tag]],
151+
[[static::class, 'batchCleanupJobs'], [$tag]],
151152
],
152153
'finished' => [static::class, 'batchFinished'],
153154
];
@@ -199,6 +200,23 @@ public static function batchExecuteJobs(string $tag, array &$context) {
199200
$context['finished'] = $executor->countJobs($tag, [Job::FAILED, Job::FINISHED]) / $executor->countJobs($tag);
200201
}
201202

203+
/**
204+
* The third batch operation.
205+
*
206+
* @param string $tag
207+
* Job tag.
208+
* @param array $context
209+
* Batch context.
210+
*/
211+
public static function batchCleanupJobs(string $tag, array &$context) {
212+
$executor = apigee_edge_get_executor();
213+
$executor->cleanup($tag);
214+
// This is needed to prevent the Drush CLI throwing warnings.
215+
$context['message'] = '';
216+
// This is needed to tell the Drush CLI we're finished.
217+
$context['finished'] = 1.0;
218+
}
219+
202220
/**
203221
* Batch finish callback.
204222
*/

src/Controller/DeveloperSyncController.php

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -148,6 +148,7 @@ public static function getBatch(): array {
148148
'operations' => [
149149
[[static::class, 'batchGenerateJobs'], [$tag]],
150150
[[static::class, 'batchExecuteJobs'], [$tag]],
151+
[[static::class, 'batchCleanupJobs'], [$tag]],
151152
],
152153
'finished' => [static::class, 'batchFinished'],
153154
];
@@ -199,6 +200,23 @@ public static function batchExecuteJobs(string $tag, array &$context) {
199200
$context['finished'] = $executor->countJobs($tag, [Job::FAILED, Job::FINISHED]) / $executor->countJobs($tag);
200201
}
201202

203+
/**
204+
* The third batch operation.
205+
*
206+
* @param string $tag
207+
* Job tag.
208+
* @param array $context
209+
* Batch context.
210+
*/
211+
public static function batchCleanupJobs(string $tag, array &$context) {
212+
$executor = apigee_edge_get_executor();
213+
$executor->cleanup($tag);
214+
// This is needed to prevent the Drush CLI throwing warnings.
215+
$context['message'] = '';
216+
// This is needed to tell the Drush CLI we're finished.
217+
$context['finished'] = 1.0;
218+
}
219+
202220
/**
203221
* Batch finish callback.
204222
*/

src/JobExecutor.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -192,4 +192,14 @@ public function countJobs(?string $tag = NULL, ?array $statuses = NULL): int {
192192
->fetchField();
193193
}
194194

195+
/**
196+
* {@inheritdoc}
197+
*/
198+
public function cleanup(string $tag): void {
199+
$query = $this->connection->delete('apigee_edge_job')
200+
->condition('status', Job::FINISHED)
201+
->condition('tag', $tag);
202+
$query->execute();
203+
}
204+
195205
}

src/JobExecutorInterface.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -99,4 +99,14 @@ public function cast(Job $job);
9999
*/
100100
public function countJobs(?string $tag = NULL, ?array $statuses = NULL): int;
101101

102+
/**
103+
* Cleans up finished jobs in the queue.
104+
*
105+
* @param string $tag
106+
* Tag to filter with.
107+
*
108+
* @todo Handle race conditions.
109+
*/
110+
public function cleanup(string $tag): void;
111+
102112
}

0 commit comments

Comments
 (0)