开发者

Using named pipes in Rails

I need to read and write some data through named pipes.

I have tested it in a simpl开发者_如何转开发e Ruby app, and it works nice.

But I dont know, where should i put it in my Rails app? I have heard about Rake tasks, but i don't sure, is it right solution.

I need to open a pipe-file, and listen to data. If there is any, i need to process it and make a DB-query. Then, write some data to another pipe. I know, how it works, but the only problem - how to run it with Rails? Give me some examples, please.


It sounds like you have a website that will have a backend source of data that you are streaming through a pipe. It also sounds like this is not going to be part of the HTTP Request/Response cycle, which could make a rake task a good choice.

Make a file in lib/tasks called listener.rake

it should look like this:

desc 'Listens to pipe and does stuff'
task :listen_to_pipe => :environment do
  puts "Listen to Pipe starting"
  #open pipe
  #loop to listen to it
    puts "going to do stuff"
    #do stuff
  #end
end

Then, from the command line in the root dir of your project you can invoke it like this:

rake listen_to_pipe

and for a different environment, do this:

rake listen_to_pipe RAILS_ENV=production

This task will have access to all your models. To end it, hit Ctrl+C

Keep in mind you will need to stop & restart the process to load any changes made to models.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