Class: DeployPin::Task

Inherits:
Object
  • Object
show all
Extended by:
ParallelWrapper
Includes:
ParallelWrapper
Defined in:
lib/deploy_pin/task.rb

Constant Summary

Constants included from ParallelWrapper

ParallelWrapper::PARALLEL_PREFIX

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from ParallelWrapper

method_missing, respond_to_missing?

Constructor Details

#initialize(file) ⇒ Task

Returns a new instance of Task.



17
18
19
20
21
22
23
24
25
# File 'lib/deploy_pin/task.rb', line 17

def initialize(file)
  @file = file
  @identifier = nil
  @group = nil
  @title = ''
  @script = ''
  @explicit_timeout = false
  @parallel = false
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method in the class DeployPin::ParallelWrapper

Instance Attribute Details

#explicit_timeoutObject (readonly)

Returns the value of attribute explicit_timeout.



9
10
11
# File 'lib/deploy_pin/task.rb', line 9

def explicit_timeout
  @explicit_timeout
end

#fileObject (readonly)

Returns the value of attribute file.



9
10
11
# File 'lib/deploy_pin/task.rb', line 9

def file
  @file
end

#groupObject (readonly)

Returns the value of attribute group.



9
10
11
# File 'lib/deploy_pin/task.rb', line 9

def group
  @group
end

#identifierObject (readonly)

Returns the value of attribute identifier.



9
10
11
# File 'lib/deploy_pin/task.rb', line 9

def identifier
  @identifier
end

#recurringObject (readonly)

Returns the value of attribute recurring.



9
10
11
# File 'lib/deploy_pin/task.rb', line 9

def recurring
  @recurring
end

#scriptObject (readonly)

Returns the value of attribute script.



9
10
11
# File 'lib/deploy_pin/task.rb', line 9

def script
  @script
end

#titleObject (readonly)

Returns the value of attribute title.



9
10
11
# File 'lib/deploy_pin/task.rb', line 9

def title
  @title
end

Instance Method Details

#<=>(other) ⇒ Object

for sorting



101
102
103
# File 'lib/deploy_pin/task.rb', line 101

def <=>(other)
  sorting_key <=> other.sorting_key
end

#detailsObject



75
76
77
78
79
80
81
# File 'lib/deploy_pin/task.rb', line 75

def details
  {
    identifier: identifier,
    group: group,
    title: title
  }
end

#done?Boolean

Returns:

  • (Boolean)


39
40
41
42
43
# File 'lib/deploy_pin/task.rb', line 39

def done?
  return if recurring

  DeployPin::Record.where(uuid: identifier).exists?
end

#each_line(&block) ⇒ Object



67
68
69
70
71
72
73
# File 'lib/deploy_pin/task.rb', line 67

def each_line(&block)
  if file.starts_with?('# no_file_task')
    file.each_line(&block)
  else
    File.foreach(file, &block)
  end
end

#eql?(other) ⇒ Boolean

Returns:

  • (Boolean)


83
84
85
86
# File 'lib/deploy_pin/task.rb', line 83

def eql?(other)
  # same script & different identifier
  script == other.script && identifier != other.identifier
end

#markObject



32
33
34
35
36
37
# File 'lib/deploy_pin/task.rb', line 32

def mark
  return if recurring

  # store record in the DB
  DeployPin::Record.create(uuid: identifier)
end

#parseObject



49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
# File 'lib/deploy_pin/task.rb', line 49

def parse
  each_line do |line|
    case line.strip
    when /\A# (-?\d+):(\w+):?(recurring)?/
      @identifier = Regexp.last_match(1).to_i
      @group = Regexp.last_match(2)
      @recurring = Regexp.last_match(3)
    when /\A# task_title:(.+)/
      @title = Regexp.last_match(1).strip
    when /\A[^#].*/
      @script += line

      @explicit_timeout = true if line =~ /Database.execute_with_timeout.*/
      @parallel = true if line =~ /[Pp]arallel.*/
    end
  end
end

#runObject



27
28
29
30
# File 'lib/deploy_pin/task.rb', line 27

def run
  # eval script
  eval(script)
end

#sorting_keyObject



92
93
94
95
96
97
98
# File 'lib/deploy_pin/task.rb', line 92

def sorting_key
  if identifier.to_i.negative?
    [group_index, unreachable_future + identifier]
  else
    [group_index, identifier]
  end
end

#under_timeout?Boolean

Returns:

  • (Boolean)


45
46
47
# File 'lib/deploy_pin/task.rb', line 45

def under_timeout?
  !explicit_timeout? && !parallel?
end

#unreachable_futureObject



88
89
90
# File 'lib/deploy_pin/task.rb', line 88

def unreachable_future
  1.year.from_now.to_date.strftime('%Y%m%d%H%M%S').to_i
end