$npx -y skills add ThibautBaissac/rails_ai_agents --skill job-patternsImplements shallow background jobs with _later/_now conventions using Solid Queue. Use when adding background processing, async operations, scheduled tasks, or when user mentions jobs, queues, workers, or background processing. WHEN NOT: Business logic implementation (use model-p
| 1 | # Job Patterns (37signals) |
| 2 | |
| 3 | Jobs orchestrate. Models do the work. Background jobs are thin wrappers around model methods. |
| 4 | |
| 5 | ## Project knowledge |
| 6 | |
| 7 | **Tech Stack:** Rails 8.2 (edge), Solid Queue, ActiveJob |
| 8 | **Pattern:** Thin jobs call model methods; models have `_later`/`_now` pairs |
| 9 | |
| 10 | **Commands:** |
| 11 | ```bash |
| 12 | bin/rails generate job NotifyRecipients # Generate job |
| 13 | bundle exec rake solid_queue:start # Run worker |
| 14 | bin/rails runner "puts SolidQueue::Job.count" # Check queue |
| 15 | bin/rails runner "SolidQueue::Job.destroy_all" # Clear jobs |
| 16 | ``` |
| 17 | |
| 18 | ## Why shallow jobs |
| 19 | |
| 20 | - Business logic stays in models (testable, reusable) |
| 21 | - Jobs are simple orchestrators |
| 22 | - Easy to run sync or async |
| 23 | - Can call methods directly in tests |
| 24 | - Clearer separation of concerns |
| 25 | |
| 26 | ## Why _later/_now convention |
| 27 | |
| 28 | - Clear which version is async |
| 29 | - Default method can be sync (explicit async) |
| 30 | - Easy to switch between sync/async |
| 31 | - Testable (call `_now` in tests) |
| 32 | |
| 33 | ## Core pattern: shallow job |
| 34 | |
| 35 | The job receives a model and calls its `_now` method. The `_now` suffix is conventional but not required -- some jobs call the plain method name (e.g., `notifiable.notify_recipients`). |
| 36 | |
| 37 | ```ruby |
| 38 | # app/jobs/notify_recipients_job.rb |
| 39 | class NotifyRecipientsJob < ApplicationJob |
| 40 | queue_as :default |
| 41 | |
| 42 | def perform(notifiable) |
| 43 | notifiable.notify_recipients_now |
| 44 | end |
| 45 | end |
| 46 | ``` |
| 47 | |
| 48 | The model defines both `_later` and `_now` methods: |
| 49 | |
| 50 | ```ruby |
| 51 | # In model or concern |
| 52 | def notify_recipients_later |
| 53 | NotifyRecipientsJob.perform_later(self) |
| 54 | end |
| 55 | |
| 56 | def notify_recipients_now |
| 57 | recipients.each do |recipient| |
| 58 | next if recipient == creator |
| 59 | Notification.create!(recipient: recipient, notifiable: self, action: notification_action) |
| 60 | end |
| 61 | end |
| 62 | |
| 63 | # Default to sync |
| 64 | def notify_recipients |
| 65 | notify_recipients_now |
| 66 | end |
| 67 | |
| 68 | # Trigger async from callbacks |
| 69 | after_create_commit :notify_recipients_later |
| 70 | ``` |
| 71 | |
| 72 | ## Job patterns |
| 73 | |
| 74 | ### Notification job |
| 75 | |
| 76 | ```ruby |
| 77 | class NotifyRecipientsJob < ApplicationJob |
| 78 | queue_as :default |
| 79 | |
| 80 | def perform(notifiable) |
| 81 | notifiable.notify_recipients_now |
| 82 | end |
| 83 | end |
| 84 | ``` |
| 85 | |
| 86 | ### Batch processing job |
| 87 | |
| 88 | ```ruby |
| 89 | class DeliverBundledNotificationsJob < ApplicationJob |
| 90 | queue_as :default |
| 91 | |
| 92 | def perform |
| 93 | Notification::Bundle.deliver_all_now |
| 94 | end |
| 95 | end |
| 96 | ``` |
| 97 | |
| 98 | ### Cleanup job |
| 99 | |
| 100 | ```ruby |
| 101 | class SessionCleanupJob < ApplicationJob |
| 102 | queue_as :low_priority |
| 103 | |
| 104 | def perform |
| 105 | Session.cleanup_old_sessions_now |
| 106 | end |
| 107 | end |
| 108 | ``` |
| 109 | |
| 110 | ### Event tracking job |
| 111 | |
| 112 | ```ruby |
| 113 | class TrackEventJob < ApplicationJob |
| 114 | queue_as :default |
| 115 | |
| 116 | def perform(eventable, action, options = {}) |
| 117 | eventable.track_event_now(action, options) |
| 118 | end |
| 119 | end |
| 120 | ``` |
| 121 | |
| 122 | ### Broadcasting job |
| 123 | |
| 124 | ```ruby |
| 125 | class BroadcastUpdateJob < ApplicationJob |
| 126 | queue_as :default |
| 127 | |
| 128 | def perform(broadcastable) |
| 129 | broadcastable.broadcast_update_now |
| 130 | end |
| 131 | end |
| 132 | ``` |
| 133 | |
| 134 | ### External API job |
| 135 | |
| 136 | ```ruby |
| 137 | class DispatchWebhookJob < ApplicationJob |
| 138 | queue_as :webhooks |
| 139 | retry_on StandardError, wait: :exponentially_longer, attempts: 5 |
| 140 | |
| 141 | def perform(webhook, event) |
| 142 | webhook.dispatch_now(event) |
| 143 | end |
| 144 | end |
| 145 | ``` |
| 146 | |
| 147 | ## Retry and error handling |
| 148 | |
| 149 | ```ruby |
| 150 | class DispatchWebhookJob < ApplicationJob |
| 151 | discard_on Webhook::InvalidUrl # Don't retry |
| 152 | retry_on StandardError, wait: :exponentially_longer, attempts: 5 # Backoff |
| 153 | retry_on CustomError, wait: 5.minutes, attempts: 3 # Fixed interval |
| 154 | |
| 155 | rescue_from Webhook::Timeout do |exception| |
| 156 | webhook.mark_as_slow! |
| 157 | raise exception # Re-raise to trigger retry |
| 158 | end |
| 159 | |
| 160 | def perform(webhook, event) |
| 161 | webhook.dispatch_now(event) |
| 162 | end |
| 163 | end |
| 164 | ``` |
| 165 | |
| 166 | ## Current attributes in jobs |
| 167 | |
| 168 | Always set and reset `Current` context: |
| 169 | |
| 170 | ```ruby |
| 171 | class NotifyRecipientsJob < ApplicationJob |
| 172 | before_perform do |job| |
| 173 | notifiable = job.arguments.first |
| 174 | Current.account = notifiable.account |
| 175 | Current.user = notifiable.creator if notifiable.respond_to?(:creator) |
| 176 | end |
| 177 | |
| 178 | after_perform { Current.reset } |
| 179 | |
| 180 | def perform(notifiable) |
| 181 | notifiable.notify_recipients_now |
| 182 | end |
| 183 | end |
| 184 | ``` |
| 185 | |
| 186 | ## Performance patterns |
| 187 | |
| 188 | ### Batch processing |
| 189 | |
| 190 | ```ruby |
| 191 | # Enqueue in batches |
| 192 | Card.active.pluck(:id).each_slice(100) do |batch| |
| 193 | ProcessCardsJob.perform_later(batch) |
| 194 | end |
| 195 | |
| 196 | class ProcessCardsJob < ApplicationJob |
| 197 | def perform(card_ids) |
| 198 | Card.where(id: card_ids).find_each(&:process_now) |
| 199 | end |
| 200 | end |
| 201 | ``` |
| 202 | |
| 203 | ### Debouncing (avoid duplicate jobs) |
| 204 | |
| 205 | ```ruby |
| 206 | def reindex_later |
| 207 | return if reindex_job_queued? |
| 208 | ReindexBoardJob.perform_later(id) |
| 209 | end |
| 210 | |
| 211 | def reindex_job_queued? |
| 212 | SolidQueue::Job.exists?( |
| 213 | job_class: "ReindexBoardJob", |
| 214 | arguments: [id].to_json, |
| 215 | finished_at: nil |
| 216 | ) |
| 217 | end |
| 218 | ``` |
| 219 | |
| 220 | ## Tes |