Class: Rufio::BackgroundCommandExecutor

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

Overview

バックグラウンドでシェルコマンドまたはRubyコードを実行するクラス

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(command_logger) ⇒ BackgroundCommandExecutor

初期化

Parameters:



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_loggerObject (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_typeSymbol?

コマンドタイプを取得

Returns:

  • (Symbol, nil)

    :shell または :ruby(実行中でない場合はnil)



146
147
148
# File 'lib/rufio/background_command_executor.rb', line 146

def command_type
  running? ? @command_type : nil
end

#current_commandString?

現在実行中のコマンド名を取得

Returns:

  • (String, nil)

    コマンド名(実行中でない場合はnil)



140
141
142
# File 'lib/rufio/background_command_executor.rb', line 140

def current_command
  running? ? @command : nil
end

#execute_async(command) ⇒ Boolean

シェルコマンドを非同期で実行

Parameters:

  • command (String)

    実行するコマンド

Returns:

  • (Boolean)

    実行を開始した場合はtrue、既に実行中の場合はfalse



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?

      error_message = success ? nil : stderr

      @command_logger.log(
        command,
        output,
        success: success,
        error: error_message
      )

      # 完了メッセージを生成
      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.message
      )

      command_name = extract_command_name(command)
      @completion_message = "#{command_name} エラー"
      @completed = true
    end
  end

  true
end

#execute_ruby_async(command_name, &block) ⇒ Boolean

Rubyコード(プラグインコマンド)を非同期で実行

Parameters:

  • command_name (String)

    コマンド名(表示用)

  • block (Proc)

    実行するコードブロック

Returns:

  • (Boolean)

    実行を開始した場合はtrue、既に実行中の場合はfalse



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.message
      )

      @completion_message = "#{command_name} エラー: #{e.message}"
      @completed = true
    end
  end

  true
end

#get_completion_messageString?

完了メッセージを取得

Returns:

  • (String, nil)

    完了メッセージ(完了していない場合はnil)



134
135
136
# File 'lib/rufio/background_command_executor.rb', line 134

def get_completion_message
  @completion_message
end

#running?Boolean

コマンドが実行中かどうか

Returns:

  • (Boolean)

    実行中の場合はtrue



128
129
130
# File 'lib/rufio/background_command_executor.rb', line 128

def running?
  @thread&.alive? || false
end