Class: Rufio::TaskStatus

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

Overview

ジョブ(タスク)の状態を管理するクラスstatus: :waiting, :running, :completed, :failed, :cancelled

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(id:, name:, path:) ⇒ TaskStatus

初期化

Parameters:

  • id (Integer)

    タスクID

  • name (String)

    タスク名(スクリプト名)

  • path (String)

    実行ディレクトリ



13
14
15
16
17
18
19
20
21
22
# File 'lib/rufio/task_status.rb', line 13

def initialize(id:, name:, path:)
  @id = id
  @name = name
  @path = path
  @status = :waiting
  @start_time = nil
  @end_time = nil
  @logs = []
  @exit_code = nil
end

Instance Attribute Details

#end_timeObject

Returns the value of attribute end_time.



7
8
9
# File 'lib/rufio/task_status.rb', line 7

def end_time
  @end_time
end

#exit_codeObject

Returns the value of attribute exit_code.



7
8
9
# File 'lib/rufio/task_status.rb', line 7

def exit_code
  @exit_code
end

#idObject

Returns the value of attribute id.



7
8
9
# File 'lib/rufio/task_status.rb', line 7

def id
  @id
end

#logsObject

Returns the value of attribute logs.



7
8
9
# File 'lib/rufio/task_status.rb', line 7

def logs
  @logs
end

#nameObject

Returns the value of attribute name.



7
8
9
# File 'lib/rufio/task_status.rb', line 7

def name
  @name
end

#pathObject

Returns the value of attribute path.



7
8
9
# File 'lib/rufio/task_status.rb', line 7

def path
  @path
end

#start_timeObject

Returns the value of attribute start_time.



7
8
9
# File 'lib/rufio/task_status.rb', line 7

def start_time
  @start_time
end

#statusObject

Returns the value of attribute status.



7
8
9
# File 'lib/rufio/task_status.rb', line 7

def status
  @status
end

Instance Method Details

#append_log(line) ⇒ Object

ログを追加

Parameters:

  • line (String)

    ログ行



62
63
64
# File 'lib/rufio/task_status.rb', line 62

def append_log(line)
  @logs << line
end

#cancelObject

タスクをキャンセルする



47
48
49
50
# File 'lib/rufio/task_status.rb', line 47

def cancel
  @status = :cancelled
  @end_time = Time.now
end

#cancelled?Boolean

キャンセルされたかどうか

Returns:

  • (Boolean)


86
87
88
# File 'lib/rufio/task_status.rb', line 86

def cancelled?
  @status == :cancelled
end

#complete(exit_code:) ⇒ Object

タスクを完了する

Parameters:

  • exit_code (Integer)

    終了コード



32
33
34
35
36
# File 'lib/rufio/task_status.rb', line 32

def complete(exit_code:)
  @status = :completed
  @end_time = Time.now
  @exit_code = exit_code
end

#completed?Boolean

完了したかどうか

Returns:

  • (Boolean)


74
75
76
# File 'lib/rufio/task_status.rb', line 74

def completed?
  @status == :completed
end

#durationFloat?

実行時間を取得

Returns:

  • (Float, nil)

    秒単位の実行時間(未開始の場合はnil)



54
55
56
57
58
# File 'lib/rufio/task_status.rb', line 54

def duration
  return nil unless @start_time

  (@end_time || Time.now) - @start_time
end

#fail(exit_code:) ⇒ Object

タスクを失敗させる

Parameters:

  • exit_code (Integer)

    終了コード



40
41
42
43
44
# File 'lib/rufio/task_status.rb', line 40

def fail(exit_code:)
  @status = :failed
  @end_time = Time.now
  @exit_code = exit_code
end

#failed?Boolean

失敗したかどうか

Returns:

  • (Boolean)


80
81
82
# File 'lib/rufio/task_status.rb', line 80

def failed?
  @status == :failed
end

#formatted_durationString

フォーマット済みの実行時間を取得

Returns:

  • (String)

    “12.4s” 形式の文字列(未開始の場合は空文字列)



111
112
113
114
115
116
# File 'lib/rufio/task_status.rb', line 111

def formatted_duration
  d = duration
  return '' unless d

  format('%.1fs', d)
end

#running?Boolean

実行中かどうか

Returns:

  • (Boolean)


68
69
70
# File 'lib/rufio/task_status.rb', line 68

def running?
  @status == :running
end

#startObject

タスクを開始する



25
26
27
28
# File 'lib/rufio/task_status.rb', line 25

def start
  @status = :running
  @start_time = Time.now
end

#status_iconString

ステータスアイコンを取得

Returns:

  • (String)

    ステータスアイコン



92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
# File 'lib/rufio/task_status.rb', line 92

def status_icon
  case @status
  when :waiting
    ''
  when :running
    ''
  when :completed
    ''
  when :failed
    ''
  when :cancelled
    ''
  else
    '?'
  end
end