From 580fa7b2c62dba52fce7fb87def9eb1254507534 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Nicolo=CC=80=20Rebughini?= Date: Fri, 26 Feb 2021 10:06:09 +0100 Subject: Move subscription processing to background jobs This change moves the processing of individual subscriptions to their own isolated jobs. The tasks of these individual jobs will be: - Deactivate/Cancel subscriptions pending one of those operations - Clear any past failed installment if it's configured to do so - Create a new installment for the subscription cycle - Enqueue a job to process any actionable installment for the subscription Given we've moved the concern of queueing the installment processing, that part can be removed safely from the main processor job. The final part of the change also touches the main processor job, whose task is now to collect any actionable subscription and any subscription with an actionable installment to enqueue them for individual processing. --- lib/solidus_subscriptions/processor.rb | 31 ++++++------------------------- 1 file changed, 6 insertions(+), 25 deletions(-) (limited to 'lib/solidus_subscriptions/processor.rb') diff --git a/lib/solidus_subscriptions/processor.rb b/lib/solidus_subscriptions/processor.rb index ffc274b..04b84e8 100644 --- a/lib/solidus_subscriptions/processor.rb +++ b/lib/solidus_subscriptions/processor.rb @@ -4,32 +4,13 @@ module SolidusSubscriptions class Processor class << self def run - SolidusSubscriptions::Subscription.actionable.find_each(&method(:process_subscription)) - SolidusSubscriptions::Installment.actionable.find_each(&method(:process_installment)) - end - - private - - def process_subscription(subscription) - ActiveRecord::Base.transaction do - subscription.successive_skip_count = 0 - subscription.advance_actionable_date - - subscription.cancel! if subscription.pending_cancellation? - subscription.deactivate! if subscription.can_be_deactivated? - - if SolidusSubscriptions.configuration.clear_past_installments - subscription.installments.unfulfilled.actionable.each do |installment| - installment.update!(actionable_date: nil) - end + SolidusSubscriptions::Subscription + .where(installments: SolidusSubscriptions::Installment.actionable) + .or(SolidusSubscriptions::Subscription.actionable) + .distinct + .find_each do |subscription| + ProcessSubscriptionJob.perform_later(subscription) end - - subscription.installments.create!(actionable_date: Time.zone.now) - end - end - - def process_installment(installment) - ProcessInstallmentJob.perform_later(installment) end end end -- cgit v1.2.3