Class: Tolliver::Services::Policies::Batch
- Inherits:
-
Object
- Object
- Tolliver::Services::Policies::Batch
- Defined in:
- lib/tolliver/services/policies/batch.rb
Instance Method Summary collapse
- #deliver(notification_delivery, batch_size = 10) ⇒ Object
- #deliver_batch(notification_delivery, batch_size = 10) ⇒ Object
- #deliver_batch_and_enqueue(notification_delivery, batch_size = 10) ⇒ Object
Instance Method Details
#deliver(notification_delivery, batch_size = 10) ⇒ Object
17 18 19 20 21 22 23 24 |
# File 'lib/tolliver/services/policies/batch.rb', line 17 def deliver(notification_delivery, batch_size = 10) # Input validation return nil if notification_delivery.nil? || notification_delivery.policy != :batch # Enqueue Tolliver::Jobs::BatchPolicyJob.perform_later(notification_delivery.id, batch_size) end |
#deliver_batch(notification_delivery, batch_size = 10) ⇒ Object
49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 |
# File 'lib/tolliver/services/policies/batch.rb', line 49 def deliver_batch(notification_delivery, batch_size = 10) # Input validation return nil if notification_delivery.nil? || notification_delivery.policy != :batch # Nothing to do return 0 if notification_delivery.sent_count == notification_delivery.receivers_count # Get batch of receivers prepared for send notification_receivers = notification_delivery.notification_receivers.where(sent_at: nil).limit(batch_size) # Send entire batch sent_counter = 0 notification_receivers.each do |notification_receiver| sent_counter += 1 if notification_delivery.method_service.deliver(notification_receiver) end # Is postponed notification_delivery.is_postponed = false # Update statistics notification_delivery.sent_count += sent_counter notification_delivery.sent_at = Time.current if notification_delivery.sent_count == notification_delivery.receivers_count # Save notification_delivery.save (notification_delivery.receivers_count - notification_delivery.sent_count) end |
#deliver_batch_and_enqueue(notification_delivery, batch_size = 10) ⇒ Object
26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 |
# File 'lib/tolliver/services/policies/batch.rb', line 26 def deliver_batch_and_enqueue(notification_delivery, batch_size = 10) # Input validation return nil if notification_delivery.nil? || notification_delivery.policy != :batch # Send single batch remaining = deliver_batch(notification_delivery, batch_size) # If still some receivers remaining, enqueue next batch if remaining > 0 # Sleep for a while to prevent SMTP/SMS service overflow sleep 5 # seconds # Queue next batch deliver(notification_delivery, batch_size) false else true end end |