Class: Rufio::JobManager

Inherits:
Object
  • Object
show all
Defined in:
lib/rufio/job_manager.rb

Overview

複数のジョブを管理するクラスジョブの追加、状態追跡、通知連携を行う

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(notification_manager: nil) ⇒ JobManager

Returns a new instance of JobManager.



11
12
13
14
15
# File 'lib/rufio/job_manager.rb', line 11

def initialize(notification_manager: nil)
  @jobs = []
  @notification_manager = notification_manager
  @next_id = 1
end

Instance Attribute Details

#jobsObject (readonly)

Returns the value of attribute jobs.



9
10
11
# File 'lib/rufio/job_manager.rb', line 9

def jobs
  @jobs
end

Instance Method Details

#add_job(name:, path:, command:) ⇒ TaskStatus

ジョブを追加

Parameters:

  • name (String)

    ジョブ名

  • path (String)

    実行ディレクトリ

  • command (String)

    実行コマンド

Returns:



22
23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/rufio/job_manager.rb', line 22

def add_job(name:, path:, command:)
  job = TaskStatus.new(
    id: @next_id,
    name: name,
    path: path
  )
  # commandを保存(TaskStatusに追加)
  job.instance_variable_set(:@command, command)
  job.define_singleton_method(:command) { @command }

  @jobs << job
  @next_id += 1
  job
end

#any_running?Boolean

実行中のジョブがあるかどうか

Returns:

  • (Boolean)


110
111
112
# File 'lib/rufio/job_manager.rb', line 110

def any_running?
  @jobs.any?(&:running?)
end

#cancel_job(id) ⇒ Boolean

ジョブをキャンセル

Parameters:

  • id (Integer)

    ジョブID

Returns:

  • (Boolean)

    キャンセル成功かどうか



82
83
84
85
86
87
88
# File 'lib/rufio/job_manager.rb', line 82

def cancel_job(id)
  job = find_job(id)
  return false unless job

  job.cancel
  true
end

#clear_completedObject

完了したジョブをクリア



91
92
93
# File 'lib/rufio/job_manager.rb', line 91

def clear_completed
  @jobs.reject! { |job| job.completed? || job.cancelled? }
end

#completed_countInteger

完了したジョブ数

Returns:

  • (Integer)


58
59
60
# File 'lib/rufio/job_manager.rb', line 58

def completed_count
  @jobs.count(&:completed?)
end

#failed_countInteger

失敗したジョブ数

Returns:

  • (Integer)


64
65
66
# File 'lib/rufio/job_manager.rb', line 64

def failed_count
  @jobs.count(&:failed?)
end

#find_job(id) ⇒ TaskStatus?

IDでジョブを検索

Parameters:

  • id (Integer)

    ジョブID

Returns:



40
41
42
# File 'lib/rufio/job_manager.rb', line 40

def find_job(id)
  @jobs.find { |job| job.id == id }
end

#has_jobs?Boolean

ジョブがあるかどうか

Returns:

  • (Boolean)


104
105
106
# File 'lib/rufio/job_manager.rb', line 104

def has_jobs?
  !@jobs.empty?
end

#job_countInteger

ジョブ数を取得

Returns:

  • (Integer)


46
47
48
# File 'lib/rufio/job_manager.rb', line 46

def job_count
  @jobs.size
end

#notify_completion(job) ⇒ Object

ジョブ完了時の通知を送信(NotificationManagerと連携)

Parameters:



116
117
118
119
120
121
122
123
124
125
126
# File 'lib/rufio/job_manager.rb', line 116

def notify_completion(job)
  return unless @notification_manager

  type = job.completed? ? :success : :error
  @notification_manager.add(
    job.name,
    type,
    duration: job.duration || 0,
    exit_code: job.exit_code
  )
end

#running_countInteger

実行中のジョブ数

Returns:

  • (Integer)


52
53
54
# File 'lib/rufio/job_manager.rb', line 52

def running_count
  @jobs.count(&:running?)
end

#status_bar_textString

ステータスバー用のテキストを生成

Returns:

  • (String)

    “3 jobs: 2 running, 1 done” のような形式



97
98
99
100
# File 'lib/rufio/job_manager.rb', line 97

def status_bar_text
  summary = status_summary
  "#{summary[:total]} jobs: #{summary[:running]} running, #{summary[:done]} done"
end

#status_summaryHash

ステータスサマリーを取得

Returns:

  • (Hash)

    { total:, running:, done:, failed: }



70
71
72
73
74
75
76
77
# File 'lib/rufio/job_manager.rb', line 70

def status_summary
  {
    total: @jobs.size,
    running: running_count,
    done: completed_count,
    failed: failed_count
  }
end