views:

78

answers:

1

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

I have tested it in a simple 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.

+1  A: 

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.

Tilendor
Thanks you, it works =). Some questions: 1. Is there a way to automatically load this task with rails application?2. I need to send data to pipe, when user clicks a link on my website (for example). I can use global variables, but i think its bad idea.
FancyDancy
There is not any way that I am aware of to start them at the same time without a batch script. Also, it seems weird to communicate from the webapp into the pipe. If the pipe task is operating on the database, you could skip that step in the controller and operate on the database directly. Or you could add something to the database that the task polls for.
Tilendor
For example, user clicks on link "write CD", then Rails send "write" to another application using named pipe. I can make a global variable and then send it, but is it a right way?
FancyDancy
I'm not sure the best way to do this, not having done anything like it. I'm fairly certain a global variable will not work because the web server and the rake task will be in different processes with no knowledge of each other.
Tilendor