Skip to content

Instantly share code, notes, and snippets.

@tansengming
Created January 27, 2017 22:14
Show Gist options
  • Save tansengming/592bab922c5c315c7ff7ac7dd0e8bed1 to your computer and use it in GitHub Desktop.
Save tansengming/592bab922c5c315c7ff7ac7dd0e8bed1 to your computer and use it in GitHub Desktop.
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