开发者

running ruby on rails function without opening the browser

Hi

I need to build an ruby on rails application which will call every 1 second to a remote Api url, in order to catch an event that should be occurred in that remote web server the url is

"remoteservername.com/folder/folder/waitforevents"

once the wanted event is occurred I need to perform some ac开发者_如何学运维tions and call again to the "remoteservername.com/folder/folder/waitforevents" url.

This should be a continuously task and done without the opening of a browser. It means that this application should start running immediately after my web server is started.

so my question is how can I run a ruby on rails application without opening the browser just the server

until now I had experience in writing rails applications which started when the browser was opened and some url was invoked. so I will be glad if you could guide me on this issue

THANKS


Are you sure you need rails for that kind of application? I suggest you try doing the job with raw ruby code, easier to maintain. As others already said, you can use cron to launch your application at regular intervals.


You may use cron together with ./script/rails runner, which can execute any method from your rails application.

Since you need to execute some action every second, you may start that runner once, and create a loop. You may start it from the cron, save the PID somewhere and later ask the cron to kill given process, OR you may just timeout in your Ruby code, in the loop. A simple example:

class MyModelOrAnything
  def self.start_loop
    stop_fname = "./stop_the_loop"
    while !File.exist? stop_fname 
      do_this_action
      sleep 1
    end
  ensure
    File.unlink stop_fname  if File.exist? stop_fname
  end
end

A cron task may then create a file "stop_the_loop" in known directory, and your task will stop after a second.

In fact, cron may not be necessary in this case. Since you should have only one instance of this script, it would be better to start it manually, and stop it manually. If you want to use cron, you could add a semaphore-like file, which will tell the script that one process is already running (The file should be removed by the 'ensure' block).

An example of the cron config:

0 8  * * 1 cd /my/app && bundle exec ./script/rails runner ThisObject.start_loop
0 22 * * 5 touch /my/app/stop_the_loop

..and an example with this 'semaphore':

class MyModelOrAnything
  def self.start_loop
    running_fname = "./i_am_working"
    return :already_running if File.exist? running_fname
    stop_fname = "./stop_the_loop"
    begin
      File.open(running_fname,'w') {|f| f.write Time.now.to_s(:db) }
      while !File.exist? stop_fname 
        do_this_action
        sleep 1
      end
    ensure
      File.unlink running_fname if File.exist? running_fname
      File.unlink stop_fname  if File.exist? stop_fname
    end
  end
end


A good way is to use delayed job:

https://github.com/collectiveidea/delayed_job

There is a railscast on the topic which may help:

http://railscasts.com/episodes/171-delayed-job

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