Skip to content

Instantly share code, notes, and snippets.

@bryanhelmig
Created May 15, 2012 17:54
Show Gist options
  • Save bryanhelmig/2703708 to your computer and use it in GitHub Desktop.
Save bryanhelmig/2703708 to your computer and use it in GitHub Desktop.
queue examples
# bad way
def new_message(request):
user = get_user_or_404(request)
message = request.POST.get('message', None)
if not message:
raise Http404
user.save_new_message(message)
for friend in user.friends.all():
friend.send_email(message)
return redirect(reverse('dashboard'))
# good way
@task
def alert_friends(user_id, message):
user = User.objects.get(id=user_id)
for friend in user.friends.all():
friend.send_email(message)
def new_message(request):
user = get_user_or_404(request)
message = request.POST.get('message', None)
if not message:
raise Http404
user.save_new_message(message)
alert_friends.delay(user.id, message)
return redirect(reverse('dashboard'))
# bad way
class MessageController < ActionController::Base
# using devise? i guess this works?
def index
if not params[:message]
raise Http404
current_user.save_new_message(params[:message])
current_user.friends.each { |friend| friend.send_email(params[:message]) }
redirect_to dashboard
end
end
# good way
class MessageSend
def self.perform(user_id, message)
user = User.find(user_id)
user.friends.each { |friend| friend.send_email(message) }
end
end
class MessageController < ActionController::Base
# using devise? i guess this works?
def index
if not params[:message]
raise Http404
current_user.save_new_message(params[:message])
Resque.enqueue(MessageSend, current_user.id, (params[:message])
redirect_to dashboard
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment