Class: Rufio::BackgroundCommandExecutor
- Inherits:
-
Object
- Object
- Rufio::BackgroundCommandExecutor
- Defined in:
- lib/rufio/background_command_executor.rb
Overview
バックグラウンドでシェルコマンドまたはRubyコードを実行するクラス
Instance Attribute Summary collapse
-
#command_logger ⇒ Object
readonly
Returns the value of attribute command_logger.
Instance Method Summary collapse
-
#command_type ⇒ Symbol?
コマンドタイプを取得.
-
#current_command ⇒ String?
現在実行中のコマンド名を取得.
-
#execute_async(command) ⇒ Boolean
シェルコマンドを非同期で実行.
-
#execute_ruby_async(command_name, &block) ⇒ Boolean
Rubyコード(プラグインコマンド)を非同期で実行.
-
#get_completion_message ⇒ String?
完了メッセージを取得.
-
#initialize(command_logger) ⇒ BackgroundCommandExecutor
constructor
初期化.
-
#running? ⇒ Boolean
コマンドが実行中かどうか.
Constructor Details
#initialize(command_logger) ⇒ BackgroundCommandExecutor
初期化
12 13 14 15 16 17 18 19 |
# File 'lib/rufio/background_command_executor.rb', line 12 def initialize(command_logger) @command_logger = command_logger @thread = nil @command = nil @command_type = nil # :shell または :ruby @completed = false @completion_message = nil end |
Instance Attribute Details
#command_logger ⇒ Object (readonly)
Returns the value of attribute command_logger.
8 9 10 |
# File 'lib/rufio/background_command_executor.rb', line 8 def command_logger @command_logger end |
Instance Method Details
#command_type ⇒ Symbol?
コマンドタイプを取得
146 147 148 |
# File 'lib/rufio/background_command_executor.rb', line 146 def command_type running? ? @command_type : nil end |
#current_command ⇒ String?
現在実行中のコマンド名を取得
140 141 142 |
# File 'lib/rufio/background_command_executor.rb', line 140 def current_command running? ? @command : nil end |
#execute_async(command) ⇒ Boolean
シェルコマンドを非同期で実行
24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 |
# File 'lib/rufio/background_command_executor.rb', line 24 def execute_async(command) # 既に実行中の場合は新しいコマンドを開始しない return false if running? @command = command @command_type = :shell @completed = false @completion_message = nil @thread = Thread.new do begin # コマンドを実行 stdout, stderr, status = Open3.capture3(command) # 結果をログに保存 output = stdout + stderr success = status.success? = success ? nil : stderr @command_logger.log( command, output, success: success, error: ) # 完了メッセージを生成 command_name = extract_command_name(command) if success @completion_message = "✓ #{command_name} 完了" else @completion_message = "✗ #{command_name} 失敗" end @completed = true rescue StandardError => e # エラーが発生した場合もログに記録 @command_logger.log( command, "", success: false, error: e. ) command_name = extract_command_name(command) @completion_message = "✗ #{command_name} エラー" @completed = true end end true end |
#execute_ruby_async(command_name, &block) ⇒ Boolean
Rubyコード(プラグインコマンド)を非同期で実行
82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 |
# File 'lib/rufio/background_command_executor.rb', line 82 def execute_ruby_async(command_name, &block) # 既に実行中の場合は新しいコマンドを開始しない return false if running? @command = command_name @command_type = :ruby @completed = false @completion_message = nil @thread = Thread.new do begin # Rubyコードを実行 result = block.call # 結果をログに保存 output = result.to_s @command_logger.log( command_name, output, success: true, error: nil ) # 完了メッセージを生成 @completion_message = "✓ #{command_name} 完了" @completed = true rescue StandardError => e # エラーが発生した場合もログに記録 @command_logger.log( command_name, "", success: false, error: e. ) @completion_message = "✗ #{command_name} エラー: #{e.}" @completed = true end end true end |
#get_completion_message ⇒ String?
完了メッセージを取得
134 135 136 |
# File 'lib/rufio/background_command_executor.rb', line 134 def @completion_message end |
#running? ⇒ Boolean
コマンドが実行中かどうか
128 129 130 |
# File 'lib/rufio/background_command_executor.rb', line 128 def running? @thread&.alive? || false end |