Class: Pindo::TaskSystem::ExecutionStrategy
- Inherits:
-
Object
- Object
- Pindo::TaskSystem::ExecutionStrategy
- Defined in:
- lib/pindo/module/task/core/execution_strategy.rb
Overview
ExecutionStrategy - 执行策略基类
定义所有执行策略必须实现的接口遵循策略模式(Strategy Pattern)
职责:
-
定义统一的策略接口
-
强制子类实现核心方法
-
提供策略工厂方法
-
支持里氏替换原则(LSP)
Direct Known Subclasses
Class Method Summary collapse
-
.create(mode, options = {}) ⇒ ExecutionStrategy
创建执行策略.
-
.detect_optimal_workers ⇒ Integer
检测最优工作线程数.
Instance Method Summary collapse
-
#execute(task_manager) ⇒ Object
执行任务(抽象方法,子类必须实现).
-
#name ⇒ String
策略名称(抽象方法,子类必须实现).
Class Method Details
.create(mode, options = {}) ⇒ ExecutionStrategy
创建执行策略
35 36 37 38 39 40 41 42 43 44 45 |
# File 'lib/pindo/module/task/core/execution_strategy.rb', line 35 def self.create(mode, = {}) case mode when :serial SerialExecutionStrategy.new when :concurrent max_workers = [:max_workers] || detect_optimal_workers ConcurrentExecutionStrategy.new(max_workers: max_workers) else raise ArgumentError, "Unknown execution mode: #{mode}" end end |
.detect_optimal_workers ⇒ Integer
检测最优工作线程数
49 50 51 52 53 54 |
# File 'lib/pindo/module/task/core/execution_strategy.rb', line 49 def self.detect_optimal_workers require 'etc' [Etc.nprocessors, 2].max rescue 4 end |
Instance Method Details
#execute(task_manager) ⇒ Object
执行任务(抽象方法,子类必须实现)
17 18 19 |
# File 'lib/pindo/module/task/core/execution_strategy.rb', line 17 def execute(task_manager) raise NotImplementedError, "#{self.class} must implement #execute" end |
#name ⇒ String
策略名称(抽象方法,子类必须实现)
24 25 26 |
# File 'lib/pindo/module/task/core/execution_strategy.rb', line 24 def name raise NotImplementedError, "#{self.class} must implement #name" end |