diff options
author | Nicolò€ Rebughini <nicolo.rebughini@gmail.com> | 2021-02-26 10:06:09 +0100 |
---|---|---|
committer | Nicolò€ Rebughini <nicolo.rebughini@gmail.com> | 2021-02-26 15:19:34 +0100 |
commit | 580fa7b2c62dba52fce7fb87def9eb1254507534 (patch) | |
tree | 601a11ab51054b776737e009e9000e86cb5b6d70 /lib/solidus_subscriptions/processor.rb | |
parent | cfcb35e8b1211d7aac66c709610c1cdc707c5db8 (diff) |
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.
Diffstat (limited to 'lib/solidus_subscriptions/processor.rb')
-rw-r--r-- | lib/solidus_subscriptions/processor.rb | 31 |
1 files changed, 6 insertions, 25 deletions
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 |