chatwoot/lib
Pranav Raj S fdbb3bf4b1
fix: Optimize email fetching logic to fix bandwidth exceeded issues (#8730)
The Inboxes::FetchImapEmailsJob is designed to fetch the entire email object and check if its message id is already in the database. However, this process is resource-intensive and time-consuming, as fetching the full email object takes a significant amount of time.

On average, fetching 100 emails can take approximately 3-4 minutes to complete depending on the IMAP server. Since we are not using server flags to identify which emails have already been fetched (to avoid compatibility issues with flags in different email services), we have to fetch all previously available emails. Currently we fetch all the messages that were created from yesterday. This becomes problematic with services like Gmail, which throttle requests based on bandwidth usage.

To address this issue, I have updated the logic as follows:

Fetch the sequence IDs of all the mails in the "Inbox" that were created from yesterday.
Use the FETCH command to fetch only the message-ids using BODY.PEEK[HEADER.FIELDS (MESSAGE-ID)] with the sequence IDs we got in the previous step. This is a faster operation with lower bandwidth usage, as it only returns the sequence ID and message ID.
Check if the message IDs are already present in the database for the selected inbox.
If not present, fetch the entire email object using the FETCH command and create the message.
If the message ID is already present, ignore it and move on to the next message-id.
I have also addressed the issue of duplicate emails appearing in conversations when two fetch email jobs occur simultaneously. I have added a lock for the channel to ensure that the job gracefully exits without waiting for the current job to complete.

Fixes #7247
Fixes #6082
Fixes #8314

Co-authored-by: Sojan <sojan@pepalo.com>
2024-01-18 11:45:16 +04:00
..
action_view/template/handlers feat: Customisable Email Templates (#1095) 2020-08-06 15:21:06 +05:30
assets Initial Commit 2019-08-14 15:18:44 +05:30
automation_rules Feat: detect language of the message content (#6660) 2023-04-04 08:57:27 +05:30
custom_exceptions chore: Enable the new Rubocop rules (#7122) 2023-05-19 14:37:10 +05:30
events feat: Add notification_deleted action cable event (#8431) 2023-12-01 23:06:22 +05:30
filters Feat: detect language of the message content (#6660) 2023-04-04 08:57:27 +05:30
integrations chore: Rescue Slack::Web::Api::Errors::NotInChannel error (#8670) 2024-01-09 14:30:17 -08:00
redis fix: Optimize email fetching logic to fix bandwidth exceeded issues (#8730) 2024-01-18 11:45:16 +04:00
seeders chore: Improve account seeding (#8357) 2023-11-15 13:29:00 +05:30
tasks feat: Report cwctl events to hub (#8009) 2023-10-10 09:16:03 +05:30
webhooks fix: Handle all types of errors in API channel webhooks (#8307) 2023-11-07 12:54:49 +05:30
base_markdown_renderer.rb feat: support image height in markdown rendering of messages (#8177) 2023-11-02 13:51:54 -07:00
chatwoot_app.rb chore: Introduce enterprise edition license (#3209) 2021-12-09 12:07:48 +05:30
chatwoot_captcha.rb feat: Add hCaptcha for public forms (#4017) 2022-02-18 20:02:50 +05:30
chatwoot_exception_tracker.rb fix: modify exception tracker to log even if sentry configured (#7563) 2023-07-21 11:58:49 +03:00
chatwoot_hub.rb chore: Update self-hosted billing redirect (#8580) 2023-12-18 16:34:47 -08:00
chatwoot_markdown_renderer.rb feat: Updated the design of the category page (#8165) 2023-11-08 18:03:16 -08:00
config_loader.rb chore: Add display manifest to whitelabel settings (#8708) 2024-01-16 09:50:23 +04:00
csml_engine.rb feat: Add native support for CSML in agent_bot API (#4913) 2022-06-23 19:17:46 +05:30
current.rb Automation enhancement (#4087) 2022-03-21 13:12:27 +05:30
custom_markdown_renderer.rb fix: Renders youtube and vimeo links within blank lines as embeds (#7422) 2023-09-21 00:16:03 -07:00
dyte.rb feat: Add backend APIs for Dyte integration (#6197) 2023-01-09 12:37:18 +05:30
exception_list.rb Fix: prevent IMAPBadResponse exception from sending the authorization mail (#7154) 2023-05-22 13:51:56 +05:30
global_config_service.rb chore: Update dependencies to the latest versions (#5033) 2022-07-15 09:51:59 +07:00
global_config.rb chore: Add display manifest to whitelabel settings (#8708) 2024-01-16 09:50:23 +04:00
limits.rb chore: Increase the limit of greeting message (#7883) 2023-09-08 15:33:09 +05:30
microsoft_graph_auth.rb Revert "feat: Support Azure single-tenant application using the Graph… (#7436) 2023-06-29 16:50:18 -07:00
online_status_tracker.rb feat: Enable Capacity Config UI (#5164) 2022-08-16 16:58:23 +05:30
regex_helper.rb fix: flaky test whatsapp_cloud_service_spec.rb:17 (#5786) 2022-11-01 19:51:42 -07:00
url_helper.rb fix: Referer URL validation (#4309) 2022-03-30 14:36:22 +05:30
vapid_service.rb chore: Switch to web-push gem (#6390) 2023-02-03 18:55:22 +05:30