Permalink
Browse files

Added a way to detect if a process is running loops (+ added readme f…

…or that)
  • Loading branch information...
kovyrin committed May 18, 2012
1 parent b63ff31 commit 16aac4881f3e7c91e3a47d0867171901cabcdc6d
Showing with 31 additions and 0 deletions.
  1. +13 −0 README.rdoc
  2. +6 −0 lib/loops.rb
  3. +12 −0 lib/loops/cli/commands.rb
View
@@ -149,6 +149,7 @@ the loop class to execute:
Now two independent sets of loops are using the same class <tt>SomeModule::MyWorkerLoop</tt>
customized by the language parameter.
+
== How to initialize the loop before workers run?
You can run initialization code before starting loop workers by implementing the initialize_loop class method. If
@@ -168,6 +169,17 @@ initialize_loop raises an error, then the loop is not started and the error is l
end
end
+
+== How do I detect if my code is running in a loop?
+
+We provide an api call <tt>Loops.running?</tt> that returns <tt>true</tt> if your code is running as a part of a loops process.
+For example, if you want your Rails.logger to write logs into your Loops logger (when you run it in a loop), you could do something
+like the following in your environment files:
+
+ # Switch logger to loops if we are in a loops process
+ config.logger = Loops.logger if Loops.running?
+
+
== I want to keep my loop running on machine reboots. How to do it?
We use monit to keep loop monitors runnings. You could use something like this in your configs:
@@ -184,6 +196,7 @@ If you do not use bundler, you can do:
start program "/your/project/current/script/loops start slow_logs -e loops -p tmp/pids/loop-slow_logs.pid -d"
stop program "/your/project/current/script/loops stop slow_logs -e loops -p tmp/pids/loop-slow_logs.pid"
+
== ActiveMQ-based workers? What's that?
In some of our worker loops we poll ActiveMQ queue and process its items to perform some
View
@@ -163,6 +163,12 @@ def self.default_logger
def self.default_logger=(logger)
@@default_logger = logger
end
+
+ # Returns true if current process is started with loops CLI
+ #
+ def self.running?
+ Loops::CLI.running?
+ end
end
require File.join(Loops::LIB_ROOT, 'loops/autoload')
View
@@ -13,10 +13,22 @@ def self.included(base)
end
module ClassMethods
+ # We set this to true when a command is running
+ @@running = false
+
+ # Returns running status
+ #
+ def running?
+ @@running
+ end
+
# Parse arguments, find and execute command requested.
#
def execute
+ @@running = true
parse(ARGV).run!
+ ensure
+ @@running = false
end
# Register a Loops command.

0 comments on commit 16aac48

Please sign in to comment.