3

I am reading some sensor values and I want to schedule recurring jobs in order to repeat every :00, :10, :15, :20, :25, :30, :35 etc.

I am using clockwork/delayed_job/activeJob & foreman in Rails 5 and I am able to retrieve and store values to the database.

What I am unable to do is to complete each task/job in its time, meaning that they appear to be queued in a series and then being executed at once. I can see that they might run 5 times at once, while the whole task may take some ms.

clock.rb

require 'clockwork'
require File.expand_path('../boot', __FILE__)
require File.expand_path('../environment', __FILE__)

module Clockwork
  handler do |job, time|
    puts "Running #{job}, at #{time}"
  end

  every(5.minutes, 'SensorJob') { Delayed::Job.enqueue SensorJob.new, queue: :default }

end

sensor_job.rb

class SensorJob < ActiveJob::Base
  queue_as :default

  def perform
    @temperature = Random.new.rand(20..30)
    @humidity    = Random.new.rand(30..70)
    SensorReading.create(temperature: @temperature, humidity: @humidity)
  end

end

What am I missing ?

ftshtw
  • 629
  • 1
  • 5
  • 19

1 Answers1

0

You helped me! Below the description:

every(5.minutes, 'SensorJob') { Delayed::Job.enqueue SensorJob.new, queue: :default }

So I help you! You should use:

Delayed::Job.enqueue SensorJob.new.perform

instead of:

Delayed::Job.enqueue SensorJob.new

You have to use SensorJob's method perform!

Good Luck!!

fkoessler
  • 6,932
  • 11
  • 60
  • 92