Skip to content

Instantly share code, notes, and snippets.

@ayerdines
Last active April 21, 2022 10:20
Show Gist options
  • Save ayerdines/1a25695f42a55b9e62ce40765abe1304 to your computer and use it in GitHub Desktop.
Save ayerdines/1a25695f42a55b9e62ce40765abe1304 to your computer and use it in GitHub Desktop.
Read before using delayed jobs

Things to be careful about when using delayed jobs

  1. Do not use delayed jobs if your system can have 100k+ jobs at a time in delayed jobs table. It'll cause the jobs to run very slow.

  2. Check conditions before adding the jobs to queue, instead of checking inside the job. This'll prevent delayed jobs table from being over-populated.

    # bad
    class Order < ApplicationRecord
      after_create :send_to_webhook
      
      def send_to_webhook
        if condition?
          # code
        end
      end
      handle_asynchronously :send_to_webhook
    end
    
    # good
    class Order < ApplicationRecord
      after_create :send_to_webhook, if: :condition?
      
      def send_to_webhook
        # code
      end
      handle_asynchronously :send_to_webhook
    end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment