Class: StupidSMS::ProcessQueue
- Inherits:
-
Object
- Object
- StupidSMS::ProcessQueue
- Defined in:
- lib/stupid_sms/process_queue.rb
Constant Summary collapse
- MAX_SMS_LENGTH =
160.0
Class Method Summary collapse
Instance Method Summary collapse
- #call ⇒ Object
-
#initialize(sms_queue:, template:, dry_run:, max_threads:) ⇒ ProcessQueue
constructor
A new instance of ProcessQueue.
Constructor Details
#initialize(sms_queue:, template:, dry_run:, max_threads:) ⇒ ProcessQueue
Returns a new instance of ProcessQueue.
9 10 11 12 13 14 15 16 17 |
# File 'lib/stupid_sms/process_queue.rb', line 9 def initialize(sms_queue:, template:, dry_run:, max_threads:) @sms_queue = sms_queue @template = template @dry_run = dry_run @max_threads = Integer(max_threads) # Stats @recipients_count = @sms_queue.length end |
Class Method Details
.call(**args) ⇒ Object
5 6 7 |
# File 'lib/stupid_sms/process_queue.rb', line 5 def self.call(**args) new(**args).call end |
Instance Method Details
#call ⇒ Object
19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 |
# File 'lib/stupid_sms/process_queue.rb', line 19 def call threads = @max_threads.times.map do Thread.new do # We need one client per Thread since the Twilio client is not thread safe client = SMSClient.new results = { send_count: 0, longest_body: 0, failed_count: 0 } until @sms_queue.empty? # TODO: Consider capturing all errors and only log them sms_result = process_sms( client: client, person: @sms_queue.pop ) results[:send_count] += sms_result.fetch(:send_count) results[:failed_count] += 1 unless sms_result.fetch(:success) body_length = sms_result.fetch(:length) if body_length > results[:longest_body] results[:longest_body] = body_length end end results end end threads.map(&:join) # Wait for each thread calculate_summary(threads: threads) end |