0

I want to run a script which is generating some files and want check the existence of the script genrated file in every 3 min.

I did the following,

def periodic_method time, block
   t = EventMachine::PeriodicTimer.new(time) {eval(block)}
   begin
      yield
   ensure
      t.cancel
   end
end

periodic_method(60, "if File.exists(file1.txt) then puts 'done with step 1' else puts 'running generator'") do
    generator.rb
end

While running i am getting error wrong no of arguments.

Here is the stacktrace:-

wrong number of arguments (1 for 0)
/tools/simulation/simulation_assemble/test.rb:652:in `eval'
(eval):1:in `block in periodic_block'
/tools/simulation/simulation_assemble/test.rb:652:in `eval'
/tools/simulation/simulation_assemble/test.rb:652:in `block in periodic_block'
/tool/pandora64/.package/ruby-1.9.2-p136/lib/ruby/gems/1.9.1/gems/eventmachine-1.0.0.beta.4
/lib/em/timers.rb:52:in `call'
/tool/pandora64/.package/ruby-1.9.2-p136/lib/ruby/gems/1.9.1/gems/eventmachine-1.0.0.beta.4/lib/em/timers.rb:52:in `fire'
/tool/pandora64/.package/ruby-1.9.2-p136/lib/ruby/gems/1.9.1/gems/eventmachine-1.0.0.beta.4/lib/eventmachine.rb:206:in `call'
/tool/pandora64/.package/ruby-1.9.2-p136/lib/ruby/gems/1.9.1/gems/eventmachine-1.0.0.beta.4/lib/eventmachine.rb:206:in `run_machine'
/tool/pandora64/.package/ruby-1.9.2-p136/lib/ruby/gems/1.9.1/gems/eventmachine-1.0.0.beta.4/lib/eventmachine.rb:206:in `run'
generator.rb:195:in `block in run'
generator.rb:168:in `standard_exception_handling'
generator:191:in `run'
generator:28:in `< main>'

Can anyone help in achieving my task?

Is there any other way to do this task?

anamika
  • 7
  • 5

1 Answers1

0

I would use rufus-scheduler for that

require 'rufus-scheduler'

scheduler = Rufus::Scheduler.new

scheduler.every '3m' do
  # run script here; would this do?
  puts "running generator..." 
  `ruby generator.rb`
  puts "done generating..."
  # now when the script is done,
  # check if File.exists?
end

scheduler.every '3m' do
  # do other things in another thread
end

scheduler.join

sleep 1 while true # in order for your program to stay alive
Abdo
  • 13,549
  • 10
  • 79
  • 98