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
In the background the server can process all background jobs one by one. Rails provides Active Job to process background jobs and making them run on a variety of queueing applications.
A server running your rails app uses workers to do all of the things an application needs. It sends email, runs requests, performs calculations etc. The number of workers you need depends on how much traffic and calculations your web app needs to do.
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.
I will suggest sidekiq as a better solution for background tasks
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With