Skip to content

Instantly share code, notes, and snippets.

@tansengming
Created February 21, 2017 16:14
Show Gist options
  • Save tansengming/1d8c97843a76c0b59903643b18cba267 to your computer and use it in GitHub Desktop.
Save tansengming/1d8c97843a76c0b59903643b18cba267 to your computer and use it in GitHub Desktop.
commnunity.users.each do |recipient|
# this chucks the job into a queue and returns immediately!
# it no longer takes a while to finish.
SmsSenderJob.perform_later recipient, community, 'Everything is fine.'
end
# Here's where you define the job that gets run by the workers
class SmsSenderJob < ApplicationJob
def perform(recipient, community, message)
sms_credits = community.sms_credits
if sms_credits > 0
send_sms recipient, message # this still takes a while.
# take off a credit
community.sms_credits = sms_credits - 1
community.save
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment