Created
January 27, 2017 22:14
-
-
Save tansengming/592bab922c5c315c7ff7ac7dd0e8bed1 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
class SmsSenderJob < ApplicationJob | |
def perform(recipient, message) | |
send_sms(recipient, message) | |
end | |
def send_sms(recipient, message) | |
# ... | |
end | |
end | |
# You then notify everyone in a community with, | |
commnunity.users.each do |recipient| | |
community = recipient.community | |
sms_credits = community.sms_credits | |
if sms_credits > 0 | |
# this chucks the job to a queue and runs it asynchronously | |
SmsSenderJob.perform_later recipient, 'Everything is fine. This is fine.' | |
community.update_attributes!(:sms_credits, sms_credits - 1) | |
end | |
end | |
# PS: This is still pretty scrappy. Do not copy. |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment