Skip to content

Commit

Permalink
feat: track publish count (#35)
Browse files Browse the repository at this point in the history
  • Loading branch information
scmmishra authored Nov 1, 2024
1 parent 27bbe69 commit bebc9a8
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 1 deletion.
8 changes: 8 additions & 0 deletions app/jobs/send_post_batch_job.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
class SendPostBatchJob < ApplicationJob
queue_as :default
after_perform: :update_batch_count

UNSUBSCRIBE_PLACEHOLDER = "{{unsubscribe_link}}"

Expand All @@ -14,6 +15,13 @@ def perform(post_id, batch_subscribers)

private

def update_batch_count
post_id = @post.id
remaining_batches = Rails.cache.decrement("post_#{post_id}_batches_remaining")

@post.publish if remaining_batches <= 0
end

def send_batch(batch_subscribers)
Rails.logger.info "[PostMailer] Sending #{@post.title} to #{batch_subscribers.count} subscribers"
batch_params = batch_subscribers.map do |subscriber|
Expand Down
4 changes: 3 additions & 1 deletion app/jobs/send_post_job.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,11 @@ def perform(post_id)
@newsletter = @post.newsletter
@post.update(status: :processing)

total_batches = (@newsletter.subscribers.verified.count.to_f / BATCH_SIZE).ceil
Rails.cache.write("post_#{post_id}_batches_remaining", total_batches)

@newsletter.subscribers.verified.find_in_batches(batch_size: BATCH_SIZE) do |batch_subscribers|
SendPostBatchJob.perform_later(@post.id, batch_subscribers)
end
@post.publish
end
end

0 comments on commit bebc9a8

Please sign in to comment.