0

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 ?

Aleksei Matiushkin
  • 119,336
  • 10
  • 100
  • 160
user3675188
  • 7,271
  • 11
  • 40
  • 76
  • Can you describe your demand more detail? If you want to run task with different inverval, you can have two tasks and add more condition to check if need run. – dddd1919 Jan 19 '15 at 06:28
  • 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. – Aleksei Matiushkin Jan 19 '15 at 06:54
  • Change task frequency dynamically, you can call `system('whenever --update-crontab .......')` when catch a change with your stock, then crontab will update to new frequency – dddd1919 Jan 19 '15 at 07:04
  • you can also use any conditional in the whenever block look this post it will clear your doubts http://stackoverflow.com/questions/4602418/rails-whenever-gem-dynamic-values – Rajarshi Das Jan 19 '15 at 07:05

1 Answers1

1

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.

Aleksei Matiushkin
  • 119,336
  • 10
  • 100
  • 160