Class: ArticlePdfWorker

Inherits:
Object
  • Object
show all
Includes:
Sidekiq::Worker
Defined in:
app/workers/article_pdf_worker.rb

Overview

Generates an article PDF in a Sidekiq process (outside Passenger workers) and
attaches it to an existing Communication, then optionally queues/releases the
communication for delivery.

Playwright/Chromium-based PDF generation is memory-intensive (~200-300 MB per
render). Running it in Sidekiq isolates that cost from the web process pool.

Instance Method Summary collapse

Instance Method Details

#perform(article_id, communication_id, dispatch_mode = nil) ⇒ Object

Parameters:

  • article_id (Integer)

    Article to render as PDF

  • communication_id (Integer)

    Communication to attach the PDF to

  • dispatch_mode (String, nil) (defaults to: nil)

    "release" → send immediately, "queue" → queue for delivery, nil → keep as draft



29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'app/workers/article_pdf_worker.rb', line 29

def perform(article_id, communication_id, dispatch_mode = nil)
  article       = Article.find(article_id)
  communication = Communication.find(communication_id)

  communication.with_lock do
    unless communication.uploads.where(category: 'article_pdf').exists?
      upload = article.generate_pdf
      communication.uploads << upload
    end
  end

  case dispatch_mode
  when 'release' then communication.release
  when 'queue'   then communication.queue
  end
end