Class: Sidekiq::Tasks::Strategies::Base
- Inherits:
-
Object
- Object
- Sidekiq::Tasks::Strategies::Base
- Includes:
- Validations
- Defined in:
- lib/sidekiq/tasks/strategies/base.rb
Direct Known Subclasses
Instance Attribute Summary collapse
-
#rules ⇒ Array<Sidekiq::Tasks::Strategies::Rules::Base>
readonly
A set of rules to fetch tasks.
Instance Method Summary collapse
-
#build_task_metadata(_task) ⇒ Sidekiq::Tasks::TaskMetadata
abstract
Factory method to build the metadata for a task.
-
#enqueue_task(name, params = {}) ⇒ String
Enqueues a task with the given parameters and returns the JID.
-
#execute_task(_name, _args = nil) ⇒ Object
Executes a task with the given parameters.
-
#initialize(rules: []) ⇒ Base
constructor
Initializes a strategy with the given rules.
-
#load_tasks ⇒ Array
abstract
Returns all the raw tasks that should be filtered.
-
#name ⇒ String
Returns the name of the strategy.
-
#tasks ⇒ Array<Sidekiq::Tasks::Task>
Returns all the tasks that should be executed.
Methods included from Validations
validate_array_classes!, validate_class!, validate_expected_values!, validate_hash_option!
Constructor Details
Instance Attribute Details
#rules ⇒ Array<Sidekiq::Tasks::Strategies::Rules::Base> (readonly)
A set of rules to fetch tasks.
11 12 13 |
# File 'lib/sidekiq/tasks/strategies/base.rb', line 11 def rules @rules end |
Instance Method Details
#build_task_metadata(_task) ⇒ Sidekiq::Tasks::TaskMetadata
Subclasses must implement this method.
Factory method to build the metadata for a task.
73 74 75 |
# File 'lib/sidekiq/tasks/strategies/base.rb', line 73 def (_task) raise NotImplementedError, "Strategy must implement #build_task_metadata" end |
#enqueue_task(name, params = {}) ⇒ String
Enqueues a task with the given parameters and returns the JID.
55 56 57 |
# File 'lib/sidekiq/tasks/strategies/base.rb', line 55 def enqueue_task(name, params = {}) Sidekiq::Tasks::Job.perform_async(name, params.to_json) end |
#execute_task(_name, _args = nil) ⇒ Object
Consider accepting a ‘Sidekiq::Tasks::Task` instead of a task name.
Executes a task with the given parameters.
46 47 48 |
# File 'lib/sidekiq/tasks/strategies/base.rb', line 46 def execute_task(_name, _args = nil) raise NotImplementedError, "Strategy must implement #execute_task" end |
#load_tasks ⇒ Array
Subclasses must implement this method.
Returns all the raw tasks that should be filtered.
35 36 37 |
# File 'lib/sidekiq/tasks/strategies/base.rb', line 35 def load_tasks raise NotImplementedError, "Strategy must implement #load_tasks" end |
#name ⇒ String
Returns the name of the strategy.
26 27 28 |
# File 'lib/sidekiq/tasks/strategies/base.rb', line 26 def name self.class.name.split("::").last end |
#tasks ⇒ Array<Sidekiq::Tasks::Task>
Returns all the tasks that should be executed.
62 63 64 65 66 |
# File 'lib/sidekiq/tasks/strategies/base.rb', line 62 def tasks filtered_tasks = load_tasks.select { |task| respects_rules?(task) } filtered_tasks.map { |task| Sidekiq::Tasks::Task.new(metadata: (task), strategy: self) } end |