How can I run a persistent background job with Sidekiq?

I have successfully used Sidekiq to run background jobs triggered by user actions in a Rails 3.2 application. My specific application includes sending and receiving data from and from an external source through a third-party API.

I need to synchronize data with this external source, constantly checking for each user if there is data available for download.

All my background jobs are still user initiated, as I mentioned above. My question is: if I want to constantly check for external data for each user in a while , I suppose:

 # pseudocode while true for user in User.active data = user.get_data next if data.blank? UserDataWorker.perform_async(user.id) end end 

then where should I put the code above? I am confused about how to generate a continuous task. I do not want to put the code in a Sidekiq employee, because it will be just a job that never ends. Or should I do this?

I don’t want to put it in the rake task, because then how can I control this rake task? Everything I read about Sidekiq seems to indicate a Worker performing a task that is finite and can be completed or erroneous. I appreciate any help you can offer.

+6
source share
1 answer

This requires Ruby multithreading. Create a thread in the initializers folder:

 Thread.new do while true for user in User.active data = user.get_data next if data.blank? UserDataWorker.perform_async(user.id) end sleep 1 end end 

If your checks do not need a lot of resources, everything should be in order.

+4
source

Source: https://habr.com/ru/post/950088/


All Articles