Skip to content

Commit 094916d

Browse files
authored
feat: add logs for addressbook subscriptions (#6841)
1 parent 9ab75dc commit 094916d

29 files changed

+524
-47
lines changed

app/Console/Kernel.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
use App\Domains\Contact\Dav\Jobs\CleanSyncToken;
77
use App\Domains\Contact\DavClient\Jobs\UpdateAddressBooks;
88
use App\Domains\Contact\ManageReminders\Jobs\ProcessScheduledContactReminders;
9+
use App\Logging\CleanLogs;
910
use Illuminate\Console\Scheduling\Schedule;
1011
use Illuminate\Foundation\Console\Kernel as ConsoleKernel;
1112
use Illuminate\Support\Facades\App;
@@ -44,6 +45,7 @@ protected function schedule(Schedule $schedule)
4445
$this->scheduleJob($schedule, UpdateAddressBooks::class, 'hourly');
4546
$this->scheduleJob($schedule, ProcessScheduledContactReminders::class, 'minutes', 1);
4647
$this->scheduleJob($schedule, CleanSyncToken::class, 'daily');
48+
$this->scheduleJob($schedule, CleanLogs::class, 'daily');
4749
}
4850

4951
/**

app/Domains/Contact/Dav/Jobs/UpdateVCard.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ public function execute(array $data): void
6565
$newtag = $this->updateCard($this->data['uri'], $this->data['card']);
6666

6767
if ($newtag !== null && ($etag = Arr::get($this->data, 'etag')) !== null && $newtag !== $etag) {
68-
Log::warning(__CLASS__.' '.__FUNCTION__." wrong etag when updating contact. Expected [$etag], got [$newtag]", [
68+
Log::channel('database')->warning(__CLASS__.' '.__FUNCTION__." wrong etag when updating contact. Expected [$etag], got [$newtag]", [
6969
'contacturl' => $this->data['uri'],
7070
'carddata' => $this->data['card'],
7171
]);
@@ -99,7 +99,7 @@ private function updateCard(string $uri, mixed $card): ?string
9999
]);
100100
}
101101
} catch (\Exception $e) {
102-
Log::error(__CLASS__.' '.__FUNCTION__.': '.$e->getMessage(), [
102+
Log::channel('database')->error(__CLASS__.' '.__FUNCTION__.': '.$e->getMessage(), [
103103
'uri' => $uri,
104104
'carddata' => $card,
105105
$e,

app/Domains/Contact/Dav/Web/Backend/CardDAV/CardDAVBackend.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -204,7 +204,7 @@ public function prepareCard(VCardResource $resource): array
204204
'lastmodified' => $resource->updated_at->timestamp,
205205
];
206206
} catch (\Exception $e) {
207-
Log::error(__CLASS__.' '.__FUNCTION__.': '.$e->getMessage(), [
207+
Log::channel('database')->error(__CLASS__.' '.__FUNCTION__.': '.$e->getMessage(), [
208208
'carddata' => $carddata,
209209
'id' => $resource->id,
210210
$e,
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
<?php
2+
3+
namespace App\Domains\Contact\DavClient\Jobs;
4+
5+
use App\Domains\Contact\Dav\Web\Backend\CardDAV\CardDAVBackend;
6+
use App\Models\AddressBookSubscription;
7+
use Illuminate\Bus\Batchable;
8+
use Illuminate\Bus\Queueable;
9+
use Illuminate\Contracts\Queue\ShouldQueue;
10+
use Illuminate\Queue\InteractsWithQueue;
11+
use Illuminate\Queue\SerializesModels;
12+
use Illuminate\Support\Facades\Log;
13+
14+
class DeleteLocalVCard implements ShouldQueue
15+
{
16+
use Batchable, InteractsWithQueue, Queueable, SerializesModels;
17+
18+
/**
19+
* Create a new job instance.
20+
*/
21+
public function __construct(
22+
private AddressBookSubscription $subscription,
23+
private string $uri
24+
) {
25+
$this->subscription = $subscription->withoutRelations();
26+
}
27+
28+
/**
29+
* Send Delete contact.
30+
*/
31+
public function handle(): void
32+
{
33+
Log::shareContext([
34+
'addressbook_subscription_id' => $this->subscription->id,
35+
]);
36+
37+
try {
38+
$this->run();
39+
} finally {
40+
Log::flushSharedContext();
41+
}
42+
}
43+
44+
/**
45+
* Run the job.
46+
*/
47+
private function run(): void
48+
{
49+
Log::channel('database')->debug("Delete local card {$this->uri}");
50+
51+
$backend = app(CardDAVBackend::class)->withUser($this->subscription->user);
52+
$backend->deleteCard($this->subscription->vault_id, $this->uri);
53+
}
54+
}

