title: How to schedule Sidekiq jobs according to a recurring schedule in database?
body: <p>I've set up a system where <code>Measurement</code>s are run based on <code>MeasurementSetting</code>s. Each setting defines a schedule using <code>ice_cube</code> (and some other properties for measurements). I'm able to edit this schedule for each setting and then poll for the next occurrences with something like:</p>
  
  <pre><code>def next_occurrences
    schedule.occurrences(Time.now + 1.day)
  end
  </code></pre>
  
  <p>This gives me a set of timestamps when there should be a <code>Measurement</code>.</p>
  
  <p>Now, I also have Sidekiq installed, and I can successfully run a measurement at a specific time using a <code>MeasurementWorker</code>. To do that, I just create an empty <code>Measurement</code> record, associate it with its settings, and then <code>perform_async</code> (or <code>perform_at(...)</code>) this worker:</p>
  
  <pre><code>class MeasurementWorker
    include Sidekiq::Worker
    sidekiq_options retry: 1, backtrace: true
  
    def perform(measurement_id)
      Measurement.find(measurement_id).run
    end
  end
  </code></pre>
  
  <p>What's missing is that I somehow need to create the empty measurements based on a setting's schedule. But how do I do this?</p>
  
  <p>Say I have the following data:</p>
  
  <ul>
  <li><code>MeasurementSetting</code> with ID 1, with a daily schedule at 12:00 PM</li>
  <li><code>MeasurementSetting</code> with ID 2, with an hourly schedule at every full hour (12:00 AM, 01:00 AM, etc.)</li>
  </ul>
  
  <p>Now I need to:</p>
  
  <ul>
  <li>Create a <code>Measurement</code> that uses setting 1, every day at 12:00</li>
  <li>Create a <code>Measurement</code> that uses setting 2, every full hour</li>
  <li>Call the worker for these measurements</li>
  </ul>
  
  <p>But how?</p>
  
  <p>Should I check, say every minute, whether there is a <code>MeasurementSetting</code> that is defined to occur <em>now</em>, and then create the empty <code>Measurement</code> records, then run them with the specific setting using Sidekiq?</p>
  
  <p>Or should I generate the empty <code>Measurement</code> records with their settings in advance, and schedule them this way?</p>
  
  <p>What would be the simplest way to achieve this?</p>