So, i’m building a small application using Rails. I need to use XMPP proto for notification of users with a bit of commands to change user status.
Previous version was an application using plain Python(pretty ugly app) with plain SQL request into DB. There was a table "jabber_queue", so in separated script, bot was checking each second for any rows, processing it(sending message), deleting. It was simple and stupid, but it was ok.
Now i see, i need to integrate this bot functionality into Rails(at least to work with RSpec).
This is my few versions of how it can be done:
-
Use separated async queue solution. For example, Resque. Use separated Ruby script and push events into Resque, pop events in application(not dependent on DB, so easily fit with RSpec tests and test DB). But it makes my application a bit bloated – i need to use second DB with a lot of memory and CPU additional requirements – it will be overhead for my problem. Also, i don’t want to support additional "thing" for this application, i know, it can be done a lot easier way.
-
Use delayed_job(queue solution using current AR DB). But i don’t know how to get current AR DB of Rails application in separated script. Anyway, it’s a dirty and ugly way.
-
Launch XMPP bot WITHIN Rails application, as background worker. So worker will get access to "current" AR(in case of testing, to test-DB). But i simply don’t know how to do it. I’ve found a Navvy, but i need to put somewhere at Rails start a string like a "Navvy::Job.enqueue(Cow, :speak)", i don’t know where will be best place for this, to start it with RSpec testing and "rails server". Also, there is a BackgrounDRb, but this project is similar to Navvy and inactive too. Using search on stackoverflow, i’ve found similar problem to mine, but solution leads me to background_job, which can just anything in background, but i still don’t know how to get current AR DB access in separated script.
I’m so sorry for this amount of words in my problem, it’s just a brainstream. I see some solutions, but i really need advices and some words from more experienced developers.
So, this was solved using yes, third way.
I’ve created a class for dealing with bot commands and AR models –
awesomo.rb. Nothing special, really. I put this in/lib/of Rails project. Secondly,Created configuration file for easy setting of password and JID –
config/awesomo.ymlI’ve created a daemon for my bot –
awesomo_daemon.rb. Same, in/lib/. This is what it contains:Created daemon script starter-
script/awesomo.Simply run it with command
script/awesomo start.And I can use any my models within
awesomo.rb! Also, for queue, i’m using little model – XMPPJob with fields jtype(for example, “xmpp_message”), body(“hey!”), to(“john@jabber.com”). Fetching it withinawesomo.rbidlefunction of bot class withlimit(5).each do |job| case jtype ....To post any new job for my “awesomo”, i’m using function
send_message:Everything works perfectly, except XMPP library(
xmpp4r-simple) itself, but I’ll rewrite it soon using justxmpp4r.