Skip to content

Commit

Permalink
feat: add logs for addressbook subscriptions
Browse files Browse the repository at this point in the history
  • Loading branch information
asbiin committed Sep 1, 2023
1 parent 9ab75dc commit c0faf85
Show file tree
Hide file tree
Showing 21 changed files with 314 additions and 18 deletions.
2 changes: 2 additions & 0 deletions app/Console/Kernel.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
use App\Domains\Contact\Dav\Jobs\CleanSyncToken;
use App\Domains\Contact\DavClient\Jobs\UpdateAddressBooks;
use App\Domains\Contact\ManageReminders\Jobs\ProcessScheduledContactReminders;
use App\Logging\CleanLogs;
use Illuminate\Console\Scheduling\Schedule;
use Illuminate\Foundation\Console\Kernel as ConsoleKernel;
use Illuminate\Support\Facades\App;
Expand Down Expand Up @@ -44,6 +45,7 @@ protected function schedule(Schedule $schedule)
$this->scheduleJob($schedule, UpdateAddressBooks::class, 'hourly');
$this->scheduleJob($schedule, ProcessScheduledContactReminders::class, 'minutes', 1);
$this->scheduleJob($schedule, CleanSyncToken::class, 'daily');
$this->scheduleJob($schedule, CleanLogs::class, 'daily');
}

