Class: GitRestart::Task

Inherits:
Object
  • Object
show all
Defined in:
lib/git-restart/task.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize {|_self| ... } ⇒ Task

Returns a new instance of Task.

Yields:

  • (_self)

Yield Parameters:



54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
# File 'lib/git-restart/task.rb', line 54

def initialize()
  @statuschange_mutex = Mutex.new();

  @targets = Array.new();
  @watched = Array.new();

  @signal  = "INT"
  @expect_clean_exit = true;
  @exiting         = false;

  @lastStatus = 0;
  @chdir = File.dirname(runner().current_task_file);

  watch(File.basename(runner().current_task_file));

  yield(self);

  valid?

  @status_file ||= "/tmp/TaskLog_#{@name}_#{current_commit()}";

  if(runner().next_tasks[@name])
    raise TaskValidityError, "A task of name #{@name} already exists!"
  else
    runner().next_tasks[@name] = self;
  end
end

Instance Attribute Details

#activeObject

Returns the value of attribute active.



15
16
17
# File 'lib/git-restart/task.rb', line 15

def active
  @active
end

#ci_taskObject

Returns the value of attribute ci_task.



12
13
14
# File 'lib/git-restart/task.rb', line 12

def ci_task
  @ci_task
end

#expect_clean_exitObject

Returns the value of attribute expect_clean_exit.



10
11
12
# File 'lib/git-restart/task.rb', line 10

def expect_clean_exit
  @expect_clean_exit
end

#lastStatusObject (readonly)

Returns the value of attribute lastStatus.



17
18
19
# File 'lib/git-restart/task.rb', line 17

def lastStatus
  @lastStatus
end

#nameObject

Returns the value of attribute name.



13
14
15
# File 'lib/git-restart/task.rb', line 13

def name
  @name
end

#report_statusObject

Returns the value of attribute report_status.



11
12
13
# File 'lib/git-restart/task.rb', line 11

def report_status
  @report_status
end

#signalObject

Returns the value of attribute signal.



9
10
11
# File 'lib/git-restart/task.rb', line 9

def signal
  @signal
end

#status_fileObject

Returns the value of attribute status_file.



13
14
15
# File 'lib/git-restart/task.rb', line 13

def status_file
  @status_file
end

#status_messageObject (readonly)

Returns the value of attribute status_message.



18
19
20
# File 'lib/git-restart/task.rb', line 18

def status_message
  @status_message
end

#targetsObject (readonly)

Returns the value of attribute targets.



7
8
9
# File 'lib/git-restart/task.rb', line 7

def targets
  @targets
end

Class Method Details

.runnerObject



23
24
25
# File 'lib/git-restart/task.rb', line 23

def self.runner()
  return @runner;
end

.runner=(runner) ⇒ Object



20
21
22
# File 'lib/git-restart/task.rb', line 20

def self.runner=(runner)
  @runner = runner;
end

Instance Method Details

#_get_statuslineObject



115
116
117
118
119
120
121
122
123
124
# File 'lib/git-restart/task.rb', line 115

def _get_statusline()
  return "No status specified" unless File.exist? @status_file

  sMsg = ""
  File.open(@status_file, "r") do |sFile|
    sFile.each_line do |l| sMsg = l; end
  end

  return sMsg;
end

#_rm_logfileObject



110
111
112
113
114
# File 'lib/git-restart/task.rb', line 110

def _rm_logfile()
  if File.exist?("/tmp/TaskLog_#{@name}_#{current_commit()}") then
    File.delete("/tmp/TaskLog_#{@name}_#{current_commit()}");
  end
end

#branchObject



30
31
32
# File 'lib/git-restart/task.rb', line 30

def branch()
  runner().current_branch();
end

#current_commitObject



33
34
35
# File 'lib/git-restart/task.rb', line 33

def current_commit()
  runner().current_commit();
end

#joinObject



190
191
192
# File 'lib/git-restart/task.rb', line 190

def join()
  @executionThread.join();
end

#modifiedObject



36
37
38
# File 'lib/git-restart/task.rb', line 36

def modified()
  runner().current_modified();
end

#on_branches(branches) ⇒ Object



48
49
50
51
52
# File 'lib/git-restart/task.rb', line 48

def on_branches(branches)
  [branches].flatten.each do |b|
    @active |= (b == branch());
  end
end

#runnerObject



26
27
28
# File 'lib/git-restart/task.rb', line 26

def runner()
  return self.class.runner();
end

#startObject



136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
# File 'lib/git-restart/task.rb', line 136

def start()
  puts "Starting Task: #{@name}"

  if @targets.empty?
    _report_status(:success, "No tasks to run!");
    return
  end

  @executionThread = Thread.new do
    _report_status(:pending);

    @targets.each do |target|
      @statuschange_mutex.synchronize {
        break if @exiting
        _rm_logfile();
        options = {
          [:out, :err] => "/tmp/TaskLog_#{@name}_#{current_commit()}"
        }
        options[:chdir] = @chdir if @chdir

        @currentPID = Process.spawn(target, options);
      }

      status = Process.wait2(@currentPID)[1];
      @currentPID = nil;
      @lastStatus = status.exitstatus();

      break unless @lastStatus == 0;
    end

    if(@lastStatus == 0)
      _report_status(:success);
      _rm_logfile();
    elsif(!@exiting || @expect_clean_exit)
      _report_status(:failure);
    end
  end
  @executionThread.abort_on_exception = true;

  sleep 0.01
end

#stopObject



178
179
180
181
182
183
184
185
186
187
188
# File 'lib/git-restart/task.rb', line 178

def stop()
  puts "Stopping Task: #{@name}"
  return if @signal.nil?

  @statuschange_mutex.synchronize {
    @exiting = true;
    if(p = @currentPID)
      Process.kill(@signal, p);
    end
  }
end

#triggered?Boolean

Returns:

  • (Boolean)


82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
# File 'lib/git-restart/task.rb', line 82

def triggered?
  return true if modified().nil?
  return true if @ci_task

  @watched.each do |regEx|
    modified().each do |f|
      if regEx.to_s =~ /^\(\?\-mix:\\\/(.*)\)$/ then
        return true if f =~ Regexp.new($1);
      else
        next unless f =~ /#{Regexp.quote(@chdir)}(.*)/
        return true if $1 =~ regEx;
      end
    end
  end

  return false;
end

#valid?Boolean

Returns:

  • (Boolean)


100
101
102
103
104
105
106
107
108
# File 'lib/git-restart/task.rb', line 100

def valid?()
  unless Signal.list[@signal] or @signal.nil?
    raise TaskValidityError, "The specified kill-signal is not valid!"
  end

  unless @name
    raise TaskValidityError, "A name needs to be set for identification!"
  end
end

#watch(regEx) ⇒ Object



40
41
42
43
44
45
46
# File 'lib/git-restart/task.rb', line 40

def watch(regEx)
  if(regEx.is_a? String)
    regEx = Regexp.quote(regEx);
  end

  @watched << Regexp.new(regEx);
end