Skip to content

Instantly share code, notes, and snippets.

@angelsystem
Last active December 31, 2015 04:09
Show Gist options
  • Save angelsystem/7932465 to your computer and use it in GitHub Desktop.
Save angelsystem/7932465 to your computer and use it in GitHub Desktop.
delayed job hooks
class Newsletter < ActiveRecord::Base
def deliver
sleep 5 # simulate long newsletter delivery
update_attribute(:delivered_at, Time.zone.now)
end
handle_asynchronously :deliver
end
===============
class NewslettersController < ApplicationController
def deliver
@newsletter = Newsletter.find(params[:id])
@newsletter.deliver
redirect_to newsletters_url, notice: "enviado newsletters"
end
end
================
class ParanoidNewsletter < Newsletter
def before
p "before"
end
def after(job)
p "after"
end
def success(job)
p "sucess"
p job
record_stat 'newsletter/success'
end
end
==========
#views newsletter/index.html.erb
<% @newsletters.each do |newsletter| %>
<%= link_to "Deliver", deliver_newsletter_path(newsletter), method: :post %>
<% end %>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment