Created
February 21, 2017 16:14
-
-
Save tansengming/1d8c97843a76c0b59903643b18cba267 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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