A job class written by hand, past mailer delivery

Free preview 2 min to read

A plain job, past deliver_later

"Mailers: templates & async" traced Solid Queue through deliver_later, Action Mailer's wrapper around a job. This app also has five job classes of its own in app/jobs/: one grades challenge submissions, three handle partner work (site icons, certificate announcements, webhook delivery), and one prunes stale partner nonces. The plainest is app/jobs/partners/fetch_site_icon_job.rb, in full:

module Partners
  class FetchSiteIconJob < ApplicationJob
    queue_as :default

    def perform(partner_id)
      partner = ::Partners::Partner.find_by(id: partner_id)
      ::Partners::FetchSiteIconService.call(partner: partner) if partner && !partner.logo.attached?
    end
  end
end

Its parent is this app's base class, which in full is class ApplicationJob < ActiveJob::Base; end. It adds no shared logic, so each job declares its own behavior.

The call site, and a deliberate choice

app/services/admin/partners/save_partner_service.rb:

::Partners::FetchSiteIconJob.perform_later(partner.id) unless partner.logo.attached?

The job receives partner.id, a plain Integer, and not the partner object already loaded in memory. perform takes partner_id and fetches the record itself with find_by. That's an ActiveJob convention with two benefits. The arguments are serialized into the job row, which lesson 2 inspects. And a fresh find_by at execution time reads the partner as it is when the job runs, rather than as it was when perform_later was called. If the partner was deleted in between, find_by returns nil and the job does nothing.

What this course covers

Seven lessons, each checked against this app's jobs, config and installed Solid Queue tables. This lesson covers the job class and its call site. Lesson 2 shows what perform_later writes to a SolidQueue::Job row. Lesson 3 lists the eleven tables a job moves through. Lesson 4 reads config/queue.yml, with its dispatcher and three worker pools. Lesson 5 explains why Solid Queue's tables don't show up through this app's default connection in development. Lesson 6 covers retry_on, discard_on and limits_concurrency, and which of this app's jobs use them.

Lesson 1 of 7 · free

Keep going with lesson 2

perform_later creates a SolidQueue::Job row you can inspect

Shows the SolidQueue::Job and ReadyExecution rows that perform_later creates once the development :async adapter is switched off.

A free account also gives you the quizzes, your progress and notes, and the discussion.

Type to start searching...