how to use “Scheduler. every ” to read a file and then the re-schedule the check interval time?

75 views
Skip to first unread message

edward

unread,
Dec 1, 2011, 4:16:34 AM12/1/11
to Rufus Ruby
now i want to do something like that:
1. the 1st section program check the config document in every 10
minutes;
2. the 2ed section program using the interval time var gotten from
config document to do a schedule,.
if the interval var is changed in the config document, the 2ed section
program should use the new var. the program is in the following, but
it doesnt work, please comment, many thanks!

require ‘rufus/scheduler’

ti= Time.now.strftime(“%Y-%m-%d %a %H:%M:%S”)
puts “[#{ti}],start to test ”
$basic_schedule_workday=”3m”

s = Rufus::Scheduler::PlainScheduler.start_new
s.every ’4m’ do
read_ini=ReadIni.new
read_ini.read ‘cfg.ini’
ti= Time.now.strftime(“%Y-%m-%d %a %H:%M:%S”)
puts ” [#{ti}]every 4m check ini file to get the
$basic_schedule_workday.”

s.every $basic_schedule_workday do
ti= Time.now.strftime(“%Y-%m-%d %a %H:%M:%S”)
puts “[#{ti}],every #{$basic_schedule_workday} print new interval time
from ini.”
end
end
s.join

John Mettraux

unread,
Dec 1, 2011, 4:36:55 AM12/1/11
to rufus...@googlegroups.com

On Thu, Dec 01, 2011 at 01:16:34AM -0800, edward wrote:
>
> now i want to do something like that:
> 1. the 1st section program check the config document in every 10
> minutes;
> 2. the 2ed section program using the interval time var gotten from
> config document to do a schedule,.
> if the interval var is changed in the config document, the 2ed section
> program should use the new var. the program is in the following, but
> it doesnt work, please comment, many thanks!

Hello Edward,

could you please be more specific about how it doesn't work ?

Please find some inspiration at: http://www.chiark.greenend.org.uk/~sgtatham/bugs.html


Thanks in advance,

--
John Mettraux - http://lambda.io/processi

edward

unread,
Dec 1, 2011, 9:56:33 PM12/1/11
to Rufus Ruby
----program----
require File.dirname(__FILE__) + '/readini'
require 'rufus/scheduler'

ti= Time.now.strftime("%Y-%m-%d %a %H:%M:%S")

puts "[#{ti}],start to test, the initial $basic_schedule_workday=7s"
$basic_schedule_workday="7s"


scheduler1 = Rufus::Scheduler::PlainScheduler.start_new
scheduler1.every '10s' do
read_ini=ReadIni.new
read_ini.read 'cfg2.ini'


ti= Time.now.strftime("%Y-%m-%d %a %H:%M:%S")

puts " [#{ti}]every 10s check ini, the $basic_schedule_workday
is:#{$basic_schedule_workday} ."
end
#scheduler1 = Rufus::Scheduler::PlainScheduler.start_new
scheduler1.every $basic_schedule_workday do


ti= Time.now.strftime("%Y-%m-%d %a %H:%M:%S")

puts "every #{$basic_schedule_workday} interval to print time:
[#{ti}]"

end
scheduler1.join

---------run result--------

[2011-12-02 Fri 10:43:41],start to test, the initial
$basic_schedule_workday=7s ##this interval is right
every 7s interval to print time:[2011-12-02 Fri 10:43:48]
[2011-12-02 Fri 10:43:51]every 10s check ini, the
$basic_schedule_workday is:4s . ##this interval is right,10s
===change the cfg2.ini content, $basic_schedule_workday from 7s to 4s
every 4s interval to print time:[2011-12-02 Fri 10:43:55] ##this
interval is right, 7s
[2011-12-02 Fri 10:44:01]every 10s check ini, the
$basic_schedule_workday is:4s . ##this interval is right,10s
every 4s interval to print time:[2011-12-02 Fri 10:44:02] ##this
interval is not right
every 4s interval to print time:[2011-12-02 Fri 10:44:09] ##this
interval is not right , it is 7s,should be 4s
[2011-12-02 Fri 10:44:11]every 10s check ini, the
$basic_schedule_workday is:5s . ##this interval is right,10s
===change the cfg2.ini content, $basic_schedule_workday from 4s to 5s
every 5s interval to print time:[2011-12-02 Fri 10:44:16] ##this
interval is not right ,it is 7s,should be 5s
[2011-12-02 Fri 10:44:21]every 10s check ini, the
$basic_schedule_workday is:5s . ##this interval is right,10s
every 5s interval to print time:[2011-12-02 Fri 10:44:23] ##this
interval is not right ,it is 7s,should be 5s
every 5s interval to print time:[2011-12-02 Fri 10:44:30] ##this
interval is not right ,it is 7s,should be 5s
[2011-12-02 Fri 10:44:31]every 10s check ini, the
$basic_schedule_workday is:5s . ##this interval is right,10s
every 5s interval to print time:[2011-12-02 Fri 10:44:38] ##this
interval is not right ,it is 8s,should be 5s
[2011-12-02 Fri 10:44:41]every 10s check ini, the
$basic_schedule_workday is:5s . ##this interval is right,10s
every 5s interval to print time:[2011-12-02 Fri 10:44:45] ##this
interval is not right ,it is 7s,should be 5s
every 5s interval to print time:[2011-12-02 Fri 10:44:51] ##this
interval is not right ,it is 6s,should be 5s
[2011-12-02 Fri 10:44:51]every 10s check ini, the
$basic_schedule_workday is:5s . ##this interval is right,10s
every 5s interval to print time:[2011-12-02 Fri 10:44:58]

John Mettraux

unread,
Dec 2, 2011, 12:22:44 AM12/2/11
to rufus...@googlegroups.com
Hello Edward,

I guess I know understand what you mean by "it doesn't work".

I have a question for you. Please consider the following program and tell me how many lines it will output. (it's only Ruby, no rufus-scheduler involved).

---8<---
def do_it(i, &block)
return if i <= 0
block.call
do_it(i - 1, &block)
end

$count = 5

do_it($count) do
puts "$count is #{$count}"
$count = $count + 1
end
--->8---

So, how many lines ?

...

The program you're looking for probably looks something like:

---8<---
require 'rufus-scheduler'

$last_time = Time.now

def format(t)
t.strftime("%Y-%m-%d %a %H:%M:%S")
end

scheduler = Rufus::Scheduler.start_new

workday_job = scheduler.every '7s' do |job|
now = Time.now
puts "{#{format(now)}} every #{job.frequency} interval: [#{now - $last_time}]"
$last_time = now
end

scheduler.every '10s' do
workday_job.instance_variable_set(:@frequency, (rand * 10).to_i + 1)
puts "[#{format(Time.now)}] new frequency: #{workday_job.frequency}"
end

scheduler.join
--->8---


Best regards,

edward

unread,
Oct 23, 2012, 4:02:06 AM10/23/12
to rufus...@googlegroups.com
Hi Join
thanks, I understood what your said, and figured it out. 
now i have  a new question from my program: 

how to re-scheduler the corn time?

eg. at beginning, 

job_routine_inspection=scheduler.cron '28:40:15 * * *' do
  puts 'activate security system'
end

every 5m , the program update the config file and then the program find  the cron time changed into '28:50:10 * * *'
how to affect it  at scheduler.cron ****?

many thanks. 

John Mettraux

unread,
Oct 23, 2012, 9:10:09 AM10/23/12
to rufus...@googlegroups.com
2012/10/23 edward <ajax...@gmail.com>:
>
> thanks, I understood what your said, and figured it out.
> now i have a new question from my program:
>
> how to re-scheduler the corn time?
>
> eg. at beginning,
>
> job_routine_inspection=scheduler.cron '28:40:15 * * *' do
> puts 'activate security system'
> end
>
> every 5m , the program update the config file and then the program find the
> cron time changed into '28:50:10 * * *'
> how to affect it at scheduler.cron ****?

Hello,

you can

a) kill/cancel the current cron job and schedule it again;
b) use "cron", the real unix utility, especially if you're running on
linux or another unix.

Best regards,

--
John Mettraux - http://lambda.io/jmettraux

edwar li

unread,
Oct 23, 2012, 11:08:36 PM10/23/12
to rufus...@googlegroups.com
ok, thanks.

2012/10/23 John Mettraux <jmet...@gmail.com>

--
you received this message because you are subscribed to the "rufus ruby" group.
to post : send email to rufus...@googlegroups.com
to unsubscribe : send email to rufus-ruby+...@googlegroups.com
more options : http://groups.google.com/group/rufus-ruby?hl=en

Reply all
Reply to author
Forward
0 new messages