Skip to content
This repository was archived by the owner on Jul 2, 2024. It is now read-only.

Commit 4419e97

Browse files
authored
Merge pull request #4 from joelharkes/fix_cleanup_memory_issue
Fix cleanup memory issue
2 parents 44e3056 + 9fe7b73 commit 4419e97

File tree

2 files changed

+42
-23
lines changed

2 files changed

+42
-23
lines changed

src/Console/CleanEmails.php

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -24,19 +24,23 @@ public function handle()
2424
return 1;
2525
}
2626

27-
$cutOffDate = Carbon::now()->subDay($maxAgeInDays)->format('Y-m-d H:i:s');
27+
$cutOffDate = Carbon::now()->subDays($maxAgeInDays)->format('Y-m-d H:i:s');
2828

2929
/** @var InboundEmail $modelClass */
3030
$modelClass = config('mailbox.model');
3131

32-
$models = $modelClass::where('created_at', '<', $cutOffDate)->get();
33-
34-
$models->each->delete();
35-
36-
$amountDeleted = $models->count();
37-
32+
$amountDeleted = 0;
33+
/** @var \Illuminate\Database\Eloquent\Builder $query */
34+
$query = $modelClass::where('created_at', '<', $cutOffDate);
35+
$query->eachById(function ($model) use (&$amountDeleted) {
36+
$amountDeleted++;
37+
/** @var InboundEmail $model */
38+
$model->delete();
39+
}, 100);
3840
$this->info("Deleted {$amountDeleted} record(s) from the Mailbox logs.");
3941

4042
$this->comment('All done!');
43+
44+
return 0;
4145
}
4246
}

tests/Console/CleanEmailsTest.php

Lines changed: 31 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22

33
namespace BeyondCode\Mailbox\Tests\Console;
44

5-
use Artisan;
65
use BeyondCode\Mailbox\InboundEmail;
76
use BeyondCode\Mailbox\Tests\TestCase;
87
use Carbon\Carbon;
@@ -23,16 +22,11 @@ public function setUp(): void
2322
/** @test */
2423
public function it_can_clean_the_statistics()
2524
{
26-
Collection::times(60)->each(function (int $index) {
27-
InboundEmail::forceCreate([
28-
'message' => Str::random(),
29-
'created_at' => Carbon::now()->subDays($index)->startOfDay(),
30-
]);
31-
});
25+
$this->makeMailForDays();
3226

3327
$this->assertCount(60, InboundEmail::all());
3428

35-
Artisan::call('mailbox:clean');
29+
$this->artisan('mailbox:clean');
3630

3731
$this->assertCount(31, InboundEmail::all());
3832

@@ -41,24 +35,45 @@ public function it_can_clean_the_statistics()
4135
$this->assertCount(0, InboundEmail::where('created_at', '<', $cutOffDate)->get());
4236
}
4337

38+
/** @test */
39+
public function it_respects_store_incoming_emails_for_days_config()
40+
{
41+
$this->app['config']->set('mailbox.store_incoming_emails_for_days', 1);
42+
$this->makeMailForDays(3);
43+
44+
$this->artisan('mailbox:clean');
45+
46+
$this->assertCount(1, InboundEmail::all());
47+
48+
}
49+
50+
4451
/** @test */
4552
public function it_errors_if_max_age_inf()
4653
{
4754
$this->app['config']->set('mailbox.store_incoming_emails_for_days', INF);
4855

49-
Collection::times(60)->each(function (int $index) {
50-
InboundEmail::forceCreate([
51-
'message' => Str::random(),
52-
'created_at' => Carbon::now()->subDays($index)->startOfDay(),
53-
]);
54-
});
56+
$this->makeMailForDays(3);
5557

56-
$this->assertCount(60, InboundEmail::all());
58+
$this->assertCount(3, InboundEmail::all());
5759

5860
$this->artisan('mailbox:clean')
5961
->expectsOutput('mailbox:clean is disabled because store_incoming_emails_for_days is set to INF.')
6062
->assertExitCode(1);
6163

62-
$this->assertCount(60, InboundEmail::all());
64+
$this->assertCount(3, InboundEmail::all());
65+
}
66+
67+
/**
68+
* @return void
69+
*/
70+
private function makeMailForDays(int $days = 60): void
71+
{
72+
Collection::times($days)->each(function (int $index) {
73+
InboundEmail::forceCreate([
74+
'message' => Str::random(),
75+
'created_at' => Carbon::now()->subDays($index)->startOfDay(),
76+
]);
77+
});
6378
}
6479
}

0 commit comments

Comments
 (0)