Class: GitRestart::Runner

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(fileList = nil) {|_self| ... } ⇒ Runner

Returns a new instance of Runner.

Yields:

  • (_self)

Yield Parameters:

Raises:

  • (ArgumentError)


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
77
78
# File 'lib/git-restart/runner.rb', line 32

def initialize(fileList = nil)
	raise ArgumentError, "File list needs to be nil or an Array!" unless (fileList.is_a? Array or fileList.nil?)

	GitRestart::Task.runner = self;

	@allowed_tasks = Array.new();
	@allowed_tasks << fileList if(fileList);
	@allowed_tasks << $taskfiles unless($taskfiles.empty?)

	@current_tasks = Hash.new();
	@next_tasks		= Hash.new();

	@branches 		= Array.new();
	@exclude_branches = Array.new();

	@branchQueue	= Queue.new();

	@git = Git.open(".");

	yield(self);

	@allowed_tasks.flatten!

	@listenedSub = @mqtt.subscribe_to "GitHub/#{@repo}" do |data|
		begin
			data = JSON.parse(data, symbolize_names: true);
		rescue
			next;
		end

		next unless data[:branch];
		if(not @branches.empty?)
			next unless @branches.include? data[:branch];
		elsif(not @exclude_branches.empty?)
			next if @exclude_branches.include? data[:branch];
		end

		@branchQueue << data;
	end

	autostart();
	_start_task_thread();

	at_exit {
		_stop_all_tasks();
	}
end

Instance Attribute Details

#allowed_tasksObject

Returns the value of attribute allowed_tasks.



14
15
16
# File 'lib/git-restart/runner.rb', line 14

def allowed_tasks
  @allowed_tasks
end

#branchesObject

Returns the value of attribute branches.



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

def branches
  @branches
end

#current_task_fileObject (readonly)

Returns the value of attribute current_task_file.



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

def current_task_file
  @current_task_file
end

#exclude_branchesObject

Returns the value of attribute exclude_branches.



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

def exclude_branches
  @exclude_branches
end

#mqttObject

Returns the value of attribute mqtt.



19
20
21
# File 'lib/git-restart/runner.rb', line 19

def mqtt
  @mqtt
end

#nameObject

Returns the value of attribute name.



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

def name
  @name
end

#next_tasksObject (readonly)

Returns the value of attribute next_tasks.



16
17
18
# File 'lib/git-restart/runner.rb', line 16

def next_tasks
  @next_tasks
end

#octokitObject

Returns the value of attribute octokit.



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

def octokit
  @octokit
end

#repoObject

Returns the value of attribute repo.



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

def repo
  @repo
end

#start_onObject

Returns the value of attribute start_on.



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

def start_on
  @start_on
end

Instance Method Details

#_generate_next_tasksObject



121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
# File 'lib/git-restart/runner.rb', line 121

def _generate_next_tasks()
	puts "Generating new tasks..."
	@next_tasks = Hash.new();

	taskFiles = `find ./ -nowarn -iname "*.gittask"`
	taskFiles.split("\n").each do |t|
		puts "Looking at: #{t}"
		t.gsub!(/^\.\//,"");
		@current_task_file = t;

		unless(@allowed_tasks.empty?)
			next unless @allowed_tasks.include? @current_task_file
		end

		begin
			load(t);
		rescue ScriptError, StandardError
			update_status("File #{t}", :failure, "File could not be parsed!")
			puts("File #{t} could not be loaded!");
		rescue GitRestart::TaskValidityError
			update_status("File #{t}", :failure, "Task-file not configured properly!")
			puts("Task-File #{t} is not configured properly!");
		end
	end

	puts "Finished loading! Next tasks: #{@next_tasks.keys}"
end

#_start_next_tasksObject



149
150
151
152
153
154
155
156
157
158
159
160
# File 'lib/git-restart/runner.rb', line 149

def _start_next_tasks()
	_generate_next_tasks();

	puts "\nStarting next tasks!"
	@next_tasks.each do |name, t|
		next unless t.active;
		next unless t.triggered?

		t.start();
		@current_tasks[name] = t;
	end
end

#_start_task_threadObject



94
95
96
97
98
99
100
101
102
103
# File 'lib/git-restart/runner.rb', line 94

def _start_task_thread()
	@taskThread = Thread.new do
		loop do
			newData = @branchQueue.pop;

			@current_modified = newData[:touched];
			_switch_to(newData[:branch], newData[:head_commit]);
		end
	end.abort_on_exception = true;
end

#_stop_all_tasksObject



114
115
116
# File 'lib/git-restart/runner.rb', line 114

def _stop_all_tasks()
	_stop_tasks(@current_tasks);
end

#_stop_tasks(taskList) ⇒ Object



105
106
107
108
109
110
111
112
113
# File 'lib/git-restart/runner.rb', line 105

def _stop_tasks(taskList)
	taskList.each do |name, t|
		t.stop();
	end
	taskList.each do |name, t|
		t.join();
		@current_tasks.delete(name);
	end
end

#_stop_triggered_tasksObject



117
118
119
# File 'lib/git-restart/runner.rb', line 117

def _stop_triggered_tasks()
	_stop_tasks(@current_tasks.select {|k,v| v.triggered?});
end

#_switch_to(branch, commit = nil) ⇒ Object



162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
# File 'lib/git-restart/runner.rb', line 162

def _switch_to(branch, commit = nil)
	puts "\n\nSwitching to branch: #{branch}#{commit ? ",commit: #{commit}" : ""}"

	begin
		@git.fetch();
	rescue
	end

	if(branch != current_branch())
		_stop_all_tasks();
	else
		_stop_triggered_tasks();
	end
	@git.reset_hard();
	@git.checkout(branch);
	@git.merge("origin/#{branch}");

	@git.reset_hard(commit);

	_start_next_tasks();
end

#autostartObject



184
185
186
187
# File 'lib/git-restart/runner.rb', line 184

def autostart()
	return unless @start_on;
	@branchQueue << {branch: @start_on};
end

#current_branchObject



25
26
27
# File 'lib/git-restart/runner.rb', line 25

def current_branch()
	@git.current_branch();
end

#current_commitObject



22
23
24
# File 'lib/git-restart/runner.rb', line 22

def current_commit()
	@git.object("HEAD").sha;
end

#current_modifiedObject



28
29
30
# File 'lib/git-restart/runner.rb', line 28

def current_modified()
	@current_modified;
end

#update_status(name, newStatus, message = nil) ⇒ Object



80
81
82
83
84
85
86
87
88
89
90
91
92
# File 'lib/git-restart/runner.rb', line 80

def update_status(name, newStatus, message = nil)
	puts "Task #{@name} assumed a new status: #{newStatus}#{message ? " MSG:#{message}" : ""}"

	return unless @octokit;

	begin
	@octokit.create_status(@repo, current_commit(), newStatus, {
			context: "#{@name}/#{name}".gsub(" ", "_"),
			description: message,
		})
	rescue
	end
end