Class: Pindo::TaskSystem::ExecutionStrategy

Inherits:
Object
  • Object
show all
Defined in:
lib/pindo/module/task/core/execution_strategy.rb

Overview

ExecutionStrategy - 执行策略基类

定义所有执行策略必须实现的接口遵循策略模式(Strategy Pattern)

职责:

  • 定义统一的策略接口

  • 强制子类实现核心方法

  • 提供策略工厂方法

  • 支持里氏替换原则(LSP)

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.create(mode, options = {}) ⇒ ExecutionStrategy

创建执行策略

Parameters:

  • mode (Symbol)

    执行模式 (:serial, :concurrent)

  • options (Hash) (defaults to: {})

    选项

Options Hash (options):

  • :max_workers (Integer)

    最大工作线程数(并发模式)

Returns:



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, options = {})
  case mode
  when :serial
    SerialExecutionStrategy.new
  when :concurrent
    max_workers = options[:max_workers] || detect_optimal_workers
    ConcurrentExecutionStrategy.new(max_workers: max_workers)
  else
    raise ArgumentError, "Unknown execution mode: #{mode}"
  end
end

.detect_optimal_workersInteger

检测最优工作线程数

Returns:

  • (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

执行任务(抽象方法,子类必须实现)

Parameters:

Raises:

  • (NotImplementedError)

    如果子类未实现此方法



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

#nameString

策略名称(抽象方法,子类必须实现)

Returns:

  • (String)

    策略的显示名称

Raises:

  • (NotImplementedError)

    如果子类未实现此方法



24
25
26
# File 'lib/pindo/module/task/core/execution_strategy.rb', line 24

def name
  raise NotImplementedError, "#{self.class} must implement #name"
end