From af4e44b9a02963981ea1ed535da45544d3b74748 Mon Sep 17 00:00:00 2001 From: Austin Kregel <5355937+austinkregel@users.noreply.github.com> Date: Sun, 4 Feb 2024 03:10:55 +0000 Subject: [PATCH] add default catch to the fetch resources from credentials job --- app/Jobs/FetchResourcesFromCredentials.php | 3 + app/Jobs/SyncMailboxIfCredentialsAreSet.php | 140 ++++++++++---------- 2 files changed, 71 insertions(+), 72 deletions(-) diff --git a/app/Jobs/FetchResourcesFromCredentials.php b/app/Jobs/FetchResourcesFromCredentials.php index 50122f0a..c724ef7f 100644 --- a/app/Jobs/FetchResourcesFromCredentials.php +++ b/app/Jobs/FetchResourcesFromCredentials.php @@ -14,6 +14,7 @@ use Illuminate\Foundation\Bus\Dispatchable; use Illuminate\Queue\InteractsWithQueue; use Illuminate\Queue\SerializesModels; +use Illuminate\Support\Facades\Log; class FetchResourcesFromCredentials implements ShouldQueue { @@ -49,6 +50,8 @@ public function handle(Dispatcher $dispatcher) Credential::TYPE_SERVER => new FetchServersForCredential($credential), Credential::TYPE_DEVELOPMENT, 'forge' => new LaravelForgeServersSyncJob($credential), Credential::TYPE_FINANCE => new SyncPlaidTransactionsJob($credential, now()->subWeek(), now(), false), + Crtdential::TYPE_EMAIL => new + default => Log::error(sprintf('Found unsupported credential type for FetchResourcesFromCredentialsJob: %s', $credential->type), []), }); } } diff --git a/app/Jobs/SyncMailboxIfCredentialsAreSet.php b/app/Jobs/SyncMailboxIfCredentialsAreSet.php index 3025ac43..972efff0 100644 --- a/app/Jobs/SyncMailboxIfCredentialsAreSet.php +++ b/app/Jobs/SyncMailboxIfCredentialsAreSet.php @@ -24,6 +24,7 @@ class SyncMailboxIfCredentialsAreSet implements ShouldQueue use Dispatchable, InteractsWithQueue, Queueable, SerializesModels; public function __construct( + protected Credential $credential, protected ?Person $me = null, protected ?Carbon $since = null, ) { @@ -31,85 +32,80 @@ public function __construct( $this->since = now()->subDay(); } - public function handle(CredentialRepositoryContract $credentialRepository, ImapFactoryService $imapFactory): void + public function handle(ImapFactoryService $imapFactory): void { $limit = 10; $page = 1; - do { - $credentials = $credentialRepository->findAllOfType(Credential::TYPE_EMAIL, $limit, $page++); - - foreach ($credentials->items() as $credential) { - $imapService = $imapFactory->make($credential); - - info('Imap service seems to have credentials, trying to access inbox'); - $start = now(); - $messages = $imapService->findAllFromDate('INBOX', $this->since); - $end = now(); - - info('Found '.count($messages).' messages in '.$start->diffInSeconds($end).' seconds'); - - foreach ($messages as $i => $message) { - $trackedMessage = Message::query()->firstWhere([ - 'type' => 'email', - 'event_id' => $message['id'], - ]); - - if (empty($trackedMessage)) { - $body = $imapService->findMessage((string) $message['id']); - $trackedMessage = Message::create([ - 'from_person' => $this->getPersonFromEmail($message), - 'from_email' => (empty($message['from']['email']) ? null : $message['from']['email']) ?? $message['addressed-from']['email'] ?? null, - 'to_email' => (empty($message['to']['email']) ? null : $message['to']['email']) ?? $message['addressed-to']['email'] ?? null, - 'type' => 'email', - 'event_id' => $message['id'], - 'originated_at' => $message['date'], - 'subject' => $body['subject'], - 'is_decrypted' => true, - 'message' => $body['body'], - 'html_message' => $body['view'], - 'seen' => $body['seen'], - 'spam' => $body['spam'], - 'answered' => $body['answered'], - ]); - } else { - $body = $imapService->findMessage((string) $message['id']); - collect([ - 'originated_at' => $message['date'], - 'is_decrypted' => true, - 'message' => $body['body'], - 'html_message' => $body['view'], - 'from_email' => (empty($message['from']['email']) ? null : $message['from']['email']) ?? $message['addressed-from']['email'] ?? null, - 'to_email' => (empty($message['to']['email']) ? null : $message['to']['email']) ?? $message['addressed-to']['email'] ?? null, - 'subject' => $body['subject'], - 'seen' => $body['seen'], - 'spam' => $body['spam'], - 'answered' => $body['answered'], - ])->map(function ($value, $key) use ($trackedMessage) { - if ($value !== $trackedMessage->$key) { - $trackedMessage->$key = $value; - } - }); - - if ($trackedMessage->isDirty([ - 'originated_at', - 'is_decrypted', - 'message', - 'html_message', - 'seen', - 'spam', - 'answered', - ])) { - $this->getPersonToEmail($message); - $this->getPersonFromEmail($message); - $trackedMessage->save(); - } - } - info('Processed '.$i.'/'.count($messages)); + $imapService = $imapFactory->make($this->credential); + + info('Imap service seems to have credentials, trying to access inbox'); + $start = now(); + $messages = $imapService->findAllFromDate('INBOX', $this->since); + $end = now(); + + info('Found '.count($messages).' messages in '.$start->diffInSeconds($end).' seconds'); + + foreach ($messages as $i => $message) { + $trackedMessage = Message::query()->firstWhere([ + 'type' => 'email', + 'event_id' => $message['id'], + ]); + + if (empty($trackedMessage)) { + $body = $imapService->findMessage((string) $message['id']); + $trackedMessage = Message::create([ + 'from_person' => $this->getPersonFromEmail($message), + 'from_email' => (empty($message['from']['email']) ? null : $message['from']['email']) ?? $message['addressed-from']['email'] ?? null, + 'to_email' => (empty($message['to']['email']) ? null : $message['to']['email']) ?? $message['addressed-to']['email'] ?? null, + 'type' => 'email', + 'event_id' => $message['id'], + 'originated_at' => $message['date'], + 'subject' => $body['subject'], + 'is_decrypted' => true, + 'message' => $body['body'], + 'html_message' => $body['view'], + 'seen' => $body['seen'], + 'spam' => $body['spam'], + 'answered' => $body['answered'], + ]); + } else { + $body = $imapService->findMessage((string) $message['id']); + collect([ + 'originated_at' => $message['date'], + 'is_decrypted' => true, + 'message' => $body['body'], + 'html_message' => $body['view'], + 'from_email' => (empty($message['from']['email']) ? null : $message['from']['email']) ?? $message['addressed-from']['email'] ?? null, + 'to_email' => (empty($message['to']['email']) ? null : $message['to']['email']) ?? $message['addressed-to']['email'] ?? null, + 'subject' => $body['subject'], + 'seen' => $body['seen'], + 'spam' => $body['spam'], + 'answered' => $body['answered'], + ])->map(function ($value, $key) use ($trackedMessage) { + if ($value !== $trackedMessage->$key) { + $trackedMessage->$key = $value; + } + }); + + if ($trackedMessage->isDirty([ + 'originated_at', + 'is_decrypted', + 'message', + 'html_message', + 'seen', + 'spam', + 'answered', + ])) { + $this->getPersonToEmail($message); + $this->getPersonFromEmail($message); + $trackedMessage->save(); } } - } while ($credentials->hasMorePages()); + + info('Processed '.$i.'/'.count($messages)); + } }