lib/rocket_job/cli.rb in rocketjob-3.5.2 vs lib/rocket_job/cli.rb in rocketjob-4.0.0

- old
+ new

@@ -1,17 +1,18 @@ require 'optparse' +require 'json' require 'semantic_logger' require 'mongoid' require 'rocketjob' -require 'rocket_job/extensions/mongoid/factory' +require 'pathname' module RocketJob # Command Line Interface parser for Rocket Job class CLI include SemanticLogger::Loggable attr_accessor :name, :workers, :environment, :pidfile, :directory, :quiet, :log_level, :log_file, :mongo_config, :symmetric_encryption_config, - :filter + :include_filter, :exclude_filter, :where_filter def initialize(argv) @name = nil @workers = nil @quiet = false @@ -20,11 +21,12 @@ @directory = '.' @log_level = nil @log_file = nil @mongo_config = nil @symmetric_encryption_config = nil - @filter = nil + @include_filter = nil + @exclude_filter = nil parse(argv) end # Run a RocketJob::Server from the command line def run @@ -36,14 +38,17 @@ write_pidfile # In case Rails did not load the Mongoid Config RocketJob::Config.load!(environment, mongo_config, symmetric_encryption_config) if ::Mongoid::Config.clients.empty? + filter = build_filter + opts = {} opts[:name] = name if name opts[:max_workers] = workers if workers - opts[:filter] = {_type: filter} if filter + opts[:filter] = filter if filter + Server.run(opts) end def rails? @rails ||= begin @@ -89,11 +94,11 @@ nil end require 'rocketjob' begin - require 'rocketjob_pro' + require 'rocketjob_batch' rescue LoadError nil end # Log to file except when booting rails, when it will add the log file path @@ -146,10 +151,21 @@ logger.debug "Loading #{path}" require path.expand_path.to_s end end + # Returns [Hash] a where clause filter to apply to this server. + # Returns nil if no filter should be applied + def build_filter + raise(ArgumentError, 'Cannot supply both a filter and an exclusion filter') if include_filter && exclude_filter + + filter = where_filter + (filter ||= {})['_type'] = include_filter if include_filter + (filter ||= {})['_type'] = {'$not' => exclude_filter} if exclude_filter + filter + end + # Parse command line options placing results in the corresponding instance variables def parse(argv) parser = OptionParser.new do |o| o.on('-n', '--name NAME', 'Unique Name of this server (Default: host_name:PID)') do |arg| @name = arg @@ -159,11 +175,17 @@ end o.on('-t', '--threads COUNT', 'DEPRECATED') do |arg| warn '-t and --threads are deprecated, use -w or --workers' @workers = arg.to_i end - o.on('-F', '--filter REGEXP', 'Limit this worker to only those job classes that match this regular expression (case-insensitive). Example: "DirmonJob|WeeklyReportJob"') do |arg| - @filter = Regexp.new(arg, true) + o.on('-F', '--filter REGEXP', 'Limit this server to only those job classes that match this regular expression (case-insensitive). Example: "DirmonJob|WeeklyReportJob"') do |arg| + @include_filter = Regexp.new(arg, true) + end + o.on('-E', '--exclude REGEXP', 'Prevent this server from working on any job classes that match this regular expression (case-insensitive). Example: "DirmonJob|WeeklyReportJob"') do |arg| + @exclude_filter = Regexp.new(arg, true) + end + o.on('-W', '--where JSON', "Limit this server instance to the supplied mongo query filter. Supply as a string in JSON format. Example: '{\"priority\":{\"$lte\":25}}'") do |arg| + @where_filter = JSON.parse(arg) end o.on('-q', '--quiet', 'Do not write to stdout, only to logfile. Necessary when running as a daemon') do @quiet = true end o.on('-d', '--dir DIR', 'Directory containing Rails app, if not current directory') do |arg|