app/Domains/Contact/DavClient/Jobs/DeleteMultipleVCard.php

Lines changed: 22 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
use Illuminate\Contracts\Queue\ShouldQueue;
99
use Illuminate\Queue\InteractsWithQueue;
1010
use Illuminate\Queue\SerializesModels;
11+
use Illuminate\Support\Facades\Log;
1112

1213
class DeleteMultipleVCard implements ShouldQueue
1314
{
@@ -32,17 +33,35 @@ public function handle(): void
3233
return; // @codeCoverageIgnore
3334
}
3435

36+
Log::shareContext([
37+
'addressbook_subscription_id' => $this->subscription->id,
38+
]);
39+
40+
try {
41+
$this->run();
42+
} finally {
43+
Log::flushSharedContext();
44+
}
45+
}
46+
47+
/**
48+
* Run the job.
49+
*/
50+
private function run(): void
51+
{
3552
$jobs = collect($this->hrefs)
36-
->map(fn (string $href): DeleteVCard => $this->deleteVCard($href));
53+
->map(fn (string $href): DeleteLocalVCard => $this->deleteVCard($href));
54+
55+
Log::channel('database')->info("Delete {$jobs->count()} card(s) from distant server...");
3756

3857
$this->batch()->add($jobs);
3958
}
4059

4160
/**
4261
* Delete the contact.
4362
*/
44-
private function deleteVCard(string $href): DeleteVCard
63+
private function deleteVCard(string $href): DeleteLocalVCard
4564
{
46-
return new DeleteVCard($this->subscription, $href);
65+
return new DeleteLocalVCard($this->subscription, $href);
4766
}
4867
}

app/Domains/Contact/DavClient/Jobs/DeleteVCard.php

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
use Illuminate\Contracts\Queue\ShouldQueue;
99
use Illuminate\Queue\InteractsWithQueue;
1010
use Illuminate\Queue\SerializesModels;
11+
use Illuminate\Support\Facades\Log;
1112

1213
class DeleteVCard implements ShouldQueue
1314
{
@@ -28,6 +29,24 @@ public function __construct(
2829
*/
2930
public function handle(): void
3031
{
32+
Log::shareContext([
33+
'addressbook_subscription_id' => $this->subscription->id,
34+
]);
35+
36+
try {
37+
$this->run();
38+
} finally {
39+
Log::flushSharedContext();
40+
}
41+
}
42+
43+
/**
44+
* Run the job.
45+
*/
46+
private function run(): void
47+
{
48+
Log::channel('database')->debug("Delete card {$this->uri}");
49+
3150
$this->subscription->getClient()
3251
->request('DELETE', $this->uri);
3352
}

app/Domains/Contact/DavClient/Jobs/GetMultipleVCard.php

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
use Illuminate\Queue\InteractsWithQueue;
1111
use Illuminate\Queue\SerializesModels;
1212
use Illuminate\Support\Arr;
13+
use Illuminate\Support\Facades\Log;
1314
use Sabre\CardDAV\Plugin as CardDav;
1415

1516
class GetMultipleVCard implements ShouldQueue
@@ -35,13 +36,31 @@ public function handle(): void
3536
return; // @codeCoverageIgnore
3637
}
3738

39+
Log::shareContext([
40+
'addressbook_subscription_id' => $this->subscription->id,
41+
]);
42+
43+
try {
44+
$this->run();
45+
} finally {
46+
Log::flushSharedContext();
47+
}
48+
}
49+
50+
/**
51+
* Run the job.
52+
*/
53+
private function run(): void
54+
{
3855
$data = $this->addressbookMultiget();
3956

4057
$jobs = collect($data)
4158
->filter(fn (array $contact): bool => is_array($contact) && $contact['status'] === '200')
4259
->map(fn (array $contact, string $href): ?UpdateVCard => $this->updateVCard($contact, $href))
4360
->filter();
4461

62+
Log::channel('database')->info("Get {$jobs->count()} card(s) from distant server...");
63+
4564
$this->batch()->add($jobs);
4665
}
4766

app/Domains/Contact/DavClient/Jobs/GetVCard.php

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
use Illuminate\Contracts\Queue\ShouldQueue;
1111
use Illuminate\Queue\InteractsWithQueue;
1212
use Illuminate\Queue\SerializesModels;
13+
use Illuminate\Support\Facades\Log;
1314

