I'm building a service which basically allows users to chat with a bot, and the bot then does some strange processing with the chat sent by the user, and eventually reply back with some meaningful data. Basically something similar to how Aardvark used (?) to work.
I've the bot working, and listening right now, and I have a separate rails app that will be doing all the other heavy lifting. Both these parts are working fine individually, and now I'm stuck at interfacing the two. My idea is to interface the bot (which is basically a small ruby script) with the rails app via Resque - anything that comes in goes to a queue, gets picked up, and the results then pushed back again to the queue, and then the script would reply back with the results.
I'm not very clear as to how to establish this interface:
I know these might be very trivial questions, but I'm having a hard time understanding which works better, and how to get the setup going.
There are three ways to communicate between your Rails app and this bot daemon:
When you are enqueuing and pulling Resque jobs off various Job queues, you're just reading/writing to the shared Redis database through an API. Both the bot and the Rails app talk to the Redis DB over the network.
I recommend running the bot directly as a ruby process or rake task managed by monit. It sounds like you already know how to do this.
I think the main issue here is that you need another solution for messaging (IPC-like, not IM) rather than trying to bend Resque which is “just” a queue. Some of the options are amqp gem (AMQP protocol) or zmq gem (ZeroMQ protocol), but you can also use plain old UNIX sockets via Ruby standard library Socket class (good examples). They all have different pros and cons, so it’s up to you and your needs.
The interaction might look like something like this:
There can be some changes as usual. For example, I think you don’t need Resque at all. The bot can simply pass the request immediately to the Rails app. However, it depends on load, time to respond you want to achieve, you current architecture, etc. Maybe the Resque job can wait for the Rails app to return the result and then the job (not the Rails app) would use IPC. There are other variations…
Do I need to write a rake task to start / stop / reload the bot
No, you don’t. It is up to you how and when you run it. After all, Rake can be viewed just as a convenient way to put multiple Ruby scripts together and create dependencies between them. If you think there will be other tasks around the bot than just running it (some cleaning up, deployment, etc.), it will be good to use Rake for convenience. If you haven’t already, refactor bot’s logic to class and use Rake task to initialize it. But it will also be fine if you leave it, and just run your script as-is (using monit, your custom init.d script, ad-hoc, etc.).
If I run it without rake (supposedly as an independent process monitored by Monit) then how do I interface with Resque or access my rails models ?
Rake doesn’t have any effect on this. It doesn’t matter from OS perspective if you run Resque via Rake and your bot via Rake or as a standalone script. They will be different processes anyway. Also, keep in mind that Resque needs Redis to be running somewhere.
I know these might be very trivial questions
No at all. I think it’ll take some time before issues like could be considered trivial.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With