Class: Rufio::JobManager
- Inherits:
-
Object
- Object
- Rufio::JobManager
- Defined in:
- lib/rufio/job_manager.rb
Overview
複数のジョブを管理するクラスジョブの追加、状態追跡、通知連携を行う
Instance Attribute Summary collapse
-
#jobs ⇒ Object
readonly
Returns the value of attribute jobs.
Instance Method Summary collapse
-
#add_job(name:, path:, command:) ⇒ TaskStatus
ジョブを追加.
-
#any_running? ⇒ Boolean
実行中のジョブがあるかどうか.
-
#cancel_job(id) ⇒ Boolean
ジョブをキャンセル.
-
#clear_completed ⇒ Object
完了したジョブをクリア.
-
#completed_count ⇒ Integer
完了したジョブ数.
-
#failed_count ⇒ Integer
失敗したジョブ数.
-
#find_job(id) ⇒ TaskStatus?
IDでジョブを検索.
-
#has_jobs? ⇒ Boolean
ジョブがあるかどうか.
-
#initialize(notification_manager: nil) ⇒ JobManager
constructor
A new instance of JobManager.
-
#job_count ⇒ Integer
ジョブ数を取得.
-
#notify_completion(job) ⇒ Object
ジョブ完了時の通知を送信(NotificationManagerと連携).
-
#running_count ⇒ Integer
実行中のジョブ数.
-
#status_bar_text ⇒ String
ステータスバー用のテキストを生成.
-
#status_summary ⇒ Hash
ステータスサマリーを取得.
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
#jobs ⇒ Object (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
ジョブを追加
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
実行中のジョブがあるかどうか
110 111 112 |
# File 'lib/rufio/job_manager.rb', line 110 def any_running? @jobs.any?(&:running?) end |
#cancel_job(id) ⇒ 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_completed ⇒ Object
完了したジョブをクリア
91 92 93 |
# File 'lib/rufio/job_manager.rb', line 91 def clear_completed @jobs.reject! { |job| job.completed? || job.cancelled? } end |
#completed_count ⇒ Integer
完了したジョブ数
58 59 60 |
# File 'lib/rufio/job_manager.rb', line 58 def completed_count @jobs.count(&:completed?) end |
#failed_count ⇒ Integer
失敗したジョブ数
64 65 66 |
# File 'lib/rufio/job_manager.rb', line 64 def failed_count @jobs.count(&:failed?) end |
#find_job(id) ⇒ TaskStatus?
IDでジョブを検索
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
ジョブがあるかどうか
104 105 106 |
# File 'lib/rufio/job_manager.rb', line 104 def has_jobs? !@jobs.empty? end |
#job_count ⇒ Integer
ジョブ数を取得
46 47 48 |
# File 'lib/rufio/job_manager.rb', line 46 def job_count @jobs.size end |
#notify_completion(job) ⇒ Object
ジョブ完了時の通知を送信(NotificationManagerと連携)
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_count ⇒ Integer
実行中のジョブ数
52 53 54 |
# File 'lib/rufio/job_manager.rb', line 52 def running_count @jobs.count(&:running?) end |
#status_bar_text ⇒ String
ステータスバー用のテキストを生成
97 98 99 100 |
# File 'lib/rufio/job_manager.rb', line 97 def summary = status_summary "#{summary[:total]} jobs: #{summary[:running]} running, #{summary[:done]} done" end |
#status_summary ⇒ Hash
ステータスサマリーを取得
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 |