1415
class GetVCard implements ShouldQueue
1516
{
@@ -34,6 +35,24 @@ public function handle(): void
3435
return; // @codeCoverageIgnore
3536
}
3637

38+
Log::shareContext([
39+
'addressbook_subscription_id' => $this->subscription->id,
40+
]);
41+
42+
try {
43+
$this->run();
44+
} finally {
45+
Log::flushSharedContext();
46+
}
47+
}
48+
49+
/**
50+
* Run the job.
51+
*/
52+
private function run(): void
53+
{
54+
Log::channel('database')->debug("Get card {$this->contact->uri}");
55+
3756
$response = $this->subscription->getClient()
3857
->request('GET', $this->contact->uri);
3958

app/Domains/Contact/DavClient/Jobs/PushVCard.php

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,22 @@ public function __construct(
4545
* Push VCard data to the distance server.
4646
*/
4747
public function handle(): void
48+
{
49+
Log::shareContext([
50+
'addressbook_subscription_id' => $this->subscription->id,
51+
]);
52+
53+
try {
54+
$this->run();
55+
} finally {
56+
Log::flushSharedContext();
57+
}
58+
}
59+
60+
/**
61+
* Run the job.
62+
*/
63+
private function run(): void
4864
{
4965
$contact = Contact::where('vault_id', $this->subscription->vault_id)
5066
->findOrFail($this->contactId);
@@ -64,6 +80,8 @@ public function handle(): void
6480
private function pushDistant(int $depth = 1): string
6581
{
6682
try {
83+
Log::channel('database')->debug("Push card {$this->uri}");
84+
6785
$response = $this->subscription->getClient()
6886
->request('PUT', $this->uri, $this->card, $this->headers());
6987

@@ -75,7 +93,7 @@ private function pushDistant(int $depth = 1): string
7593

7694
return $this->pushDistant(--$depth);
7795
} else {
78-
Log::error(__CLASS__.' '.__FUNCTION__.': '.$e->getMessage(), [
96+
Log::channel('database')->error(__CLASS__.' '.__FUNCTION__.': '.$e->getMessage(), [
7997
'body' => $e->response->body(),
8098
$e,
8199
]);

app/Domains/Contact/DavClient/Jobs/SynchronizeAddressBooks.php

Lines changed: 23 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,13 @@ class SynchronizeAddressBooks implements ShouldQueue
1616
{
1717
use Dispatchable, InteractsWithQueue, Queueable, SerializesModels, Localizable;
1818

19+
/**
20+
* The number of times the job may be attempted.
21+
*
22+
* @var int
23+
*/
24+
public $tries = 1;
25+
1926
/**
2027
* Create a new job instance.
2128
*/
@@ -31,19 +38,29 @@ public function __construct(
3138
*/
3239
public function handle(): void
3340
{
34-
$this->withLocale($this->subscription->user->preferredLocale(), fn () => $this->synchronize());
41+
try {
42+
$logid = $this->subscription->current_logid ?? 0;
43+
$this->subscription->current_logid = $logid + 1;
44+
$this->subscription->save();
45+
46+
Log::shareContext([
47+
'addressbook_subscription_id' => $this->subscription->id,
48+
]);
49+
50+
$this->withLocale($this->subscription->user->preferredLocale(), fn () => $this->synchronize());
51+
} finally {
52+
Log::flushSharedContext();
53+
}
3554
}
3655

3756
/**
3857
* Run synchronization.
3958
*/
4059
private function synchronize(): void
4160
{
42-
try {
43-
Log::withContext([
44-
'addressbook_subscription_id' => $this->subscription->id,
45-
]);
61+
Log::channel('database')->info("Synchronize addressbook '{$this->subscription->vault->name}'");
4662

63+
try {
4764
$batchId = app(SynchronizeAddressBook::class)->execute([
4865
'account_id' => $this->subscription->user->account_id,
4966
'addressbook_subscription_id' => $this->subscription->id,
@@ -52,13 +69,11 @@ private function synchronize(): void
5269

5370
$this->subscription->last_batch = $batchId;
5471
} catch (\Exception $e) {
55-
Log::error(__CLASS__.' '.__FUNCTION__.':'.$e->getMessage(), [$e]);
72+
Log::stack([config('logging.default'), 'database'])->error(__CLASS__.' '.__FUNCTION__.':'.$e->getMessage(), [$e]);
5673
$this->fail($e);
5774
} finally {
5875
$this->subscription->last_synchronized_at = now();
5976
$this->subscription->save();
60-
61-
Log::withoutContext();
6277
}
6378
}
6479
}

0 commit comments

Comments
 (0)