Image of pethotel.io home page

Implemented Automated Mailers and Background Jobs with Sidekiq

Enhancing user engagement through timely email notifications

Overview

At Pethotel.io, effective communication with our users is crucial. We noticed that important notifications, such as review requests and booking updates, were not reaching users efficiently. To address this, we implemented automated email notifications and background jobs using Sidekiq in our Ruby on Rails application.

By offloading email sending and other time-consuming tasks to background, we improved reliability but most importantly were able to run tasks that were scheduled at specific times without blocking the user from what they were doing.

Objectives

Background

Initially, our application handled email sending synchronously within controller actions. This approach led to delays in user interactions and occasional timeouts, especially when sending emails to multiple recipients. We needed a solution that could handle email delivery efficiently without impacting the user experience.

Recognizing these challenges, we decided to integrate Sidekiq for background processing. This allowed us to handle email sending and other heavy tasks asynchronously, improving performance and reliability.

Sidekiq Dashboard

Communicating things to customers

Feedback requests

Scheduled task to request users to leave review after their booking has ended.

Chat message notifications

Real-time notifications for chat messages between users and pet sitter.

New booking notifications

Real-time notifications for new bookings.

Booking reminders

Scheduled reminders for upcoming bookings or bookings that require action.

Review reminders

Scheduled reminders for users to leave a review after their booking has ended.

Communicating cancellations or changes

Notifications for booking cancellations or changes.

Receipts from charges

Receipt notifications for charges.

Implementation

Approach

Ruby on Rails served as the foundation for our application. We integrated Sidekiq for background job processing and utilized Action Mailer for sending emails.

We created background jobs to handle email sending and scheduled tasks using Sidekiq's cron feature.

Sidekiq Configuration

We configured Sidekiq to process background jobs and set up a cron job for fetching orders that needed review requests.

For example below is a configuration for a Sidekiq job that fetches orders for review requests. This scheduled task will request Sidekiq to run FetchOrdersForReviewJob in the background every day at 7 AM in order to ask user for a review.


      fetch_orders_for_review_job:
        cron: "0 7 * * *"
        class: "FetchOrdersForReviewJob"
        queue: default
    

This scheduled task will request sidekiq to run in the background the FetchOrdersForReviewJob every day at 7 AM.

Scheduled Sidekiq Jobs

Scheduled jobs were used to for example automate the review requesting process. The job queries orders that have ended but have not received a review yet, then sends review request emails to the users which booking has just ended.

The job queries orders that have ended but have not received a review yet, then sends review request emails to the users.

Mailers

We structured our mailers to handle different types of emails, such as authentication codes, order receipts, and review requests.


      class UserMailer < **
        def review_request_email(order_id:, review_link:)
          # -- EMAIL CONTENT HERE TO BE INCLUDED IN THE HTML & TEXT EMAILS --
        end
      end
    

For managers, we created a separate ManagerMailer to handle notifications specific to them.


      def new_order_notification(order_id, hotel_id)
        # -- Notification logic here --
      end
    

Error Handling and Logging

We included error handling in our jobs to ensure robustness. For instance, we checked for null emails, archived hotels, and other potential issues. Logging was added to track the progress and identify any issues during execution.

Integration with Third-Party Services

For certain notifications, we integrated with Zapier to handle additional workflows and automate processes.

Testing and Validation

We thoroughly tested the background jobs and mailers to ensure they worked as expected and emails were delivered correctly.

Email Preview

Challenges Faced

Query Logic can have unaddressed blind spots leading to logic errors

For example crafting the database queries to accurately select the correct orders for review requests is moderate effort as it needs to consider state of the booking, when it has ended, if it has already been reviewed etc. and more.

Error Handling in Background Jobs

Ensuring that the background jobs handled errors gracefully without crashing required careful implementation. We used exception handling and logging to manage unexpected issues.

Email Deliverability

We had to make sure that our emails were not marked as spam and were delivered promptly to users. This involved configuring proper email headers and adhering to best practices.

Results

Implementing mailers and background jobs with Sidekiq led to significant improvements.

Lessons Learned

By implementing automated mailers and background jobs with Sidekiq, we enhanced user communication and application performance. The improvements led to increased user engagement and a more maintainable codebase. Moving forward, we plan to continue leveraging background processing for other time-intensive tasks.

The results demonstrate the value of investing time in optimizing backend processes to improve overall user satisfaction.