Execute a short background task in a rails controller

Let's take an example :

class UsersController
  def create
    User.create(...)
    PushService.push('you have a new friend') # Can take 1/2 seconds
  end
end

I want to execute a short task (Pushing some users) in the controller. I don't do it synchronously because it can increase the response time, but using Resque, Sidekiq or DelayJob seems exaggerated to me.

What would be the consequences of using a simple Thread, is that a good practice to use it in a controller? Do you have other alternatives?

Upvotes: 0

Views: 548

Answers (1)

Vasfed
Vasfed

Reputation: 18454

Starting a thread it not that bad for simple things, but you'll have to manually handle all possibilities of it going wrong (freezing, exceptions)

You might want to look into using sucker_punch ActiveJob backend - it runs tasks inprocess, but you'll have the benefits of activejob (easier testing, generators etc.) and ability to switch to other backend later, if needed.

Upvotes: 1

Related Questions