/**
Expand Down
4 changes: 2 additions & 2 deletions app/Domains/Contact/Dav/Jobs/UpdateVCard.php
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ public function execute(array $data): void
$newtag = $this->updateCard($this->data['uri'], $this->data['card']);

if ($newtag !== null && ($etag = Arr::get($this->data, 'etag')) !== null && $newtag !== $etag) {
Log::warning(__CLASS__.' '.__FUNCTION__." wrong etag when updating contact. Expected [$etag], got [$newtag]", [
Log::channel('database')->warning(__CLASS__.' '.__FUNCTION__." wrong etag when updating contact. Expected [$etag], got [$newtag]", [
'contacturl' => $this->data['uri'],
'carddata' => $this->data['card'],
]);
Expand Down Expand Up @@ -99,7 +99,7 @@ private function updateCard(string $uri, mixed $card): ?string
]);
}
} catch (\Exception $e) {
Log::error(__CLASS__.' '.__FUNCTION__.': '.$e->getMessage(), [
Log::channel('database')->error(__CLASS__.' '.__FUNCTION__.': '.$e->getMessage(), [
'uri' => $uri,
'carddata' => $card,
$e,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -204,7 +204,7 @@ public function prepareCard(VCardResource $resource): array
'lastmodified' => $resource->updated_at->timestamp,
];
} catch (\Exception $e) {
Log::error(__CLASS__.' '.__FUNCTION__.': '.$e->getMessage(), [
Log::channel('database')->error(__CLASS__.' '.__FUNCTION__.': '.$e->getMessage(), [
'carddata' => $carddata,
'id' => $resource->id,
$e,
Expand Down
17 changes: 17 additions & 0 deletions app/Domains/Contact/DavClient/Jobs/DeleteMultipleVCard.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Queue\SerializesModels;
use Illuminate\Support\Facades\Log;

class DeleteMultipleVCard implements ShouldQueue
{
Expand All @@ -32,6 +33,22 @@ public function handle(): void
return; // @codeCoverageIgnore
}

Log::shareContext([
'addressbook_subscription_id' => $this->subscription->id,
]);

try {
$this->run();
} finally {
Log::flushSharedContext();
}
}

/**
* Run the job.
*/
private function run(): void
{
$jobs = collect($this->hrefs)
->map(fn (string $href): DeleteVCard => $this->deleteVCard($href));

Expand Down
19 changes: 19 additions & 0 deletions app/Domains/Contact/DavClient/Jobs/DeleteVCard.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Queue\SerializesModels;
use Illuminate\Support\Facades\Log;

class DeleteVCard implements ShouldQueue
{
Expand All @@ -28,6 +29,24 @@ public function __construct(
*/
public function handle(): void
{
Log::shareContext([
'addressbook_subscription_id' => $this->subscription->id,
]);

try {
$this->run();
} finally {
Log::flushSharedContext();
}
}

/**
* Run the job.
*/
private function run(): void
{
Log::channel('database')->info("Delete card {$this->uri}");

$this->subscription->getClient()
->request('DELETE', $this->uri);
}
Expand Down
17 changes: 17 additions & 0 deletions app/Domains/Contact/DavClient/Jobs/GetMultipleVCard.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Queue\SerializesModels;
use Illuminate\Support\Arr;
use Illuminate\Support\Facades\Log;
use Sabre\CardDAV\Plugin as CardDav;

class GetMultipleVCard implements ShouldQueue
Expand All @@ -35,6 +36,22 @@ public function handle(): void
return; // @codeCoverageIgnore
}

Log::shareContext([
'addressbook_subscription_id' => $this->subscription->id,
]);

try {
$this->run();
} finally {
Log::flushSharedContext();
}
}

/**
* Run the job.
*/
private function run(): void
{
$data = $this->addressbookMultiget();

$jobs = collect($data)
Expand Down
19 changes: 19 additions & 0 deletions app/Domains/Contact/DavClient/Jobs/GetVCard.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Queue\SerializesModels;
use Illuminate\Support\Facades\Log;

class GetVCard implements ShouldQueue
{
Expand All @@ -34,6 +35,24 @@ public function handle(): void
return; // @codeCoverageIgnore
}

Log::shareContext([
'addressbook_subscription_id' => $this->subscription->id,
]);

try {
$this->run();
} finally {
Log::flushSharedContext();
}
}

/**
* Run the job.
*/
private function run(): void
{
Log::channel('database')->info("Get card {$this->contact->uri}");

$response = $this->subscription->getClient()
->request('GET', $this->contact->uri);

Expand Down
20 changes: 19 additions & 1 deletion app/Domains/Contact/DavClient/Jobs/PushVCard.php
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,22 @@ public function __construct(
* Push VCard data to the distance server.
*/
public function handle(): void
{
Log::shareContext([
'addressbook_subscription_id' => $this->subscription->id,
]);

try {
$this->run();
} finally {
Log::flushSharedContext();
}
}

/**
* Run the job.
*/
private function run(): void
{
$contact = Contact::where('vault_id', $this->subscription->vault_id)
->findOrFail($this->contactId);
Expand All @@ -64,6 +80,8 @@ public function handle(): void
private function pushDistant(int $depth = 1): string
{
try {
Log::channel('database')->info("Push card {$this->uri}");

$response = $this->subscription->getClient()
->request('PUT', $this->uri, $this->card, $this->headers());

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

return $this->pushDistant(--$depth);
} else {
Log::error(__CLASS__.' '.__FUNCTION__.': '.$e->getMessage(), [
Log::channel('database')->error(__CLASS__.' '.__FUNCTION__.': '.$e->getMessage(), [
'body' => $e->response->body(),
$e,
]);
Expand Down
31 changes: 24 additions & 7 deletions app/Domains/Contact/DavClient/Jobs/SynchronizeAddressBooks.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,13 @@ class SynchronizeAddressBooks implements ShouldQueue
{
use Dispatchable, InteractsWithQueue, Queueable, SerializesModels, Localizable;

/**
* The number of times the job may be attempted.
*
* @var int
*/
public $tries = 1;

/**
* Create a new job instance.
*/
Expand All @@ -31,19 +38,29 @@ public function __construct(
*/
public function handle(): void
{
$this->withLocale($this->subscription->user->preferredLocale(), fn () => $this->synchronize());
try {
$logid = $this->subscription->current_logid ?? 0;
$this->subscription->current_logid = $logid + 1;
$this->subscription->save();

Log::shareContext([
'addressbook_subscription_id' => $this->subscription->id,
]);

$this->withLocale($this->subscription->user->preferredLocale(), fn () => $this->synchronize());
} finally {
Log::flushSharedContext();
}
}

/**
* Run synchronization.
*/
private function synchronize(): void
{
try {
Log::withContext([
'addressbook_subscription_id' => $this->subscription->id,
]);
Log::channel('database')->info('Synchronize addressbook');

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

$this->subscription->last_batch = $batchId;
} catch (\Exception $e) {
Log::error(__CLASS__.' '.__FUNCTION__.':'.$e->getMessage(), [$e]);
Log::stack([config('logging.default'), 'database'])->error(__CLASS__.' '.__FUNCTION__.':'.$e->getMessage(), [$e]);
$this->fail($e);
} finally {
$this->subscription->last_synchronized_at = now();
$this->subscription->save();

Log::withoutContext();
Log::channel('database')->info('End of synchronization');
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ private function synchronize(bool $force): ?string
->withSubscription($this->subscription)
->execute($force);
} catch (ClientException $e) {
Log::error(__CLASS__.' '.__FUNCTION__.': '.$e->getMessage(), [
Log::channel('database')->error(__CLASS__.' '.__FUNCTION__.': '.$e->getMessage(), [
'body' => $e->hasResponse() ? $e->getResponse()->getBody() : null,
$e,
]);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ public function execute(): ?array
try {
return $this->getAddressBookData();
} catch (ClientException $e) {
Log::error(__CLASS__.' '.__FUNCTION__.': '.$e->getMessage(), [$e]);
Log::channel('database')->error(__CLASS__.' '.__FUNCTION__.': '.$e->getMessage(), [$e]);
throw $e;
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -230,7 +230,7 @@ private function callSyncCollection(): array
$this->subscription->save();
}
} catch (RequestException $e) {
Log::error(__CLASS__.' '.__FUNCTION__.':'.$e->getMessage(), [$e]);
Log::channel('database')->error(__CLASS__.' '.__FUNCTION__.':'.$e->getMessage(), [$e]);
$collection = [];
$this->subscription->distant_sync_token = null;
$this->subscription->save();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -473,7 +473,7 @@ public function request(string $method, string $url = '', mixed $body = null, ar

$url = Str::startsWith($url, 'http') ? $url : $this->path($url);

Log::debug(__CLASS__.' '.__FUNCTION__.'[request]: '.$method.' '.$url, [
Log::channel('database')->debug(__CLASS__.' '.__FUNCTION__.'[request]: '.$method.' '.$url, [
'body' => $body,
'headers' => $headers,
'options' => $options,
Expand All @@ -482,13 +482,13 @@ public function request(string $method, string $url = '', mixed $body = null, ar
$response = $request
->send($method, $url, $options)
->throw(function (Response $response) use ($method, $url) {
Log::debug(__CLASS__.' '.__FUNCTION__.'[error]: '.$method.' '.$url.' '.$response->status(), [
Log::channel('database')->debug(__CLASS__.' '.__FUNCTION__.'[error]: '.$method.' '.$url.' '.$response->status(), [
'body' => $response->body(),
'headers' => $response->headers(),
]);
});

Log::debug(__CLASS__.' '.__FUNCTION__.'[response]: '.$method.' '.$url.' '.$response->status(), [
Log::channel('database')->debug(__CLASS__.' '.__FUNCTION__.'[response]: '.$method.' '.$url.' '.$response->status(), [
'body' => $response->body(),
'headers' => $response->headers(),
]);
Expand Down Expand Up @@ -557,7 +557,7 @@ private static function addElementNS(\DOMDocument $dom, ?string $namespace, stri
/**
* Create a new Element and add it as root's child.
*/
private static function addElement(\DOMDocument $dom, \DOMNode $root, string $name, string $value = null): \DOMNode
private static function addElement(\DOMDocument $dom, \DOMNode $root, string $name, string $value = ''): \DOMNode
{
return $root->appendChild($dom->createElement($name, $value));
}
Expand Down
19 changes: 19 additions & 0 deletions app/Logging/CleanLogs.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<?php

namespace App\Logging;

use App\Interfaces\ServiceInterface;
use App\Models\Log;
use App\Services\QueuableService;
use Carbon\Carbon;

class CleanLogs extends QueuableService implements ServiceInterface
{
/**
* Clean logs list.
*/
public function execute(array $data): void
{
Log::where('created_at', '<', Carbon::now()->subDays(15))->delete();
}
}
Loading

0 comments on commit c0faf85

Please sign in to comment.