Rails + XMPP bot in the background

I create a service that basically allows users to communicate with the bot, and the bot then does some weird processing with the chat sent by the user, and ultimately returns response messages with some meaningful data. Basically something similar to how Aardvark used (?) To work.

My bot works for me, and now I'm listening, and I have a separate application for rails, which will do all the other hard work. Both of these parts work fine individually, and now I'm stuck in interacting with them. My idea is to associate the bot (which is basically a small ruby ​​script) with the rails application via Resque - everything that comes into the queue goes into the queue, receives the message, and the results then go back into the queue and the script will respond with the results.

I do not quite understand how to install this interface:

  • Do I need to write a rake task to start / stop / reboot the bot.
  • If I started it without a rake (presumably as an independent process controlled by Monit), then how can I interact with Resque or gain access to rail models?

I know that these can be very trivial questions, but it’s hard for me to understand what works better and how to configure the setting.

+5
source share
3 answers

There are three ways to communicate between your Rails application and this bot daemon:

  • Call the Rails application as an HTTP request (by clicking / pulling data from the Rails application)
  • When interacting directly with the database, the Rails application uses (probably Mysql / Postgres)
  • Interacting with a Resque Queue System Supported by a Redis Database

When you start and pull Resque jobs from different job queues, you just read / write to the common Redis database via the API. Both bots and the Rails application talk to Redis DB over the network.

-, monit. , , .

+4

, , (IPC-like, IM), Resque, "" . : amqp gem (AMQP-) zmq gem ( ZeroMQ), UNIX- Ruby. Socket ( ). , , , .

:

  • .
  • IPC.
  • ( XMPP).
  • Resque.
  • Rails HTTP.
  • Rails .
  • - - , , Rails.
  • Rails , IPC .
  • ( XMPP).

, . , , Resque. Rails, , , , .. , Resque , Rails , ( Rails) IPC. ...

//

, . , . , Rake Ruby . , - , ( , ..), Rake . , Rake . , , , script ( monit, init.d script, ad-hoc ..).

rake (, , Monit), Resque ?

. , Resque Rake Rake script, , . , Resque , Redis -.

, .

. , , .

+2

Rails- .

, "" Rails-, Rails.

:

//background_app_tasks.rb

class BackgroundWorker

      #-------------------------------
      def initialize(operation='normal')
        @exit = false
        @lock = Mutex.new  # For thread safety
        @thread = nil
        say "Starting in '#{operation}' mode..."
        case operation
          when 'normal'
            @thread = Thread.new() {    loopme     }
          when 'cleanup'
            @thread = Thread.new() {    cleanup     }
          when 'nothing'
            #startup without threads
        end
        @thread.run if @thread
      end

      #-------------------------------
      def exit!
        begin
            return if @exit # #stop?
            say   "Exiting #{}, waiting for mutex..."
            @lock.synchronize {
                say "exiting thread #{@thread.to_s || '<sem nome>' }..."
                @exit = true # #stop
            }
        rescue Exception => e
            exceptme(e)
        end
      end

      #-------------------------------
      def loopme

        at_exit { exit! }
        i=0;  ok=false;

        nap = 30

        while true do
          begin
              break if @exit
              i+=1

              #lock mutex for processing...
              @lock.synchronize {

                  #.... do some work ....

              }
          rescue StandardError => e

              #....

          end

          sleep(nap)
        end 
      end

end #class

# ------ M A I N --------

Thread.abort_on_exception=false
e = BackgroundWorker.new(OPERATION)
0

All Articles