user3675188
user3675188

Reputation: 7409

To run periodic tasks with dynamic changing period

I have a task that is to monitor some information from stock website.

It should check the stock status the website every 10 minute,

Once if the stock index is rising, the check period should change to every 5 second.

If the sotck index is downing, then the check period should be 10 minute.

For running a periodic task

I found gem whenever can do cron task. But I have no ideas to dynamically change the check period ? any ideas ?

Upvotes: 0

Views: 347

Answers (1)

Aleksei Matiushkin
Aleksei Matiushkin

Reputation: 121000

For this simple task I would create two different cron tasks and one, say, “global” variable, denoting whether the frequent task should be actually run. That way the per-10-mins task will be run always and per-5-secs task will be run if and only the index is rising.

class Checker
  @@rising = false
  class << self
    def check
      @@rising = actual_check > 0 # core check func
    end
    def freq_check
      check if @@rising
    end
    def rare_check
      check
    end
  end
end

every 10.minutes do
  runner "Checker.rare_check"
end
every 5.seconds do
  runner "Checker.freq_check"
end

This is definitely not a most elegant solution, but it does a trick and is really easy to handle/test.

Upvotes: 1

Related Questions