What is the best way to run a background process in Rails 5?
Asked Answered
F

2

6

I have a web application where I need to perform a process that takes a while to complete (Usually 1 minute).

I'll try to explain it briefly: In my app I have an algorithm that assigns foreign keys to a set of objects based on a bunch of parameters (mainly dates). When a user presses the designated button inside my application a controller method is executed. Inside that method I make a call to a method in the model where all the logic is handled and the keys are being assigned. As mentioned before, the whole process takes about a minute to complete.

So my question is: What is the best way to run this process in the background in Rails 5? I obviously don't want to force my users to wait an entire minute before they can navigate around the application and I also don't want the application to time out while the browser is waiting for a response from the server. So what's the best way to handle this? Do I need a framework that can make the request asynchronously? If so, which one? (I would prefer if it doesn't require too many dependencies and if I can keep using ActiveRecord).

I haven't really worked too much with ActionCable or AJAX, but if they can get the job done in any way, then I would be thrilled to know how.

Ideally I should be able to send a notification to the user, within the app, once the process is done

view.html.erb:

# Button the user presses to execute the algorithm
<%= link_to 'execute algorithm', algorithm_path(@variable), :method => :put %>

my_controller.rb:

def button_method
  Object.execute_algorithm(@variable)
  redirect_to :back
end

Object.rb:

def self.execute_algorithm(variable)
  # Logic
end
Fachanan answered 23/11, 2018 at 16:13 Comment(1)
ActiveJob should be able to handle this. Though I'm curious what's the purpose of the custom foreign key process, and why does it take a minute?Fibroin
P
6

Rails uses ActiveJob, which allow you to queue up an expensive tasks and run them in the background. They are quite simple to use.

Fist, you can use rails generator to create one:

rails generate job slow_algorithm

...which will create a job under app/jobs/slow_algorithm_job.rb. There, you can implement your algorithm logic.

Queue up the job is just as easy, simply do it in your controller:

SlowAlgorithmJob.perform_later

Last thing you need to cover is setting up queuing backend that will actually run the jobs. Rails supports couple of them. Probably the simplest one to set up is delayed_job, but if you're looking for something more scalable, I'd recommend looking into Sidekiq.

EDIT: How to notify user that the job is done?

In order to do this, you clearly have to somehow track the progress of the task. One possible direction is to create a record, let's call it a Task, which represents one invocation of your algorithm.

When user triggers the action that should queue up the job, Task record is created with status pending, then before_perform and after_perform job hooks can be used to move the Task status to running and then to completed.

after_perform hook can be used to notify the customer. How the app is going to do it, is up to you. For example, Dropbox uses similar system when downloading large amount of files. They queue up the job, the zips ups the files and then send the email with download link once the job is finished.

Another approach would be using in-app notification system.

Lastly, to have the notification pop-up without requiring user to refresh the page, you'd have to use ActiveCable and push the notification back to user, again, using after_perform hook.

Permittivity answered 23/11, 2018 at 17:25 Comment(1)
I've updated the answer with a suggestion how to achieve that. Cheers.Scurry
T
2

I will suggest sidekiq as a better solution for background tasks

Transformer answered 24/11, 2018 at 4:53 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.