Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[3.x] Prevent 'memory exhausted' when deleting monitored tag #690

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 8 additions & 1 deletion src/Jobs/StopMonitoringTag.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,14 @@ public function handle(JobRepository $jobs, TagRepository $tags)
{
$tags->stopMonitoring($this->tag);

$jobs->deleteMonitored($tags->jobs($this->tag));
$tagJobs = $tags->paginate($this->tag);

while (count($tagJobs) !== 0) {
$jobs->deleteMonitored($tagJobs);

$offset = array_keys($tagJobs)[count($tagJobs) - 1] + 1;
$tagJobs = $tags->paginate($this->tag, $offset);
}

$tags->forget($this->tag);
}
Expand Down
14 changes: 14 additions & 0 deletions tests/Feature/MonitoringTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -64,4 +64,18 @@ public function test_completed_jobs_are_removed_from_database_when_their_tag_is_
dispatch(new StopMonitoringTag('first'));
$this->assertEquals(0, $this->monitoredJobs('first'));
}

public function test_all_completed_jobs_are_removed_from_database_when_their_tag_is_no_longer_monitored()
{
dispatch(new MonitorTag('first'));

for ($i = 0; $i < 80; $i++) {
Queue::push(new Jobs\BasicJob);
}

$this->work();

dispatch(new StopMonitoringTag('first'));
$this->assertEquals(0, $this->monitoredJobs('first'));
}
}