SergiuXG
SergiuXG

Reputation: 43

How can I run a sidekiq worker a certain number of times per day, based on database column?

My Project model:

class Project < ApplicationRecord
  enum :number_of_samples, { once_per_day: 0, three_times_per_day: 1, hourly: 2 }, prefix: true
end

The sidekiq worker:

class PagespeedWorker
  include Sidekiq::Worker
  
  def perform
    puts 'Beginning Fetching...'
    projects = ...
    projects.each do |project|
      sitemap = ...
      sitemap.each do |url|
        retrieve_data(...)
      end
    end
    puts 'Fetching done!'
  end
end

I want to be able to run the worker, based on the number_of_samples in the Project model.
It should run for every project, for every url in the project, the number of times per day specified in the number_of_samples field.

Upvotes: 0

Views: 72

Answers (1)

Vamsi Pavan Mahesh
Vamsi Pavan Mahesh

Reputation: 250

You can use the number_of_samples property to loop

Sample code

projects.each do |project|
  project.number_of_samples.times do
    sitemap_path = project.sitemap_path
    sitemap = SitemapParseService.call(sitemap_path)
    sitemap.each do |url|
      retrieve_data(url, 'desktop', project.id)
      retrieve_data(url, 'mobile', project.id)
    end
  end
end

Upvotes: 1

Related Questions