lib/rocket_job/config.rb in rocketjob-4.3.0.beta2 vs lib/rocket_job/config.rb in rocketjob-5.0.0.beta

- old
+ new

@@ -1,54 +1,77 @@ require 'yaml' module RocketJob - # Centralized Configuration for Rocket Jobs + # Rocket Job Configuration class Config - include Plugins::Document + include SemanticLogger::Loggable - # Returns the single instance of the Rocket Job Configuration for this site - # in a thread-safe way - def self.instance - @instance ||= begin - first || create - rescue StandardError - # In case another process has already created the first document - first - end - end + # [String] This Rocket Job Server name + class_attribute :server_name + self.server_name = "#{SemanticLogger.host}:#{$$}" - # DEPRECATED - cattr_accessor(:inline_mode) { false } + # [Integer] The maximum number of workers to create on any one server + class_attribute :max_workers + self.max_workers = 10 - store_in collection: 'rocket_job.configs' + # [Integer] Number of seconds between heartbeats from a Rocket Job Server process + class_attribute :heartbeat_seconds + self.heartbeat_seconds = 15.0 + # [Integer] Maximum number of seconds a Worker will wait before checking for new jobs + class_attribute :max_poll_seconds + self.max_poll_seconds = 5.0 + + # [Integer] Number of seconds between checking for: + # - Jobs with a higher priority + # - If the current job has been paused, or aborted # - # Servers + # Making this interval too short results in too many checks for job status + # changes instead of focusing on completing the active tasks # + # Notes: + # - Not all job types support pausing in the middle + # + # Default: 60 seconds between checks. + class_attribute :re_check_seconds + self.re_check_seconds = 60.0 - # The maximum number of workers to create on any one server - field :max_workers, type: Integer, default: 10 + # [Regexp] Limit this server to only those job classes that match this regular expression. + # + # Note: + # - Supply a case insensitive Regexp if required. + # - Only supply include_filter or exclude_filter, not both. + # + # Example: + # # This server can only work on jobs that include anywhere + # # in their names: `DirmonJob` or `WeeklyReportJob` + # RocketJob::Config.include_filter = /DirmonJob|WeeklyReportJob/i + class_attribute :include_filter + self.include_filter = nil - # Number of seconds between heartbeats from a Rocket Job Server process - field :heartbeat_seconds, type: Integer, default: 15 - + # [Regexp] Prevent this server from working on any job classes that match this regular expression. # - # Workers + # Notes: + # - Supply a case insensitive Regexp if required. + # - Only supply include_filter or exclude_filter, not both. # + # Example: + # # This server can only work any job except that that include anywhere + # # in their names: `DirmonJob` or `WeeklyReportJob` + # RocketJob::Config.exclude_filter = /DirmonJob|WeeklyReportJob/i + class_attribute :exclude_filter + self.exclude_filter = nil - # Maximum number of seconds a Worker will wait before checking for new jobs - field :max_poll_seconds, type: Integer, default: 5 - - # Number of seconds between checking for: - # - Jobs with a higher priority - # - If the current job has been paused, or aborted + # [Hash] Limit this server instance to the supplied mongo query filter. # - # Making this interval too short results in too many checks for job status - # changes instead of focusing on completing the active tasks + # Notes: + # - Can be supplied together with `include_filter` or `exclude_filter` above. # - # Note: - # Not all job types support pausing in the middle - field :re_check_seconds, type: Integer, default: 60 + # Example: + # # This server can only work on jobs with priorities between 1 and 25 + # RocketJob::Config.where_filter = { "priority" => {"$lte" => 25}} + class_attribute :where_filter + self.where_filter = nil # Configure Mongoid def self.load!(environment = 'development', file_name = nil, encryption_file_name = nil) config_file = file_name ? Pathname.new(file_name) : Pathname.pwd.join('config/mongoid.yml') @@ -69,8 +92,22 @@ return unless config_file.file? logger.debug "Reading SymmetricEncryption configuration from: #{config_file}" SymmetricEncryption.load!(config_file.to_s, environment) + end + + # Returns [Hash] the where clause built from the filters above: + # include_filter, exclude_filter, and where_filter. + # Returns nil if no filter should be applied. + def self.filter + if include_filter && exclude_filter + raise(ArgumentError, 'Cannot supply both an include_filter and an exclude_filter') + end + + filter = where_filter + (filter ||= {})['_type'] = include_filter if include_filter + (filter ||= {})['_type'] = {'$not' => exclude_filter} if exclude_filter + filter end end end