From 3d33d704b76e952f85d9d45356a783d925d0906b Mon Sep 17 00:00:00 2001 From: Xasin Date: Mon, 23 Jul 2018 19:32:45 +0200 Subject: [PATCH 01/81] First (incomplete) version of git-restart --- Ruby/GitRestart/bin/git-restart | 3 +++ Ruby/GitRestart/git-restart.gemspec | 5 +++++ .../lib/git-restart/specification.rb | 22 +++++++++++++++++++ 3 files changed, 30 insertions(+) create mode 100644 Ruby/GitRestart/bin/git-restart create mode 100644 Ruby/GitRestart/git-restart.gemspec create mode 100644 Ruby/GitRestart/lib/git-restart/specification.rb diff --git a/Ruby/GitRestart/bin/git-restart b/Ruby/GitRestart/bin/git-restart new file mode 100644 index 00000000..9247d21f --- /dev/null +++ b/Ruby/GitRestart/bin/git-restart @@ -0,0 +1,3 @@ +#!/usr/bin/ruby + +require 'mqtt/sub_handler.rb' diff --git a/Ruby/GitRestart/git-restart.gemspec b/Ruby/GitRestart/git-restart.gemspec new file mode 100644 index 00000000..54572d49 --- /dev/null +++ b/Ruby/GitRestart/git-restart.gemspec @@ -0,0 +1,5 @@ +Gem::Specification.new do |s| + s.name = 'git-restart' + s.version = '0.0.1' + s.summary = '(Re)start scripts and monitor them on a GitHub push' +end diff --git a/Ruby/GitRestart/lib/git-restart/specification.rb b/Ruby/GitRestart/lib/git-restart/specification.rb new file mode 100644 index 00000000..633f0455 --- /dev/null +++ b/Ruby/GitRestart/lib/git-restart/specification.rb @@ -0,0 +1,22 @@ + +module GitRestart + class Specification + def initialize(&block) + @target = "start.rb" + end + + def valid?() + unless File.exist?(@target) + raise ArgumentError, "No executable found!" + end + + unless @mqtt + raise ArgumentError, "No mqtt URL specified" + end + + unless @signal + + end + end + end +end From 5edb0208fd2bc06d35b02cbd245756da58fc7354 Mon Sep 17 00:00:00 2001 From: David Bailey Date: Mon, 23 Jul 2018 22:21:14 +0200 Subject: [PATCH 02/81] Added more info to the .gemspec --- Ruby/GitRestart/git-restart.gemspec | 15 ++++++++++++--- 1 file changed, 12 insertions(+), 3 deletions(-) diff --git a/Ruby/GitRestart/git-restart.gemspec b/Ruby/GitRestart/git-restart.gemspec index 54572d49..0d9423e5 100644 --- a/Ruby/GitRestart/git-restart.gemspec +++ b/Ruby/GitRestart/git-restart.gemspec @@ -1,5 +1,14 @@ Gem::Specification.new do |s| - s.name = 'git-restart' - s.version = '0.0.1' - s.summary = '(Re)start scripts and monitor them on a GitHub push' + s.name = 'git-restart' + s.version = '0.0.1' + s.summary = '(Re)start scripts and monitor them on a GitHub push' + s.description = 'This gem can be used to (re)start scripts whenever a GitHub push event is recorded. +The exit status of scripts can be monitored, and a failure can be sent back, making this capable of running simple tests too!' + s.authors = ['Xasin'] + + s.homepage = + 'https://github.com/XasWorks/XasCode/tree/master/Ruby/GitRestart' + s.license = 'GPL-3.0' + + s.add_runtime_dependency "mqtt-sub_handler", "~> 1.0" end From d36b7b6e61c37749528f79a64ddedf859fe7a797 Mon Sep 17 00:00:00 2001 From: David Bailey Date: Mon, 23 Jul 2018 22:21:46 +0200 Subject: [PATCH 03/81] Renamed specification.rb to task.rb, began working on the logic --- .../lib/git-restart/specification.rb | 22 ------- Ruby/GitRestart/lib/git-restart/task.rb | 58 +++++++++++++++++++ 2 files changed, 58 insertions(+), 22 deletions(-) delete mode 100644 Ruby/GitRestart/lib/git-restart/specification.rb create mode 100644 Ruby/GitRestart/lib/git-restart/task.rb diff --git a/Ruby/GitRestart/lib/git-restart/specification.rb b/Ruby/GitRestart/lib/git-restart/specification.rb deleted file mode 100644 index 633f0455..00000000 --- a/Ruby/GitRestart/lib/git-restart/specification.rb +++ /dev/null @@ -1,22 +0,0 @@ - -module GitRestart - class Specification - def initialize(&block) - @target = "start.rb" - end - - def valid?() - unless File.exist?(@target) - raise ArgumentError, "No executable found!" - end - - unless @mqtt - raise ArgumentError, "No mqtt URL specified" - end - - unless @signal - - end - end - end -end diff --git a/Ruby/GitRestart/lib/git-restart/task.rb b/Ruby/GitRestart/lib/git-restart/task.rb new file mode 100644 index 00000000..17f95c64 --- /dev/null +++ b/Ruby/GitRestart/lib/git-restart/task.rb @@ -0,0 +1,58 @@ + +module GitRestart + class Task + attr_reader :targets + attr_accessor :signal + attr_accessor :restart_on_exit, :restart_on_failure, :expect_clean_exit + attr_accessor :complete + + def initialize() + @targets = Array.new(); + @signal = "INT" + + @restart_on_exit = false; + @restart_on_failure = false; + @expect_clean_exit = true; + + @complete = true; + @exiting = false; + + yield(self); + + @name ||= @targets[-1]; + end + + def valid?() + @targets.each do |t| + raise ArgumentError, "Target-File #{t} wasn't found!" unless File.exist?(t) + end + + unless Signal.list[@signal] or @signal.nil? + raise ArgumentError, "The specified kill-signal is not valid!" + end + end + + def start() + @executionThread = Thread.new do + currentTargetI = 0; + loop do + target = @targets[currentTargetI]; + + @currentPID = Process.spawn(target, [:in, :out, :err] => "/dev/null", chdir: @chdir); + pid, status = Process.wait2(@currentPID); + @lastStatus = status.exitstatus; + + success = @lastStatus == 0; + lastTask = @targets[currentTargetI + 1].nil? + + end + end + end + + def stop() + end + + def join() + end + end +end From 3dba00a843f969c33a2e4d000f22e9520d8e189c Mon Sep 17 00:00:00 2001 From: David Bailey Date: Tue, 24 Jul 2018 23:28:06 +0200 Subject: [PATCH 04/81] First version of the single task execution thread --- Ruby/GitRestart/lib/git-restart/task.rb | 32 ++++++++++++++----------- 1 file changed, 18 insertions(+), 14 deletions(-) diff --git a/Ruby/GitRestart/lib/git-restart/task.rb b/Ruby/GitRestart/lib/git-restart/task.rb index 17f95c64..9fa926d9 100644 --- a/Ruby/GitRestart/lib/git-restart/task.rb +++ b/Ruby/GitRestart/lib/git-restart/task.rb @@ -2,20 +2,17 @@ module GitRestart class Task attr_reader :targets + attr_accessor :signal - attr_accessor :restart_on_exit, :restart_on_failure, :expect_clean_exit - attr_accessor :complete + attr_accessor :expect_clean_exit + attr_accessor :name, :status_descriptor def initialize() @targets = Array.new(); @signal = "INT" - @restart_on_exit = false; - @restart_on_failure = false; @expect_clean_exit = true; - - @complete = true; - @exiting = false; + @exiting = false; yield(self); @@ -32,19 +29,26 @@ def valid?() end end + def report_error() + + end + def start() @executionThread = Thread.new do - currentTargetI = 0; - loop do - target = @targets[currentTargetI]; + @targets.each do |target| + break if (@exiting && !@signal.nil?); @currentPID = Process.spawn(target, [:in, :out, :err] => "/dev/null", chdir: @chdir); - pid, status = Process.wait2(@currentPID); - @lastStatus = status.exitstatus; + nil, status = Process.wait2(@currentPID); + @lastStatus = status.exitstatus(); - success = @lastStatus == 0; - lastTask = @targets[currentTargetI + 1].nil? + break unless @lastStatus == 0; + end + if(@lastStatus == 0) + report_success(); + elsif(!@exiting || @expect_clean_exit) + report_error(); end end end From 97a2cb4fe27b47f491693df27dcdfc1b516785b1 Mon Sep 17 00:00:00 2001 From: David Bailey Date: Wed, 25 Jul 2018 18:58:20 +0200 Subject: [PATCH 05/81] Simplified code execution significantly --- Ruby/GitRestart/lib/git-restart/task.rb | 13 ++++++++++--- 1 file changed, 10 insertions(+), 3 deletions(-) diff --git a/Ruby/GitRestart/lib/git-restart/task.rb b/Ruby/GitRestart/lib/git-restart/task.rb index 9fa926d9..ae71c4a8 100644 --- a/Ruby/GitRestart/lib/git-restart/task.rb +++ b/Ruby/GitRestart/lib/git-restart/task.rb @@ -36,10 +36,17 @@ def report_error() def start() @executionThread = Thread.new do @targets.each do |target| - break if (@exiting && !@signal.nil?); + @statuschange_mutex.synchronize { + break if @exiting + options = { + [:in, :out, :err] => "/dev/null" + } + options[:chdir] = @chdir if @chdir - @currentPID = Process.spawn(target, [:in, :out, :err] => "/dev/null", chdir: @chdir); - nil, status = Process.wait2(@currentPID); + @currentPID = Process.spawn(target, options); + } + + status = Process.wait2(@currentPID)[1]; @lastStatus = status.exitstatus(); break unless @lastStatus == 0; From 6cf7489dc44c1b01e7873f5789434fc7b2ad0300 Mon Sep 17 00:00:00 2001 From: David Bailey Date: Wed, 25 Jul 2018 18:58:52 +0200 Subject: [PATCH 06/81] Added proper start/stop/join commands --- Ruby/GitRestart/lib/git-restart/task.rb | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/Ruby/GitRestart/lib/git-restart/task.rb b/Ruby/GitRestart/lib/git-restart/task.rb index ae71c4a8..0170af2a 100644 --- a/Ruby/GitRestart/lib/git-restart/task.rb +++ b/Ruby/GitRestart/lib/git-restart/task.rb @@ -61,9 +61,18 @@ def start() end def stop() + return if @signal.nil? + + @statuschange_mutex.synchronize { + @exiting = true; + if(p = @currentPID) + Process.kill(@signal, p); + end + } end def join() + @executionThread.join(); end end end From e1cfa3f99f58613ce80df506f678b44a8005925f Mon Sep 17 00:00:00 2001 From: David Bailey Date: Wed, 25 Jul 2018 18:59:46 +0200 Subject: [PATCH 07/81] Added better status reporting --- Ruby/GitRestart/lib/git-restart/task.rb | 12 +++++++++--- 1 file changed, 9 insertions(+), 3 deletions(-) diff --git a/Ruby/GitRestart/lib/git-restart/task.rb b/Ruby/GitRestart/lib/git-restart/task.rb index 0170af2a..d85d902a 100644 --- a/Ruby/GitRestart/lib/git-restart/task.rb +++ b/Ruby/GitRestart/lib/git-restart/task.rb @@ -29,12 +29,18 @@ def valid?() end end - def report_error() + def _report_status(status, message = nil) + @status_message = "" + return unless @report_status + print "Task #{@name} assumed a new status: #{status}#{message ? " MSG:#{message}" : ""}" end + private :_report_status def start() @executionThread = Thread.new do + _report_status(:pending); + @targets.each do |target| @statuschange_mutex.synchronize { break if @exiting @@ -53,9 +59,9 @@ def start() end if(@lastStatus == 0) - report_success(); + _report_status(:success); elsif(!@exiting || @expect_clean_exit) - report_error(); + _report_status(:failure); end end end From d54763c31a311cafc213e0f27d5aa41f2678fd39 Mon Sep 17 00:00:00 2001 From: David Bailey Date: Wed, 25 Jul 2018 19:00:08 +0200 Subject: [PATCH 08/81] Added more possible config options --- Ruby/GitRestart/lib/git-restart/task.rb | 35 +++++++++++++++++++------ 1 file changed, 27 insertions(+), 8 deletions(-) diff --git a/Ruby/GitRestart/lib/git-restart/task.rb b/Ruby/GitRestart/lib/git-restart/task.rb index d85d902a..439096e0 100644 --- a/Ruby/GitRestart/lib/git-restart/task.rb +++ b/Ruby/GitRestart/lib/git-restart/task.rb @@ -5,28 +5,47 @@ class Task attr_accessor :signal attr_accessor :expect_clean_exit - attr_accessor :name, :status_descriptor + attr_accessor :report_status + attr_accessor :name, :status_file + attr_accessor :active + attr_reader :lastStatus + attr_reader :status_message + + def self.branch(newBranch) + @branch = newBranch; + end + def branch() + self.class.branch(); + end + + def self.modified(newAffected) + @modified = newAffected; + end + def modified() + self.class.modified + end + def initialize() + @statuschange_mutex = Mutex.new(); + @targets = Array.new(); - @signal = "INT" + @signal = "INT" @expect_clean_exit = true; @exiting = false; yield(self); - - @name ||= @targets[-1]; end def valid?() - @targets.each do |t| - raise ArgumentError, "Target-File #{t} wasn't found!" unless File.exist?(t) - end - unless Signal.list[@signal] or @signal.nil? raise ArgumentError, "The specified kill-signal is not valid!" end + + unless @name + raise ArgumentError, "A name needs to be set for identification!" + end end def _report_status(status, message = nil) From 2bcdb1195b3b138abc1cc2c491379efc5923ec06 Mon Sep 17 00:00:00 2001 From: David Bailey Date: Wed, 25 Jul 2018 19:00:36 +0200 Subject: [PATCH 09/81] Added Guard and a test for GitRestart::Task --- Ruby/GitRestart/Guardfile | 9 ++++ Ruby/GitRestart/git-restart.gemspec | 4 ++ Ruby/GitRestart/tests/tc_task.rb | 83 +++++++++++++++++++++++++++++ 3 files changed, 96 insertions(+) create mode 100644 Ruby/GitRestart/Guardfile create mode 100644 Ruby/GitRestart/tests/tc_task.rb diff --git a/Ruby/GitRestart/Guardfile b/Ruby/GitRestart/Guardfile new file mode 100644 index 00000000..73140a36 --- /dev/null +++ b/Ruby/GitRestart/Guardfile @@ -0,0 +1,9 @@ + +# with Minitest::Unit +guard :minitest, + test_folders: ['tests'], + test_file_patterns: ["tc_*.rb"] do + + watch(%r{^lib/git-restart/([^/]+)\.rb}) { |m| "tests/tc_#{m[1]}.rb"} + watch(%r{^tests/tc_(.*).rb}) +end diff --git a/Ruby/GitRestart/git-restart.gemspec b/Ruby/GitRestart/git-restart.gemspec index 0d9423e5..f46851e2 100644 --- a/Ruby/GitRestart/git-restart.gemspec +++ b/Ruby/GitRestart/git-restart.gemspec @@ -11,4 +11,8 @@ The exit status of scripts can be monitored, and a failure can be sent back, mak s.license = 'GPL-3.0' s.add_runtime_dependency "mqtt-sub_handler", "~> 1.0" + + s.add_development_dependency "minitest" + s.add_development_dependency "guard" + s.add_development_dependency "guard-minitest" end diff --git a/Ruby/GitRestart/tests/tc_task.rb b/Ruby/GitRestart/tests/tc_task.rb new file mode 100644 index 00000000..9938f27e --- /dev/null +++ b/Ruby/GitRestart/tests/tc_task.rb @@ -0,0 +1,83 @@ + +require_relative "../lib/git-restart/task.rb" + +class Test_Task < Minitest::Test + def setup() + `rm /tmp/TEST_FILE_* 2>/dev/null` + end + + def teardown() + `rm /tmp/TEST_FILE_* 2>/dev/null` + end + + def test_init + # Test if the whole code starts normally + @task = GitRestart::Task.new() do |t| + t.name = "Test-Task" + end + @task.valid? + + # Check if signals are checked properly + @task.signal = nil; + @task.valid? + + @task.signal = "NonexistantSignal" + assert_raises { @task.valid? } + @task.signal = "INT" + end + + def test_abort_on_error + @task = GitRestart::Task.new() do |t| + t.name = "TestTask" + + t.targets << "touch /tmp/TEST_FILE_1"; + t.targets << "exit 1"; + t.targets << "touch /tmp/TEST_FILE_2"; + end + + @task.start(); + @task.join(); + + assert_equal 1, @task.lastStatus; + assert File.exist?("/tmp/TEST_FILE_1"); + refute File.exist?("/tmp/TEST_FILE_2"); + end + + def test_stop_execution + @task = GitRestart::Task.new() do |t| + t.name = "TestTask" + + t.targets << "touch /tmp/TEST_FILE_1"; + t.targets << "sleep 10"; + t.targets << "touch /tmp/TEST_FILE_2"; + end + + @task.start(); + sleep(0.5); + @task.stop(); + @task.join(); + + assert File.exist?("/tmp/TEST_FILE_1"); + refute File.exist?("/tmp/TEST_FILE_2"); + end + + def test_complete_execution + @task = GitRestart::Task.new() do |t| + t.name = "TestTask" + + t.targets << "touch /tmp/TEST_FILE_1"; + t.targets << "sleep 1"; + t.targets << "touch /tmp/TEST_FILE_2"; + + t.signal = nil; + end + + @task.start(); + sleep 0.5; + @task.stop(); + @task.join(); + + assert File.exist?("/tmp/TEST_FILE_1"); + assert File.exist?("/tmp/TEST_FILE_2"); + end +end From b08882e5f819d7e61488f76d33793eb771266ecb Mon Sep 17 00:00:00 2001 From: David Bailey Date: Wed, 25 Jul 2018 20:08:24 +0200 Subject: [PATCH 10/81] Fixed class-wide `branch` and `modified` getter/setter --- Ruby/GitRestart/lib/git-restart/task.rb | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/Ruby/GitRestart/lib/git-restart/task.rb b/Ruby/GitRestart/lib/git-restart/task.rb index 439096e0..edd6760b 100644 --- a/Ruby/GitRestart/lib/git-restart/task.rb +++ b/Ruby/GitRestart/lib/git-restart/task.rb @@ -12,16 +12,22 @@ class Task attr_reader :lastStatus attr_reader :status_message - def self.branch(newBranch) + def self.branch=(newBranch) @branch = newBranch; end + def self.branch() + @branch; + end def branch() self.class.branch(); end - def self.modified(newAffected) + def self.modified=(newAffected) @modified = newAffected; end + def self.modified() + return @modified; + end def modified() self.class.modified end From ed608e2e5edd456d0f809e3fbe73971ea8cba835 Mon Sep 17 00:00:00 2001 From: David Bailey Date: Wed, 25 Jul 2018 20:09:39 +0200 Subject: [PATCH 11/81] Added "triggered" option vs. "active", added `watch` and `on_branch` --- Ruby/GitRestart/lib/git-restart/task.rb | 27 ++++++++++++++++++++++++- 1 file changed, 26 insertions(+), 1 deletion(-) diff --git a/Ruby/GitRestart/lib/git-restart/task.rb b/Ruby/GitRestart/lib/git-restart/task.rb index edd6760b..1065c4bf 100644 --- a/Ruby/GitRestart/lib/git-restart/task.rb +++ b/Ruby/GitRestart/lib/git-restart/task.rb @@ -7,7 +7,10 @@ class Task attr_accessor :expect_clean_exit attr_accessor :report_status attr_accessor :name, :status_file + attr_accessor :chdir + attr_accessor :active + attr_accessor :triggered attr_reader :lastStatus attr_reader :status_message @@ -31,7 +34,29 @@ def self.modified() def modified() self.class.modified end - + + def watch(regEx) + if(regEx.is_a? String) + regEx = %r[#{Regexp.quote(regEx)}]; + end + + modified().each do |f| + unless(@chdir.nil? or @chdir.empty?) + if(f =~ %r{^#{Regexp.quote(@chdir)}/(.+)}) + @triggered |= ($1 =~ regEx); + end + else + @triggered |= (f =~ regEx); + end + end + end + + def on_branches(branches) + [branches].flatten.each do |b| + @active |= (b == branch()); + end + end + def initialize() @statuschange_mutex = Mutex.new(); From cdb81a4ab8577aa51556b8707131df85fcfb013d Mon Sep 17 00:00:00 2001 From: David Bailey Date: Wed, 25 Jul 2018 20:09:58 +0200 Subject: [PATCH 12/81] Added tests for all! --- Ruby/GitRestart/tests/tc_task.rb | 42 ++++++++++++++++++++++++++++++++ 1 file changed, 42 insertions(+) diff --git a/Ruby/GitRestart/tests/tc_task.rb b/Ruby/GitRestart/tests/tc_task.rb index 9938f27e..2a82611b 100644 --- a/Ruby/GitRestart/tests/tc_task.rb +++ b/Ruby/GitRestart/tests/tc_task.rb @@ -80,4 +80,46 @@ def test_complete_execution assert File.exist?("/tmp/TEST_FILE_1"); assert File.exist?("/tmp/TEST_FILE_2"); end + + def test_chdir + @task = GitRestart::Task.new() do |t| + t.name = "TestTask"; + t.chdir = "/tmp"; + + t.targets << "touch TEST_FILE_1"; + end + + @task.start(); + @task.join(); + + assert File.exist?("/tmp/TEST_FILE_1"); + end + + def test_triggers() + GitRestart::Task.branch ="master"; + GitRestart::Task.modified = ["Tests/Test1/Test.rb"]; + + @task = GitRestart::Task.new() do |t| + t.chdir = "Tests/Test1"; + t.name = "TestTask" + + t.watch(%r{.*\.rb}); + assert t.triggered; + t.triggered = false; + + t.watch("Test.rb"); + assert t.triggered; + t.triggered = false; + + t.watch("NotTest.rb"); + refute t.triggered; + + t.on_branches ["master", "dev"]; + assert t.active; + t.active = false; + + t.on_branches "dev"; + refute t.active; + end + end end From e45bd54bc1b747e119dbd8f049c68e63908f82ad Mon Sep 17 00:00:00 2001 From: Xasin Date: Thu, 26 Jul 2018 19:07:37 +0200 Subject: [PATCH 13/81] Began working on the runner file --- Ruby/GitRestart/git-restart.gemspec | 5 ++ Ruby/GitRestart/lib/git-restart/runner.rb | 67 +++++++++++++++++++++++ 2 files changed, 72 insertions(+) create mode 100644 Ruby/GitRestart/lib/git-restart/runner.rb diff --git a/Ruby/GitRestart/git-restart.gemspec b/Ruby/GitRestart/git-restart.gemspec index f46851e2..f3d31fae 100644 --- a/Ruby/GitRestart/git-restart.gemspec +++ b/Ruby/GitRestart/git-restart.gemspec @@ -6,11 +6,16 @@ Gem::Specification.new do |s| The exit status of scripts can be monitored, and a failure can be sent back, making this capable of running simple tests too!' s.authors = ['Xasin'] + s.files = [ 'bin/git-restart', + 'lib/git-restart/runner.rb', + 'lib/git-restart/task.rb'] + s.homepage = 'https://github.com/XasWorks/XasCode/tree/master/Ruby/GitRestart' s.license = 'GPL-3.0' s.add_runtime_dependency "mqtt-sub_handler", "~> 1.0" + s.add_runtime_dependency "git", "~> 1.4" s.add_development_dependency "minitest" s.add_development_dependency "guard" diff --git a/Ruby/GitRestart/lib/git-restart/runner.rb b/Ruby/GitRestart/lib/git-restart/runner.rb new file mode 100644 index 00000000..149cfad2 --- /dev/null +++ b/Ruby/GitRestart/lib/git-restart/runner.rb @@ -0,0 +1,67 @@ +require 'mqtt/sub_handler' + +require_relative "runner.rb" + +module GitRestart + class Runner + attr_accessor :name + + attr_accessor :repo, :branches, :exclude_branches, :start_on + + def current_commit() + @git.object("HEAD^").sha; + end + def current_branch() + @git.current_branch(); + end + + def initialize() + @currentTasks = Hash.new(); + @nextTasks = Hash.new(); + + @branches = Array.new(); + @exclude_branches = Array.new(); + + @branchQueue = Queue.new(); + + yield(self); + + @git = Git.open("."); + + @mqtt.subscribe_to "GitHub/#{@repo}" do |data| + begin + data = JSON.parse(data, symbolize_names: true); + rescue + next; + end + + next unless data[:branch]; + if(@branches) + next unless @branches.include? data[:branch]; + elsif(@exclude_branches) + next if @exclude_branches.include? data[:branch]; + end + + @branchQueue << data; + end + + autostart(); + end + + def autostart() + return unless @start_on; + + @git.fetch(); + @git.checkout(@start_on); + @git.merge("origin/#{@start_on}"); + end + + def mqtt=(mqtt) + if(mqtt.is_a? String) + @mqtt = MQTT::SubHandler.new(mqtt); + else + @mqtt = mqtt; + end + end + end +end From b39d2ce98551e06b8dac5cb69a94caf04500c226 Mon Sep 17 00:00:00 2001 From: David Bailey Date: Thu, 26 Jul 2018 20:40:04 +0200 Subject: [PATCH 14/81] Added a proper error class for validation errors --- Ruby/GitRestart/lib/git-restart/task.rb | 16 ++++++++++++++-- 1 file changed, 14 insertions(+), 2 deletions(-) diff --git a/Ruby/GitRestart/lib/git-restart/task.rb b/Ruby/GitRestart/lib/git-restart/task.rb index 1065c4bf..42ffac87 100644 --- a/Ruby/GitRestart/lib/git-restart/task.rb +++ b/Ruby/GitRestart/lib/git-restart/task.rb @@ -1,5 +1,8 @@ module GitRestart + class TaskValidityError < StandardError + end + class Task attr_reader :targets @@ -67,15 +70,24 @@ def initialize() @exiting = false; yield(self); + + valid? + + if(runner().next_tasks[@name]) + raise TaskValidityError, "A task of name #{@name} already exists!" + else + runner().next_tasks[@name] = self; + end + end end def valid?() unless Signal.list[@signal] or @signal.nil? - raise ArgumentError, "The specified kill-signal is not valid!" + raise TaskValidityError, "The specified kill-signal is not valid!" end unless @name - raise ArgumentError, "A name needs to be set for identification!" + raise TaskValidityError, "A name needs to be set for identification!" end end From a8a38533f7e666eff4de6edc31cfd92019a0427e Mon Sep 17 00:00:00 2001 From: David Bailey Date: Thu, 26 Jul 2018 20:42:53 +0200 Subject: [PATCH 15/81] Reworked the trigger mechanism to be dynamic --- Ruby/GitRestart/lib/git-restart/task.rb | 28 ++++++++++++++++--------- 1 file changed, 18 insertions(+), 10 deletions(-) diff --git a/Ruby/GitRestart/lib/git-restart/task.rb b/Ruby/GitRestart/lib/git-restart/task.rb index 42ffac87..37a16b86 100644 --- a/Ruby/GitRestart/lib/git-restart/task.rb +++ b/Ruby/GitRestart/lib/git-restart/task.rb @@ -13,7 +13,6 @@ class Task attr_accessor :chdir attr_accessor :active - attr_accessor :triggered attr_reader :lastStatus attr_reader :status_message @@ -40,18 +39,14 @@ def modified() def watch(regEx) if(regEx.is_a? String) - regEx = %r[#{Regexp.quote(regEx)}]; + regEx = Regexp.quote(regEx); end - modified().each do |f| - unless(@chdir.nil? or @chdir.empty?) - if(f =~ %r{^#{Regexp.quote(@chdir)}/(.+)}) - @triggered |= ($1 =~ regEx); - end - else - @triggered |= (f =~ regEx); - end + if(@chdir) + regEx = "#{Regexp.quote(@chdir)}/#{regEx.to_s}"; end + + @watched << Regexp.new(regEx); end def on_branches(branches) @@ -64,6 +59,8 @@ def initialize() @statuschange_mutex = Mutex.new(); @targets = Array.new(); + @watched = Array.new(); + watch(runner().current_task_file); @signal = "INT" @expect_clean_exit = true; @@ -79,6 +76,17 @@ def initialize() runner().next_tasks[@name] = self; end end + + def triggered? + return true if modified().nil? + + @watched.each do |regEx| + modified().each do |f| + return true if f =~ regEx; + end + end + + return false; end def valid?() From 2f2257abd08539b8fdc77b02b0d95a96c53e6af8 Mon Sep 17 00:00:00 2001 From: David Bailey Date: Thu, 26 Jul 2018 20:44:22 +0200 Subject: [PATCH 16/81] Better integration with the Runner class --- Ruby/GitRestart/lib/git-restart/task.rb | 24 +++++++++++------------- 1 file changed, 11 insertions(+), 13 deletions(-) diff --git a/Ruby/GitRestart/lib/git-restart/task.rb b/Ruby/GitRestart/lib/git-restart/task.rb index 37a16b86..10a85401 100644 --- a/Ruby/GitRestart/lib/git-restart/task.rb +++ b/Ruby/GitRestart/lib/git-restart/task.rb @@ -10,31 +10,27 @@ class Task attr_accessor :expect_clean_exit attr_accessor :report_status attr_accessor :name, :status_file - attr_accessor :chdir attr_accessor :active attr_reader :lastStatus attr_reader :status_message - def self.branch=(newBranch) - @branch = newBranch; + def self.runner=(runner) + @runner = runner; end - def self.branch() - @branch; + def self.runner() + return @runner; end - def branch() - self.class.branch(); + def runner() + return self.class.runner(); end - def self.modified=(newAffected) - @modified = newAffected; - end - def self.modified() - return @modified; + def branch() + runner().current_branch(); end def modified() - self.class.modified + runner().current_modified(); end def watch(regEx) @@ -66,6 +62,8 @@ def initialize() @expect_clean_exit = true; @exiting = false; + @chdir = File.dirname(runner().current_task_file); + yield(self); valid? From f83e94131d224a73a8dbcdbc20ee0ae8dab649fb Mon Sep 17 00:00:00 2001 From: David Bailey Date: Thu, 26 Jul 2018 20:44:43 +0200 Subject: [PATCH 17/81] Rewrote to use the new integration of the runner class --- Ruby/GitRestart/tests/tc_task.rb | 45 +++++++++++++++++++++++++------- 1 file changed, 35 insertions(+), 10 deletions(-) diff --git a/Ruby/GitRestart/tests/tc_task.rb b/Ruby/GitRestart/tests/tc_task.rb index 2a82611b..d415bfe0 100644 --- a/Ruby/GitRestart/tests/tc_task.rb +++ b/Ruby/GitRestart/tests/tc_task.rb @@ -1,9 +1,27 @@ require_relative "../lib/git-restart/task.rb" +class DummyRunner + attr_accessor :current_branch, :current_commit, :current_modified; + attr_accessor :current_task_file; + attr_accessor :next_tasks; + + def initialize() + @current_branch = ""; + @current_commit = "wkdfaosdo2988a9sd"; + @current_modified = Array.new(); + @current_task_file = ""; + + @next_tasks = Hash.new(); + end +end + class Test_Task < Minitest::Test def setup() `rm /tmp/TEST_FILE_* 2>/dev/null` + + @runner = DummyRunner.new(); + GitRestart::Task.runner = @runner; end def teardown() @@ -82,9 +100,10 @@ def test_complete_execution end def test_chdir + @runner.current_task_file = "/tmp/.gittask"; + @task = GitRestart::Task.new() do |t| t.name = "TestTask"; - t.chdir = "/tmp"; t.targets << "touch TEST_FILE_1"; end @@ -96,23 +115,22 @@ def test_chdir end def test_triggers() - GitRestart::Task.branch ="master"; - GitRestart::Task.modified = ["Tests/Test1/Test.rb"]; + @runner.current_branch = "master"; + @runner.current_modified = ["Tests/Test1/Test.rb"]; + + @runner.current_task_file = "Tests/Test1/.gittask"; @task = GitRestart::Task.new() do |t| - t.chdir = "Tests/Test1"; t.name = "TestTask" t.watch(%r{.*\.rb}); - assert t.triggered; - t.triggered = false; + assert t.triggered? t.watch("Test.rb"); - assert t.triggered; - t.triggered = false; + assert t.triggered? - t.watch("NotTest.rb"); - refute t.triggered; + @runner.current_modified = ["Tests/Test1/NotTested.txt"]; + refute t.triggered? t.on_branches ["master", "dev"]; assert t.active; @@ -120,6 +138,13 @@ def test_triggers() t.on_branches "dev"; refute t.active; + + @runner.current_modified = ["Tests/Test1/.gittask"]; + assert t.triggered? + + @runner.current_modified = nil; + assert t.triggered? end + end end From b875eca70e6e91710b4bfc2ed42aadd7e47278c0 Mon Sep 17 00:00:00 2001 From: David Bailey Date: Thu, 26 Jul 2018 20:44:57 +0200 Subject: [PATCH 18/81] TOWARDS OUR GOAL, FELLOW FRIENDS --- Ruby/GitRestart/lib/git-restart/runner.rb | 72 ++++++++++++++++++++--- 1 file changed, 65 insertions(+), 7 deletions(-) diff --git a/Ruby/GitRestart/lib/git-restart/runner.rb b/Ruby/GitRestart/lib/git-restart/runner.rb index 149cfad2..026eb5d4 100644 --- a/Ruby/GitRestart/lib/git-restart/runner.rb +++ b/Ruby/GitRestart/lib/git-restart/runner.rb @@ -8,26 +8,34 @@ class Runner attr_accessor :repo, :branches, :exclude_branches, :start_on + attr_reader :next_tasks + attr_reader :current_task_file + def current_commit() @git.object("HEAD^").sha; end def current_branch() @git.current_branch(); end + def current_modified() + @current_modified; + end def initialize() - @currentTasks = Hash.new(); - @nextTasks = Hash.new(); + GitRestart::Task.runner = self; + + @current_tasks = Hash.new(); + @next_tasks = Hash.new(); @branches = Array.new(); @exclude_branches = Array.new(); @branchQueue = Queue.new(); - yield(self); - @git = Git.open("."); + yield(self); + @mqtt.subscribe_to "GitHub/#{@repo}" do |data| begin data = JSON.parse(data, symbolize_names: true); @@ -48,12 +56,62 @@ def initialize() autostart(); end + def _stop_all_tasks() + @current_tasks.each do |name, t| + t.stop(); + end + @current_tasks.each do |name, t| + t.join(); + end + @current_tasks.clear(); + end + + def _switch_to(branch, commit = nil) + @git.fetch(); + + if(branch != current_branch()) + _stop_all_tasks(); + end + @git.checkout(branch); + @git.reset_hard(commit) if commit; + + @git.merge("origin/#{start_on}"); + end + + def _generate_next_tasks() + @next_tasks = Hash.new(); + + taskFiles = `find . -iname -nowarn "*.gittask"` + taskFiles.split("\n"); + + taskFiles.each do |t| + t.gsub!(/^\.\//,""); + @current_task_file = t; + + # TODO Add proper error reporting + begin + load(t); + rescue LoadError + puts("File #{t} could not be loaded!"); + rescue GitRestart::TaskValidityError + puts("Task-File #{t} is not configured properly!"); + end + end + end + + def _stop_ + def autostart() return unless @start_on; - @git.fetch(); - @git.checkout(@start_on); - @git.merge("origin/#{@start_on}"); + _switch_to(@start_on); + _generate_next_tasks(); + + @next_tasks.each do |name, t| + next unless t.active + t.start(); + @current_tasks[name] = t; + end end def mqtt=(mqtt) From 04c9a7e7b57830d02ecc5238ce376e8a29ceb186 Mon Sep 17 00:00:00 2001 From: David Bailey Date: Mon, 30 Jul 2018 21:13:38 +0200 Subject: [PATCH 19/81] Unified how tasks are stopped --- Ruby/GitRestart/lib/git-restart/runner.rb | 15 +++++++++------ 1 file changed, 9 insertions(+), 6 deletions(-) diff --git a/Ruby/GitRestart/lib/git-restart/runner.rb b/Ruby/GitRestart/lib/git-restart/runner.rb index 026eb5d4..40ea610a 100644 --- a/Ruby/GitRestart/lib/git-restart/runner.rb +++ b/Ruby/GitRestart/lib/git-restart/runner.rb @@ -56,14 +56,14 @@ def initialize() autostart(); end - def _stop_all_tasks() - @current_tasks.each do |name, t| + def _stop_tasks(taskList) + taskList.each do |name, t| t.stop(); end - @current_tasks.each do |name, t| + taskList.each do |name, t| t.join(); + @current_tasks.delete(name); end - @current_tasks.clear(); end def _switch_to(branch, commit = nil) @@ -76,6 +76,11 @@ def _switch_to(branch, commit = nil) @git.reset_hard(commit) if commit; @git.merge("origin/#{start_on}"); + def _stop_all_tasks() + _stop_tasks(@current_tasks); + end + def _stop_triggered_tasks() + _stop_tasks(@current_tasks.select {|k,v| v.triggered?}); end def _generate_next_tasks() @@ -99,8 +104,6 @@ def _generate_next_tasks() end end - def _stop_ - def autostart() return unless @start_on; From d5f2f098e9817a12b24818d3833c4102ad2c4bba Mon Sep 17 00:00:00 2001 From: David Bailey Date: Mon, 30 Jul 2018 21:13:51 +0200 Subject: [PATCH 20/81] Added a task thread --- Ruby/GitRestart/lib/git-restart/runner.rb | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/Ruby/GitRestart/lib/git-restart/runner.rb b/Ruby/GitRestart/lib/git-restart/runner.rb index 40ea610a..c4194a6f 100644 --- a/Ruby/GitRestart/lib/git-restart/runner.rb +++ b/Ruby/GitRestart/lib/git-restart/runner.rb @@ -54,6 +54,18 @@ def initialize() end autostart(); + _start_task_thread(); + end + + def _start_task_thread() + @taskThread = Thread.new do + loop do + newData = @branchQueue.pop; + + @current_modified = newData[:touched]; + _switch_to(newData[:branch], newData[:commit]); + end + end end def _stop_tasks(taskList) From 45c6aeec4fcba8277725ebe319464e4fddc4d2ae Mon Sep 17 00:00:00 2001 From: David Bailey Date: Mon, 30 Jul 2018 21:14:17 +0200 Subject: [PATCH 21/81] Used _switch_to for all necessary actions --- Ruby/GitRestart/lib/git-restart/runner.rb | 43 ++++++++++++++--------- 1 file changed, 26 insertions(+), 17 deletions(-) diff --git a/Ruby/GitRestart/lib/git-restart/runner.rb b/Ruby/GitRestart/lib/git-restart/runner.rb index c4194a6f..cda62e35 100644 --- a/Ruby/GitRestart/lib/git-restart/runner.rb +++ b/Ruby/GitRestart/lib/git-restart/runner.rb @@ -36,7 +36,7 @@ def initialize() yield(self); - @mqtt.subscribe_to "GitHub/#{@repo}" do |data| + @listenedSub = @mqtt.subscribe_to "GitHub/#{@repo}" do |data| begin data = JSON.parse(data, symbolize_names: true); rescue @@ -77,17 +77,6 @@ def _stop_tasks(taskList) @current_tasks.delete(name); end end - - def _switch_to(branch, commit = nil) - @git.fetch(); - - if(branch != current_branch()) - _stop_all_tasks(); - end - @git.checkout(branch); - @git.reset_hard(commit) if commit; - - @git.merge("origin/#{start_on}"); def _stop_all_tasks() _stop_tasks(@current_tasks); end @@ -116,19 +105,39 @@ def _generate_next_tasks() end end - def autostart() - return unless @start_on; - - _switch_to(@start_on); + def _start_next_tasks() _generate_next_tasks(); @next_tasks.each do |name, t| - next unless t.active + next unless t.active; + next unless t.triggered? + t.start(); @current_tasks[name] = t; end end + def _switch_to(branch, commit = nil) + @git.fetch(); + + if(branch != current_branch()) + _stop_all_tasks(); + else + _stop_triggered_tasks(); + end + @git.checkout(branch); + @git.reset_hard(commit); + + @git.merge("origin/#{branch}"); + + _start_next_tasks(); + end + + def autostart() + return unless @start_on; + @branchQueue << {branch: @start_on}; + end + def mqtt=(mqtt) if(mqtt.is_a? String) @mqtt = MQTT::SubHandler.new(mqtt); From 62b1db482a5a38cb2ece3b3acab196d90db6da16 Mon Sep 17 00:00:00 2001 From: David Bailey Date: Mon, 30 Jul 2018 21:14:24 +0200 Subject: [PATCH 22/81] Test files! :D --- Ruby/GitRestart/TestRunner.gitrun | 5 +++++ Ruby/GitRestart/TestTasks.gittask | 10 ++++++++++ 2 files changed, 15 insertions(+) create mode 100644 Ruby/GitRestart/TestRunner.gitrun create mode 100644 Ruby/GitRestart/TestTasks.gittask diff --git a/Ruby/GitRestart/TestRunner.gitrun b/Ruby/GitRestart/TestRunner.gitrun new file mode 100644 index 00000000..67c2a5df --- /dev/null +++ b/Ruby/GitRestart/TestRunner.gitrun @@ -0,0 +1,5 @@ + +GitRestart::Runner.new do |r| + r.name = "Test Git Runner"; + r.repo = "XasWorks/XasCode"; +end diff --git a/Ruby/GitRestart/TestTasks.gittask b/Ruby/GitRestart/TestTasks.gittask new file mode 100644 index 00000000..74484532 --- /dev/null +++ b/Ruby/GitRestart/TestTasks.gittask @@ -0,0 +1,10 @@ + +GitRestart::Task.new do |t| + t.name = "Failure testing"; + t.report_status = true; + t.active = true; + + t.targets << 'echo "Hello from #{branch}, modified files: #{modified}" > TestOutput.txt' + t.targets << 'sleep 10' + t.targets << 'exit 1' +end From a8c1ab29114fb04f0bf9e1025ba88e0d40e449d9 Mon Sep 17 00:00:00 2001 From: David Bailey Date: Mon, 30 Jul 2018 21:26:35 +0200 Subject: [PATCH 23/81] Fixed wrong require statements --- Ruby/GitRestart/lib/git-restart/runner.rb | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/Ruby/GitRestart/lib/git-restart/runner.rb b/Ruby/GitRestart/lib/git-restart/runner.rb index cda62e35..0ca4f023 100644 --- a/Ruby/GitRestart/lib/git-restart/runner.rb +++ b/Ruby/GitRestart/lib/git-restart/runner.rb @@ -1,6 +1,8 @@ + require 'mqtt/sub_handler' +require 'git' -require_relative "runner.rb" +require_relative "task.rb" module GitRestart class Runner From 23e6914680303f7bbb8e27597c00a5034893d5c4 Mon Sep 17 00:00:00 2001 From: David Bailey Date: Mon, 30 Jul 2018 21:26:55 +0200 Subject: [PATCH 24/81] Test file changes --- Ruby/GitRestart/TestRunner.gitrun | 2 ++ Ruby/GitRestart/TestTasks.gittask | 2 ++ 2 files changed, 4 insertions(+) diff --git a/Ruby/GitRestart/TestRunner.gitrun b/Ruby/GitRestart/TestRunner.gitrun index 67c2a5df..9be56096 100644 --- a/Ruby/GitRestart/TestRunner.gitrun +++ b/Ruby/GitRestart/TestRunner.gitrun @@ -2,4 +2,6 @@ GitRestart::Runner.new do |r| r.name = "Test Git Runner"; r.repo = "XasWorks/XasCode"; + + r.mqtt = "mqtt://xasin.hopto.org"; end diff --git a/Ruby/GitRestart/TestTasks.gittask b/Ruby/GitRestart/TestTasks.gittask index 74484532..f414cd17 100644 --- a/Ruby/GitRestart/TestTasks.gittask +++ b/Ruby/GitRestart/TestTasks.gittask @@ -2,7 +2,9 @@ GitRestart::Task.new do |t| t.name = "Failure testing"; t.report_status = true; + t.active = true; + t.watch(/.*/); t.targets << 'echo "Hello from #{branch}, modified files: #{modified}" > TestOutput.txt' t.targets << 'sleep 10' From 45f2d6a16c0f0849eba392d074c1e315badf9004 Mon Sep 17 00:00:00 2001 From: David Bailey Date: Mon, 30 Jul 2018 21:35:13 +0200 Subject: [PATCH 25/81] Debug messages --- Ruby/GitRestart/lib/git-restart/runner.rb | 8 +++++++- Ruby/GitRestart/lib/git-restart/task.rb | 3 +++ 2 files changed, 10 insertions(+), 1 deletion(-) diff --git a/Ruby/GitRestart/lib/git-restart/runner.rb b/Ruby/GitRestart/lib/git-restart/runner.rb index 0ca4f023..82d8be27 100644 --- a/Ruby/GitRestart/lib/git-restart/runner.rb +++ b/Ruby/GitRestart/lib/git-restart/runner.rb @@ -67,7 +67,7 @@ def _start_task_thread() @current_modified = newData[:touched]; _switch_to(newData[:branch], newData[:commit]); end - end + end.abort_on_exception = true; end def _stop_tasks(taskList) @@ -87,12 +87,14 @@ def _stop_triggered_tasks() end def _generate_next_tasks() + puts "Generating new tasks..." @next_tasks = Hash.new(); taskFiles = `find . -iname -nowarn "*.gittask"` taskFiles.split("\n"); taskFiles.each do |t| + puts "Looking at: #{t}" t.gsub!(/^\.\//,""); @current_task_file = t; @@ -105,11 +107,14 @@ def _generate_next_tasks() puts("Task-File #{t} is not configured properly!"); end end + + puts "Finished loading! Next tasks are: #{@next_tasks}" end def _start_next_tasks() _generate_next_tasks(); + puts "Starting next tasks!" @next_tasks.each do |name, t| next unless t.active; next unless t.triggered? @@ -120,6 +125,7 @@ def _start_next_tasks() end def _switch_to(branch, commit = nil) + puts "Switching to branch: #{branch}, commit: #{commit}" @git.fetch(); if(branch != current_branch()) diff --git a/Ruby/GitRestart/lib/git-restart/task.rb b/Ruby/GitRestart/lib/git-restart/task.rb index 10a85401..5cd6a030 100644 --- a/Ruby/GitRestart/lib/git-restart/task.rb +++ b/Ruby/GitRestart/lib/git-restart/task.rb @@ -106,6 +106,8 @@ def _report_status(status, message = nil) private :_report_status def start() + puts "Starting Task: #{@name}" + @executionThread = Thread.new do _report_status(:pending); @@ -135,6 +137,7 @@ def start() end def stop() + puts "Stopping Task: #{@name}" return if @signal.nil? @statuschange_mutex.synchronize { From 6358786e4d58b4f308b567505cf0a28ba3fd5a8f Mon Sep 17 00:00:00 2001 From: David Bailey Date: Mon, 30 Jul 2018 21:36:54 +0200 Subject: [PATCH 26/81] MOAR debug >:C --- Ruby/GitRestart/TestRunner.gitrun | 4 +++- Ruby/GitRestart/lib/git-restart/runner.rb | 1 + 2 files changed, 4 insertions(+), 1 deletion(-) diff --git a/Ruby/GitRestart/TestRunner.gitrun b/Ruby/GitRestart/TestRunner.gitrun index 9be56096..e0bed100 100644 --- a/Ruby/GitRestart/TestRunner.gitrun +++ b/Ruby/GitRestart/TestRunner.gitrun @@ -1,5 +1,7 @@ -GitRestart::Runner.new do |r| +require_relative "lib/git-restart/runner.rb" + +$runner = GitRestart::Runner.new do |r| r.name = "Test Git Runner"; r.repo = "XasWorks/XasCode"; diff --git a/Ruby/GitRestart/lib/git-restart/runner.rb b/Ruby/GitRestart/lib/git-restart/runner.rb index 82d8be27..c1616288 100644 --- a/Ruby/GitRestart/lib/git-restart/runner.rb +++ b/Ruby/GitRestart/lib/git-restart/runner.rb @@ -39,6 +39,7 @@ def initialize() yield(self); @listenedSub = @mqtt.subscribe_to "GitHub/#{@repo}" do |data| + puts "Received data: #{data}" begin data = JSON.parse(data, symbolize_names: true); rescue From 3d648053f924232290a6caba89ae731c7355975a Mon Sep 17 00:00:00 2001 From: David Bailey Date: Mon, 30 Jul 2018 21:39:44 +0200 Subject: [PATCH 27/81] 'scuse the debug >.> --- Ruby/GitRestart/lib/git-restart/runner.rb | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/Ruby/GitRestart/lib/git-restart/runner.rb b/Ruby/GitRestart/lib/git-restart/runner.rb index c1616288..50867fdb 100644 --- a/Ruby/GitRestart/lib/git-restart/runner.rb +++ b/Ruby/GitRestart/lib/git-restart/runner.rb @@ -46,6 +46,8 @@ def initialize() next; end + puts "Processing data #{data}" + next unless data[:branch]; if(@branches) next unless @branches.include? data[:branch]; @@ -53,6 +55,8 @@ def initialize() next if @exclude_branches.include? data[:branch]; end + puts "Queueing data!" + @branchQueue << data; end @@ -65,6 +69,8 @@ def _start_task_thread() loop do newData = @branchQueue.pop; + puts "Popped data: #{newData}" + @current_modified = newData[:touched]; _switch_to(newData[:branch], newData[:commit]); end From c521f86eaaecc61ee37f6473c4e60541ceded7f9 Mon Sep 17 00:00:00 2001 From: David Bailey Date: Mon, 30 Jul 2018 21:40:59 +0200 Subject: [PATCH 28/81] Fixed check of `if(@branches)` to `not branches.empty?` --- Ruby/GitRestart/lib/git-restart/runner.rb | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/Ruby/GitRestart/lib/git-restart/runner.rb b/Ruby/GitRestart/lib/git-restart/runner.rb index 50867fdb..549d4e91 100644 --- a/Ruby/GitRestart/lib/git-restart/runner.rb +++ b/Ruby/GitRestart/lib/git-restart/runner.rb @@ -49,9 +49,9 @@ def initialize() puts "Processing data #{data}" next unless data[:branch]; - if(@branches) + if(not @branches.empty?) next unless @branches.include? data[:branch]; - elsif(@exclude_branches) + elsif(not @exclude_branches.empty?) next if @exclude_branches.include? data[:branch]; end From 5b837f03c866184888e9f59af777ee914602d52e Mon Sep 17 00:00:00 2001 From: David Bailey Date: Mon, 30 Jul 2018 21:43:06 +0200 Subject: [PATCH 29/81] Fixed wrong argument order for `find` --- Ruby/GitRestart/lib/git-restart/runner.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Ruby/GitRestart/lib/git-restart/runner.rb b/Ruby/GitRestart/lib/git-restart/runner.rb index 549d4e91..fa2298dc 100644 --- a/Ruby/GitRestart/lib/git-restart/runner.rb +++ b/Ruby/GitRestart/lib/git-restart/runner.rb @@ -97,7 +97,7 @@ def _generate_next_tasks() puts "Generating new tasks..." @next_tasks = Hash.new(); - taskFiles = `find . -iname -nowarn "*.gittask"` + taskFiles = `find ./ -nowarn -iname "*.gittask"` taskFiles.split("\n"); taskFiles.each do |t| From 07355fd3799092e71d73f10977082852c13fec1f Mon Sep 17 00:00:00 2001 From: David Bailey Date: Mon, 30 Jul 2018 21:45:05 +0200 Subject: [PATCH 30/81] Fixed forgotten `split!` --- Ruby/GitRestart/lib/git-restart/runner.rb | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/Ruby/GitRestart/lib/git-restart/runner.rb b/Ruby/GitRestart/lib/git-restart/runner.rb index fa2298dc..b7b6f710 100644 --- a/Ruby/GitRestart/lib/git-restart/runner.rb +++ b/Ruby/GitRestart/lib/git-restart/runner.rb @@ -98,9 +98,7 @@ def _generate_next_tasks() @next_tasks = Hash.new(); taskFiles = `find ./ -nowarn -iname "*.gittask"` - taskFiles.split("\n"); - - taskFiles.each do |t| + taskFiles.split("\n").each do |t| puts "Looking at: #{t}" t.gsub!(/^\.\//,""); @current_task_file = t; From 43402b09b1bcc94befb4cf483d4ab9c0dff1f39a Mon Sep 17 00:00:00 2001 From: David Bailey Date: Mon, 30 Jul 2018 21:47:31 +0200 Subject: [PATCH 31/81] Slight tweak to the TestTask file --- Ruby/GitRestart/TestTasks.gittask | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Ruby/GitRestart/TestTasks.gittask b/Ruby/GitRestart/TestTasks.gittask index f414cd17..a7487400 100644 --- a/Ruby/GitRestart/TestTasks.gittask +++ b/Ruby/GitRestart/TestTasks.gittask @@ -6,7 +6,7 @@ GitRestart::Task.new do |t| t.active = true; t.watch(/.*/); - t.targets << 'echo "Hello from #{branch}, modified files: #{modified}" > TestOutput.txt' + t.targets << "echo Hello from #{branch}, modified files: #{modified} > TestOutput.txt" t.targets << 'sleep 10' t.targets << 'exit 1' end From 0c10807ad4d33f3f1f06513ac0a6ba47d44c4020 Mon Sep 17 00:00:00 2001 From: David Bailey Date: Mon, 30 Jul 2018 21:49:49 +0200 Subject: [PATCH 32/81] Fixed missing PID-clear after task completion --- Ruby/GitRestart/lib/git-restart/task.rb | 1 + 1 file changed, 1 insertion(+) diff --git a/Ruby/GitRestart/lib/git-restart/task.rb b/Ruby/GitRestart/lib/git-restart/task.rb index 5cd6a030..f9f0d9af 100644 --- a/Ruby/GitRestart/lib/git-restart/task.rb +++ b/Ruby/GitRestart/lib/git-restart/task.rb @@ -123,6 +123,7 @@ def start() } status = Process.wait2(@currentPID)[1]; + @currentPID = nil; @lastStatus = status.exitstatus(); break unless @lastStatus == 0; From edf50b3171aade3a1bed6c6eccdaff65c3d29272 Mon Sep 17 00:00:00 2001 From: David Bailey Date: Mon, 30 Jul 2018 21:50:36 +0200 Subject: [PATCH 33/81] Fixed wrong hash index used for commit selection --- Ruby/GitRestart/lib/git-restart/runner.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Ruby/GitRestart/lib/git-restart/runner.rb b/Ruby/GitRestart/lib/git-restart/runner.rb index b7b6f710..232d3149 100644 --- a/Ruby/GitRestart/lib/git-restart/runner.rb +++ b/Ruby/GitRestart/lib/git-restart/runner.rb @@ -72,7 +72,7 @@ def _start_task_thread() puts "Popped data: #{newData}" @current_modified = newData[:touched]; - _switch_to(newData[:branch], newData[:commit]); + _switch_to(newData[:branch], newData[:head_commit]); end end.abort_on_exception = true; end From ac3f2dc2b88898dc4351a44a6b72e587d38482b8 Mon Sep 17 00:00:00 2001 From: David Bailey Date: Mon, 30 Jul 2018 21:51:23 +0200 Subject: [PATCH 34/81] GitTask fixes --- Ruby/GitRestart/TestOutput.txt | 1 + Ruby/GitRestart/TestTasks.gittask | 2 +- 2 files changed, 2 insertions(+), 1 deletion(-) create mode 100644 Ruby/GitRestart/TestOutput.txt diff --git a/Ruby/GitRestart/TestOutput.txt b/Ruby/GitRestart/TestOutput.txt new file mode 100644 index 00000000..85feedec --- /dev/null +++ b/Ruby/GitRestart/TestOutput.txt @@ -0,0 +1 @@ +Hello from #{branch}, modified files: #{modified} diff --git a/Ruby/GitRestart/TestTasks.gittask b/Ruby/GitRestart/TestTasks.gittask index a7487400..efdb27af 100644 --- a/Ruby/GitRestart/TestTasks.gittask +++ b/Ruby/GitRestart/TestTasks.gittask @@ -6,7 +6,7 @@ GitRestart::Task.new do |t| t.active = true; t.watch(/.*/); - t.targets << "echo Hello from #{branch}, modified files: #{modified} > TestOutput.txt" + t.targets << "echo Hello from #{t.branch}, modified files: #{t.modified} > TestOutput.txt" t.targets << 'sleep 10' t.targets << 'exit 1' end From 29f57b54fd39405557569b09515e8427f7d5084a Mon Sep 17 00:00:00 2001 From: David Bailey Date: Mon, 30 Jul 2018 21:52:19 +0200 Subject: [PATCH 35/81] Whoopsie, shouldn't have been added! --- Ruby/GitRestart/TestOutput.txt | 1 - 1 file changed, 1 deletion(-) delete mode 100644 Ruby/GitRestart/TestOutput.txt diff --git a/Ruby/GitRestart/TestOutput.txt b/Ruby/GitRestart/TestOutput.txt deleted file mode 100644 index 85feedec..00000000 --- a/Ruby/GitRestart/TestOutput.txt +++ /dev/null @@ -1 +0,0 @@ -Hello from #{branch}, modified files: #{modified} From d711a7dffbd5284cabe46be2f64d2f072c032354 Mon Sep 17 00:00:00 2001 From: David Bailey Date: Thu, 2 Aug 2018 21:43:42 +0200 Subject: [PATCH 36/81] Tried adding a self-testing script --- Ruby/GitRestart/TestTasks.gittask | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/Ruby/GitRestart/TestTasks.gittask b/Ruby/GitRestart/TestTasks.gittask index efdb27af..310fca2b 100644 --- a/Ruby/GitRestart/TestTasks.gittask +++ b/Ruby/GitRestart/TestTasks.gittask @@ -10,3 +10,13 @@ GitRestart::Task.new do |t| t.targets << 'sleep 10' t.targets << 'exit 1' end + +GitTask::Task.new do |t| + t.name = "Git-Task-Tester" + t.active = true; + + t.watch(/task.rb/); + t.watch(/tc_task.rb/); + + t.targets << "ruby tests/tc_task.rb"; +end From 701fe111c1cc3af92295f6643a8cb608ff663583 Mon Sep 17 00:00:00 2001 From: David Bailey Date: Thu, 2 Aug 2018 21:47:03 +0200 Subject: [PATCH 37/81] Changed error type fetch --- Ruby/GitRestart/lib/git-restart/runner.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Ruby/GitRestart/lib/git-restart/runner.rb b/Ruby/GitRestart/lib/git-restart/runner.rb index 232d3149..5d08d3e5 100644 --- a/Ruby/GitRestart/lib/git-restart/runner.rb +++ b/Ruby/GitRestart/lib/git-restart/runner.rb @@ -106,7 +106,7 @@ def _generate_next_tasks() # TODO Add proper error reporting begin load(t); - rescue LoadError + rescue ScriptError, StandardError puts("File #{t} could not be loaded!"); rescue GitRestart::TaskValidityError puts("Task-File #{t} is not configured properly!"); From 5c7ef0392fc7a501288be6bfefdeed2dd4cc766b Mon Sep 17 00:00:00 2001 From: David Bailey Date: Thu, 2 Aug 2018 21:48:03 +0200 Subject: [PATCH 38/81] Testing with proper task name >.> --- Ruby/GitRestart/TestTasks.gittask | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Ruby/GitRestart/TestTasks.gittask b/Ruby/GitRestart/TestTasks.gittask index 310fca2b..464f8d3e 100644 --- a/Ruby/GitRestart/TestTasks.gittask +++ b/Ruby/GitRestart/TestTasks.gittask @@ -11,7 +11,7 @@ GitRestart::Task.new do |t| t.targets << 'exit 1' end -GitTask::Task.new do |t| +GitRestart::Task.new do |t| t.name = "Git-Task-Tester" t.active = true; From d3600a0b932516b6b2d38a83d1a3e2c8beea14e8 Mon Sep 17 00:00:00 2001 From: David Bailey Date: Thu, 2 Aug 2018 22:08:48 +0200 Subject: [PATCH 39/81] Forgot to enable error reporting --- Ruby/GitRestart/TestTasks.gittask | 1 + 1 file changed, 1 insertion(+) diff --git a/Ruby/GitRestart/TestTasks.gittask b/Ruby/GitRestart/TestTasks.gittask index 464f8d3e..0a704a75 100644 --- a/Ruby/GitRestart/TestTasks.gittask +++ b/Ruby/GitRestart/TestTasks.gittask @@ -14,6 +14,7 @@ end GitRestart::Task.new do |t| t.name = "Git-Task-Tester" t.active = true; + t.report_status = true; t.watch(/task.rb/); t.watch(/tc_task.rb/); From 08bb3cc9a78b25cc72f546045570b6ed21a60316 Mon Sep 17 00:00:00 2001 From: David Bailey Date: Thu, 2 Aug 2018 22:10:58 +0200 Subject: [PATCH 40/81] Small changes, we'll see --- Ruby/GitRestart/lib/git-restart/task.rb | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/Ruby/GitRestart/lib/git-restart/task.rb b/Ruby/GitRestart/lib/git-restart/task.rb index f9f0d9af..022b9b60 100644 --- a/Ruby/GitRestart/lib/git-restart/task.rb +++ b/Ruby/GitRestart/lib/git-restart/task.rb @@ -101,7 +101,7 @@ def _report_status(status, message = nil) @status_message = "" return unless @report_status - print "Task #{@name} assumed a new status: #{status}#{message ? " MSG:#{message}" : ""}" + puts "Task #{@name} assumed a new status: #{status}#{message ? " MSG:#{message}" : ""}" end private :_report_status @@ -115,7 +115,7 @@ def start() @statuschange_mutex.synchronize { break if @exiting options = { - [:in, :out, :err] => "/dev/null" + #[:in, :out, :err] => "/dev/null" } options[:chdir] = @chdir if @chdir From c469828588ac38978880dc76cd45356c6ac83094 Mon Sep 17 00:00:00 2001 From: David Bailey Date: Thu, 2 Aug 2018 22:14:00 +0200 Subject: [PATCH 41/81] Forgot to initialize lastStatus --- Ruby/GitRestart/lib/git-restart/task.rb | 3 +++ 1 file changed, 3 insertions(+) diff --git a/Ruby/GitRestart/lib/git-restart/task.rb b/Ruby/GitRestart/lib/git-restart/task.rb index 022b9b60..46a3afac 100644 --- a/Ruby/GitRestart/lib/git-restart/task.rb +++ b/Ruby/GitRestart/lib/git-restart/task.rb @@ -62,6 +62,7 @@ def initialize() @expect_clean_exit = true; @exiting = false; + @lastStatus = 0; @chdir = File.dirname(runner().current_task_file); yield(self); @@ -101,6 +102,8 @@ def _report_status(status, message = nil) @status_message = "" return unless @report_status + + runner().update_status(@name, status, message); puts "Task #{@name} assumed a new status: #{status}#{message ? " MSG:#{message}" : ""}" end private :_report_status From cb7ba3bb5db606e10cbd35b113f3e74e16d6193c Mon Sep 17 00:00:00 2001 From: David Bailey Date: Thu, 2 Aug 2018 22:14:34 +0200 Subject: [PATCH 42/81] Task needn't run if no targets are specified --- Ruby/GitRestart/lib/git-restart/task.rb | 1 + 1 file changed, 1 insertion(+) diff --git a/Ruby/GitRestart/lib/git-restart/task.rb b/Ruby/GitRestart/lib/git-restart/task.rb index 46a3afac..df87f71e 100644 --- a/Ruby/GitRestart/lib/git-restart/task.rb +++ b/Ruby/GitRestart/lib/git-restart/task.rb @@ -111,6 +111,7 @@ def _report_status(status, message = nil) def start() puts "Starting Task: #{@name}" + return if @targets.empty? @executionThread = Thread.new do _report_status(:pending); From 4eea66f1a2d621fce6d4fdd978798ada93c128ae Mon Sep 17 00:00:00 2001 From: David Bailey Date: Thu, 2 Aug 2018 22:22:18 +0200 Subject: [PATCH 43/81] Improvements to the trigger RegExp mechanism --- Ruby/GitRestart/lib/git-restart/task.rb | 8 +++----- 1 file changed, 3 insertions(+), 5 deletions(-) diff --git a/Ruby/GitRestart/lib/git-restart/task.rb b/Ruby/GitRestart/lib/git-restart/task.rb index df87f71e..352475c3 100644 --- a/Ruby/GitRestart/lib/git-restart/task.rb +++ b/Ruby/GitRestart/lib/git-restart/task.rb @@ -38,10 +38,6 @@ def watch(regEx) regEx = Regexp.quote(regEx); end - if(@chdir) - regEx = "#{Regexp.quote(@chdir)}/#{regEx.to_s}"; - end - @watched << Regexp.new(regEx); end @@ -81,7 +77,8 @@ def triggered? @watched.each do |regEx| modified().each do |f| - return true if f =~ regEx; + next unless f =~ /#{Regexp.quote(@chdir)}(.*)/ + return true if $1 =~ regEx; end end @@ -112,6 +109,7 @@ def start() puts "Starting Task: #{@name}" return if @targets.empty? + @executionThread = Thread.new do _report_status(:pending); From 56b4fdc7e8533eb7e4e6f55d42ce4bd5c31e98c7 Mon Sep 17 00:00:00 2001 From: David Bailey Date: Thu, 2 Aug 2018 22:41:32 +0200 Subject: [PATCH 44/81] Added abort_on_exception and a dummy "update_status" --- Ruby/GitRestart/lib/git-restart/runner.rb | 3 +++ Ruby/GitRestart/lib/git-restart/task.rb | 5 +++-- Ruby/GitRestart/tests/tc_task.rb | 3 +++ 3 files changed, 9 insertions(+), 2 deletions(-) diff --git a/Ruby/GitRestart/lib/git-restart/runner.rb b/Ruby/GitRestart/lib/git-restart/runner.rb index 5d08d3e5..006b6262 100644 --- a/Ruby/GitRestart/lib/git-restart/runner.rb +++ b/Ruby/GitRestart/lib/git-restart/runner.rb @@ -64,6 +64,9 @@ def initialize() _start_task_thread(); end + def update_status(name, newStatus, message = nil) + end + def _start_task_thread() @taskThread = Thread.new do loop do diff --git a/Ruby/GitRestart/lib/git-restart/task.rb b/Ruby/GitRestart/lib/git-restart/task.rb index 352475c3..662f59c7 100644 --- a/Ruby/GitRestart/lib/git-restart/task.rb +++ b/Ruby/GitRestart/lib/git-restart/task.rb @@ -52,7 +52,6 @@ def initialize() @targets = Array.new(); @watched = Array.new(); - watch(runner().current_task_file); @signal = "INT" @expect_clean_exit = true; @@ -61,6 +60,8 @@ def initialize() @lastStatus = 0; @chdir = File.dirname(runner().current_task_file); + watch(runner().current_task_file); + yield(self); valid? @@ -136,7 +137,7 @@ def start() elsif(!@exiting || @expect_clean_exit) _report_status(:failure); end - end + end.abort_on_exception = true; end def stop() diff --git a/Ruby/GitRestart/tests/tc_task.rb b/Ruby/GitRestart/tests/tc_task.rb index d415bfe0..21f20e43 100644 --- a/Ruby/GitRestart/tests/tc_task.rb +++ b/Ruby/GitRestart/tests/tc_task.rb @@ -14,6 +14,9 @@ def initialize() @next_tasks = Hash.new(); end + + def update_status(*args) + end end class Test_Task < Minitest::Test From abda99f7a867db8cb8b80d5a83770911411d0252 Mon Sep 17 00:00:00 2001 From: David Bailey Date: Thu, 2 Aug 2018 22:43:33 +0200 Subject: [PATCH 45/81] Forgot to require minitest/autorun --- Ruby/GitRestart/TestOutput.txt | 1 + 1 file changed, 1 insertion(+) create mode 100644 Ruby/GitRestart/TestOutput.txt diff --git a/Ruby/GitRestart/TestOutput.txt b/Ruby/GitRestart/TestOutput.txt new file mode 100644 index 00000000..ad2a1f55 --- /dev/null +++ b/Ruby/GitRestart/TestOutput.txt @@ -0,0 +1 @@ +Hello from GIT_RESTART, modified files: [Ruby/GitRestart/TestOutput.txt] From 3f04f83ed3f4042dd920a0447b1d4003450c6330 Mon Sep 17 00:00:00 2001 From: David Bailey Date: Thu, 2 Aug 2018 22:45:52 +0200 Subject: [PATCH 46/81] Why wasn't this staged o.o' --- Ruby/GitRestart/lib/git-restart/task.rb | 3 ++- Ruby/GitRestart/tests/tc_task.rb | 1 + 2 files changed, 3 insertions(+), 1 deletion(-) diff --git a/Ruby/GitRestart/lib/git-restart/task.rb b/Ruby/GitRestart/lib/git-restart/task.rb index 662f59c7..6df687ae 100644 --- a/Ruby/GitRestart/lib/git-restart/task.rb +++ b/Ruby/GitRestart/lib/git-restart/task.rb @@ -137,7 +137,8 @@ def start() elsif(!@exiting || @expect_clean_exit) _report_status(:failure); end - end.abort_on_exception = true; + end + @executionThread.abort_on_exception = true; end def stop() diff --git a/Ruby/GitRestart/tests/tc_task.rb b/Ruby/GitRestart/tests/tc_task.rb index 21f20e43..52c87d40 100644 --- a/Ruby/GitRestart/tests/tc_task.rb +++ b/Ruby/GitRestart/tests/tc_task.rb @@ -1,5 +1,6 @@ require_relative "../lib/git-restart/task.rb" +require 'minitest/autorun' class DummyRunner attr_accessor :current_branch, :current_commit, :current_modified; From 8df629b13e8961892aef79a950cacce54a0a7c98 Mon Sep 17 00:00:00 2001 From: David Bailey Date: Thu, 2 Aug 2018 22:47:58 +0200 Subject: [PATCH 47/81] Reenabled log redirection --- Ruby/GitRestart/lib/git-restart/task.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Ruby/GitRestart/lib/git-restart/task.rb b/Ruby/GitRestart/lib/git-restart/task.rb index 6df687ae..fcfad62b 100644 --- a/Ruby/GitRestart/lib/git-restart/task.rb +++ b/Ruby/GitRestart/lib/git-restart/task.rb @@ -118,7 +118,7 @@ def start() @statuschange_mutex.synchronize { break if @exiting options = { - #[:in, :out, :err] => "/dev/null" + [:in, :out, :err] => "/dev/null" } options[:chdir] = @chdir if @chdir From bff440d1021a131cc666d5e070dc1d1df015add3 Mon Sep 17 00:00:00 2001 From: David Bailey Date: Thu, 2 Aug 2018 22:53:41 +0200 Subject: [PATCH 48/81] Small delay might improve console output --- Ruby/GitRestart/lib/git-restart/task.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Ruby/GitRestart/lib/git-restart/task.rb b/Ruby/GitRestart/lib/git-restart/task.rb index fcfad62b..35378a10 100644 --- a/Ruby/GitRestart/lib/git-restart/task.rb +++ b/Ruby/GitRestart/lib/git-restart/task.rb @@ -110,8 +110,8 @@ def start() puts "Starting Task: #{@name}" return if @targets.empty? - @executionThread = Thread.new do + sleep 0.01 _report_status(:pending); @targets.each do |target| From e52ac89f84c697f3f53c43fcef66d3f524335aed Mon Sep 17 00:00:00 2001 From: David Bailey Date: Thu, 2 Aug 2018 22:55:55 +0200 Subject: [PATCH 49/81] This might work a bit better >.< --- Ruby/GitRestart/lib/git-restart/task.rb | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/Ruby/GitRestart/lib/git-restart/task.rb b/Ruby/GitRestart/lib/git-restart/task.rb index 35378a10..46d9b1f8 100644 --- a/Ruby/GitRestart/lib/git-restart/task.rb +++ b/Ruby/GitRestart/lib/git-restart/task.rb @@ -110,8 +110,9 @@ def start() puts "Starting Task: #{@name}" return if @targets.empty? + sleep 0.01 + @executionThread = Thread.new do - sleep 0.01 _report_status(:pending); @targets.each do |target| From 0d413e2010ff3fef626b557f228de098095322d7 Mon Sep 17 00:00:00 2001 From: David Bailey Date: Thu, 2 Aug 2018 23:02:16 +0200 Subject: [PATCH 50/81] We only need the basename here, not the full name! --- Ruby/GitRestart/lib/git-restart/task.rb | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/Ruby/GitRestart/lib/git-restart/task.rb b/Ruby/GitRestart/lib/git-restart/task.rb index 46d9b1f8..8cc276d8 100644 --- a/Ruby/GitRestart/lib/git-restart/task.rb +++ b/Ruby/GitRestart/lib/git-restart/task.rb @@ -60,7 +60,7 @@ def initialize() @lastStatus = 0; @chdir = File.dirname(runner().current_task_file); - watch(runner().current_task_file); + watch(File.basename(runner().current_task_file)); yield(self); @@ -111,7 +111,7 @@ def start() return if @targets.empty? sleep 0.01 - + @executionThread = Thread.new do _report_status(:pending); From c47f18248071e661449f6e450205945d0d92f1a0 Mon Sep 17 00:00:00 2001 From: Xasin Date: Mon, 6 Aug 2018 20:03:47 +0200 Subject: [PATCH 51/81] Added a first version of the git-restart executable --- Ruby/GitRestart/bin/git-restart | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/Ruby/GitRestart/bin/git-restart b/Ruby/GitRestart/bin/git-restart index 9247d21f..dd4f665e 100644 --- a/Ruby/GitRestart/bin/git-restart +++ b/Ruby/GitRestart/bin/git-restart @@ -1,3 +1,15 @@ #!/usr/bin/ruby require 'mqtt/sub_handler.rb' +require 'git-restart/runner.rb' + +puts "Starting runner ..." + +target = ARGV[0] +target ||= ".gitrun" + +raise ArgumentError, "No valid task file specified!" unless File.exist? target + +load target + +GitRestart::Task.runner.mqtt.lockAndListen From 6f340f2169c0e969b9fb32ed29919c3daf4d34b9 Mon Sep 17 00:00:00 2001 From: Xasin Date: Mon, 6 Aug 2018 20:04:25 +0200 Subject: [PATCH 52/81] Gemspec update to include executable --- Ruby/GitRestart/git-restart.gemspec | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/Ruby/GitRestart/git-restart.gemspec b/Ruby/GitRestart/git-restart.gemspec index f3d31fae..073960e3 100644 --- a/Ruby/GitRestart/git-restart.gemspec +++ b/Ruby/GitRestart/git-restart.gemspec @@ -1,6 +1,6 @@ Gem::Specification.new do |s| s.name = 'git-restart' - s.version = '0.0.1' + s.version = '0.0.6-dev' s.summary = '(Re)start scripts and monitor them on a GitHub push' s.description = 'This gem can be used to (re)start scripts whenever a GitHub push event is recorded. The exit status of scripts can be monitored, and a failure can be sent back, making this capable of running simple tests too!' @@ -10,12 +10,16 @@ The exit status of scripts can be monitored, and a failure can be sent back, mak 'lib/git-restart/runner.rb', 'lib/git-restart/task.rb'] + s.executables << 'git-restart' + + s.homepage = 'https://github.com/XasWorks/XasCode/tree/master/Ruby/GitRestart' s.license = 'GPL-3.0' - s.add_runtime_dependency "mqtt-sub_handler", "~> 1.0" + s.add_runtime_dependency "mqtt-sub_handler", "~> 0.1" s.add_runtime_dependency "git", "~> 1.4" + s.add_runtime_dependency "octokit", "~> 4.0" s.add_development_dependency "minitest" s.add_development_dependency "guard" From 9360566be50bbb772c60ef973bf5d2b1dad6f2b3 Mon Sep 17 00:00:00 2001 From: Xasin Date: Mon, 6 Aug 2018 20:05:16 +0200 Subject: [PATCH 53/81] Improved usage of the git gem --- Ruby/GitRestart/lib/git-restart/runner.rb | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/Ruby/GitRestart/lib/git-restart/runner.rb b/Ruby/GitRestart/lib/git-restart/runner.rb index 006b6262..3ae8b7eb 100644 --- a/Ruby/GitRestart/lib/git-restart/runner.rb +++ b/Ruby/GitRestart/lib/git-restart/runner.rb @@ -135,6 +135,10 @@ def _start_next_tasks() def _switch_to(branch, commit = nil) puts "Switching to branch: #{branch}, commit: #{commit}" @git.fetch(); + begin + @git.fetch(); + rescue + end if(branch != current_branch()) _stop_all_tasks(); @@ -142,10 +146,10 @@ def _switch_to(branch, commit = nil) _stop_triggered_tasks(); end @git.checkout(branch); - @git.reset_hard(commit); - @git.merge("origin/#{branch}"); + @git.reset_hard(commit); + _start_next_tasks(); end From 78b4bb4026b287b321a95477d49014595d1b0e3d Mon Sep 17 00:00:00 2001 From: Xasin Date: Mon, 6 Aug 2018 20:06:10 +0200 Subject: [PATCH 54/81] Slowly removing debugs :D --- Ruby/GitRestart/lib/git-restart/runner.rb | 15 ++++----------- 1 file changed, 4 insertions(+), 11 deletions(-) diff --git a/Ruby/GitRestart/lib/git-restart/runner.rb b/Ruby/GitRestart/lib/git-restart/runner.rb index 3ae8b7eb..a2bb5221 100644 --- a/Ruby/GitRestart/lib/git-restart/runner.rb +++ b/Ruby/GitRestart/lib/git-restart/runner.rb @@ -39,15 +39,12 @@ def initialize() yield(self); @listenedSub = @mqtt.subscribe_to "GitHub/#{@repo}" do |data| - puts "Received data: #{data}" begin data = JSON.parse(data, symbolize_names: true); rescue next; end - puts "Processing data #{data}" - next unless data[:branch]; if(not @branches.empty?) next unless @branches.include? data[:branch]; @@ -55,8 +52,6 @@ def initialize() next if @exclude_branches.include? data[:branch]; end - puts "Queueing data!" - @branchQueue << data; end @@ -72,8 +67,6 @@ def _start_task_thread() loop do newData = @branchQueue.pop; - puts "Popped data: #{newData}" - @current_modified = newData[:touched]; _switch_to(newData[:branch], newData[:head_commit]); end @@ -116,13 +109,13 @@ def _generate_next_tasks() end end - puts "Finished loading! Next tasks are: #{@next_tasks}" + puts "Finished loading! Next tasks: #{@next_tasks.keys}" end def _start_next_tasks() _generate_next_tasks(); - puts "Starting next tasks!" + puts "\n\nStarting next tasks!" @next_tasks.each do |name, t| next unless t.active; next unless t.triggered? @@ -133,8 +126,8 @@ def _start_next_tasks() end def _switch_to(branch, commit = nil) - puts "Switching to branch: #{branch}, commit: #{commit}" - @git.fetch(); + puts "\nSwitching to branch: #{branch}#{commit ? ",commit: #{commit}" : ""}" + begin @git.fetch(); rescue From 3e6aae7758664c69097a5e0e6513d461abc0f829 Mon Sep 17 00:00:00 2001 From: Xasin Date: Mon, 6 Aug 2018 20:06:24 +0200 Subject: [PATCH 55/81] Adding support for octokit --- Ruby/GitRestart/lib/git-restart/runner.rb | 17 ++++++++++++++++- 1 file changed, 16 insertions(+), 1 deletion(-) diff --git a/Ruby/GitRestart/lib/git-restart/runner.rb b/Ruby/GitRestart/lib/git-restart/runner.rb index a2bb5221..6c910659 100644 --- a/Ruby/GitRestart/lib/git-restart/runner.rb +++ b/Ruby/GitRestart/lib/git-restart/runner.rb @@ -1,6 +1,8 @@ require 'mqtt/sub_handler' + require 'git' +require 'octokit' require_relative "task.rb" @@ -13,6 +15,9 @@ class Runner attr_reader :next_tasks attr_reader :current_task_file + attr_reader :mqtt + attr_accessor :octokit + def current_commit() @git.object("HEAD^").sha; end @@ -60,6 +65,15 @@ def initialize() end def update_status(name, newStatus, message = nil) + return unless @octokit; + + begin + @octokit.create_status(@repo, current_commit, newStatus, { + context: "#{@name}/#{name}".gsub(" ", "_"), + description: message, + }) + rescue + end end def _start_task_thread() @@ -99,12 +113,13 @@ def _generate_next_tasks() t.gsub!(/^\.\//,""); @current_task_file = t; - # TODO Add proper error reporting 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 From 80f83ddca484e49a15f9e159f1a004f515fc2e7c Mon Sep 17 00:00:00 2001 From: Xasin Date: Mon, 6 Aug 2018 20:06:56 +0200 Subject: [PATCH 56/81] Added ci_task for tasks that shall always run --- Ruby/GitRestart/lib/git-restart/task.rb | 2 ++ 1 file changed, 2 insertions(+) diff --git a/Ruby/GitRestart/lib/git-restart/task.rb b/Ruby/GitRestart/lib/git-restart/task.rb index 8cc276d8..43df9c49 100644 --- a/Ruby/GitRestart/lib/git-restart/task.rb +++ b/Ruby/GitRestart/lib/git-restart/task.rb @@ -9,6 +9,7 @@ class Task attr_accessor :signal attr_accessor :expect_clean_exit attr_accessor :report_status + attr_accessor :ci_task attr_accessor :name, :status_file attr_accessor :active @@ -75,6 +76,7 @@ def initialize() def triggered? return true if modified().nil? + return true if @ci_task @watched.each do |regEx| modified().each do |f| From 91213bc0216461e937bb0dbeded5a731ff3e8fa2 Mon Sep 17 00:00:00 2001 From: Xasin Date: Mon, 6 Aug 2018 20:07:13 +0200 Subject: [PATCH 57/81] Improved restart behavior --- Ruby/GitRestart/lib/git-restart/task.rb | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) diff --git a/Ruby/GitRestart/lib/git-restart/task.rb b/Ruby/GitRestart/lib/git-restart/task.rb index 43df9c49..b34ba44d 100644 --- a/Ruby/GitRestart/lib/git-restart/task.rb +++ b/Ruby/GitRestart/lib/git-restart/task.rb @@ -111,8 +111,10 @@ def _report_status(status, message = nil) def start() puts "Starting Task: #{@name}" - return if @targets.empty? - sleep 0.01 + if @targets.empty? + _report_status(:success, "No tasks to run!"); + return + end @executionThread = Thread.new do _report_status(:pending); @@ -121,7 +123,7 @@ def start() @statuschange_mutex.synchronize { break if @exiting options = { - [:in, :out, :err] => "/dev/null" + [:out, :err] => "/dev/null" } options[:chdir] = @chdir if @chdir @@ -142,6 +144,8 @@ def start() end end @executionThread.abort_on_exception = true; + + sleep 0.01 end def stop() From cd3101f15616a724a0282459cb75d9124b3d99ca Mon Sep 17 00:00:00 2001 From: Xasin Date: Mon, 6 Aug 2018 20:07:48 +0200 Subject: [PATCH 58/81] Added executable flags --- Ruby/GitRestart/bin/git-restart | 0 1 file changed, 0 insertions(+), 0 deletions(-) mode change 100644 => 100755 Ruby/GitRestart/bin/git-restart diff --git a/Ruby/GitRestart/bin/git-restart b/Ruby/GitRestart/bin/git-restart old mode 100644 new mode 100755 From 880327734b0dc33418ad29c056fb8859f90cf90e Mon Sep 17 00:00:00 2001 From: David Bailey Date: Tue, 14 Aug 2018 23:33:09 +0200 Subject: [PATCH 59/81] Updated gemspec a bit, and fixed wrong REF --- Ruby/GitRestart/git-restart.gemspec | 2 +- Ruby/GitRestart/lib/git-restart/runner.rb | 10 ++++++---- Ruby/GitRestart/lib/git-restart/task.rb | 1 - 3 files changed, 7 insertions(+), 6 deletions(-) diff --git a/Ruby/GitRestart/git-restart.gemspec b/Ruby/GitRestart/git-restart.gemspec index 073960e3..6ec21790 100644 --- a/Ruby/GitRestart/git-restart.gemspec +++ b/Ruby/GitRestart/git-restart.gemspec @@ -1,6 +1,6 @@ Gem::Specification.new do |s| s.name = 'git-restart' - s.version = '0.0.6-dev' + s.version = '0.0.10.dev' s.summary = '(Re)start scripts and monitor them on a GitHub push' s.description = 'This gem can be used to (re)start scripts whenever a GitHub push event is recorded. The exit status of scripts can be monitored, and a failure can be sent back, making this capable of running simple tests too!' diff --git a/Ruby/GitRestart/lib/git-restart/runner.rb b/Ruby/GitRestart/lib/git-restart/runner.rb index 6c910659..66f8328f 100644 --- a/Ruby/GitRestart/lib/git-restart/runner.rb +++ b/Ruby/GitRestart/lib/git-restart/runner.rb @@ -19,7 +19,7 @@ class Runner attr_accessor :octokit def current_commit() - @git.object("HEAD^").sha; + @git.object("HEAD").sha; end def current_branch() @git.current_branch(); @@ -65,10 +65,12 @@ def initialize() end 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, { + @octokit.create_status(@repo, current_commit(), newStatus, { context: "#{@name}/#{name}".gsub(" ", "_"), description: message, }) @@ -130,7 +132,7 @@ def _generate_next_tasks() def _start_next_tasks() _generate_next_tasks(); - puts "\n\nStarting next tasks!" + puts "\nStarting next tasks!" @next_tasks.each do |name, t| next unless t.active; next unless t.triggered? @@ -141,7 +143,7 @@ def _start_next_tasks() end def _switch_to(branch, commit = nil) - puts "\nSwitching to branch: #{branch}#{commit ? ",commit: #{commit}" : ""}" + puts "\n\nSwitching to branch: #{branch}#{commit ? ",commit: #{commit}" : ""}" begin @git.fetch(); diff --git a/Ruby/GitRestart/lib/git-restart/task.rb b/Ruby/GitRestart/lib/git-restart/task.rb index b34ba44d..8cf68599 100644 --- a/Ruby/GitRestart/lib/git-restart/task.rb +++ b/Ruby/GitRestart/lib/git-restart/task.rb @@ -104,7 +104,6 @@ def _report_status(status, message = nil) return unless @report_status runner().update_status(@name, status, message); - puts "Task #{@name} assumed a new status: #{status}#{message ? " MSG:#{message}" : ""}" end private :_report_status From 5f543a3560215536ee0d32f010cfb90c23d615b5 Mon Sep 17 00:00:00 2001 From: Xasin Date: Fri, 17 Aug 2018 13:33:25 +0200 Subject: [PATCH 60/81] Added the option of specifying task and runner files --- Ruby/GitRestart/bin/git-restart | 13 ++++++++++--- Ruby/GitRestart/git-restart.gemspec | 2 +- Ruby/GitRestart/lib/git-restart/runner.rb | 15 ++++++++++++++- 3 files changed, 25 insertions(+), 5 deletions(-) diff --git a/Ruby/GitRestart/bin/git-restart b/Ruby/GitRestart/bin/git-restart index dd4f665e..66c60c4d 100755 --- a/Ruby/GitRestart/bin/git-restart +++ b/Ruby/GitRestart/bin/git-restart @@ -5,10 +5,17 @@ require 'git-restart/runner.rb' puts "Starting runner ..." -target = ARGV[0] -target ||= ".gitrun" +$taskfiles = Array.new(); +target = ".gitrun" +ARGV.each do |t| + if(File.extname(t) == ".gitrun") + target = t; + elsif(File.extname(t) == ".gittask") + $taskfiles << t; + end +end -raise ArgumentError, "No valid task file specified!" unless File.exist? target +raise ArgumentError, "No valid runner file specified!" unless File.exist? target load target diff --git a/Ruby/GitRestart/git-restart.gemspec b/Ruby/GitRestart/git-restart.gemspec index 073960e3..f57adcbf 100644 --- a/Ruby/GitRestart/git-restart.gemspec +++ b/Ruby/GitRestart/git-restart.gemspec @@ -1,6 +1,6 @@ Gem::Specification.new do |s| s.name = 'git-restart' - s.version = '0.0.6-dev' + s.version = '0.0.11-dev' s.summary = '(Re)start scripts and monitor them on a GitHub push' s.description = 'This gem can be used to (re)start scripts whenever a GitHub push event is recorded. The exit status of scripts can be monitored, and a failure can be sent back, making this capable of running simple tests too!' diff --git a/Ruby/GitRestart/lib/git-restart/runner.rb b/Ruby/GitRestart/lib/git-restart/runner.rb index 6c910659..19217c66 100644 --- a/Ruby/GitRestart/lib/git-restart/runner.rb +++ b/Ruby/GitRestart/lib/git-restart/runner.rb @@ -11,6 +11,7 @@ class Runner attr_accessor :name attr_accessor :repo, :branches, :exclude_branches, :start_on + attr_accessor :allowed_tasks attr_reader :next_tasks attr_reader :current_task_file @@ -28,9 +29,15 @@ def current_modified() @current_modified; end - def initialize() + 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(); @@ -43,6 +50,8 @@ def initialize() yield(self); + @allowed_tasks.flatten! + @listenedSub = @mqtt.subscribe_to "GitHub/#{@repo}" do |data| begin data = JSON.parse(data, symbolize_names: true); @@ -113,6 +122,10 @@ def _generate_next_tasks() 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 From 3822e7d7552731ce2d729fa1d0086bddf3c9dade Mon Sep 17 00:00:00 2001 From: Xasin Date: Fri, 17 Aug 2018 15:33:09 +0200 Subject: [PATCH 61/81] Added reset before checkout and at_exit --- Ruby/GitRestart/lib/git-restart/runner.rb | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/Ruby/GitRestart/lib/git-restart/runner.rb b/Ruby/GitRestart/lib/git-restart/runner.rb index 19217c66..f972b8aa 100644 --- a/Ruby/GitRestart/lib/git-restart/runner.rb +++ b/Ruby/GitRestart/lib/git-restart/runner.rb @@ -71,6 +71,10 @@ def initialize(fileList = nil) autostart(); _start_task_thread(); + + at_exit { + _stop_all_tasks(); + } end def update_status(name, newStatus, message = nil) @@ -166,6 +170,7 @@ def _switch_to(branch, commit = nil) else _stop_triggered_tasks(); end + @git.reset_hard(); @git.checkout(branch); @git.merge("origin/#{branch}"); From 30d29c175ecd9b0da473f3bb510687bec70bfe9e Mon Sep 17 00:00:00 2001 From: Xasin Date: Tue, 28 Aug 2018 18:19:22 +0200 Subject: [PATCH 62/81] Added `current_commit` for later functions --- Ruby/GitRestart/lib/git-restart/task.rb | 3 +++ 1 file changed, 3 insertions(+) diff --git a/Ruby/GitRestart/lib/git-restart/task.rb b/Ruby/GitRestart/lib/git-restart/task.rb index 8cf68599..9afc1401 100644 --- a/Ruby/GitRestart/lib/git-restart/task.rb +++ b/Ruby/GitRestart/lib/git-restart/task.rb @@ -30,6 +30,9 @@ def runner() def branch() runner().current_branch(); end + def current_commit() + runner().current_commit(); + end def modified() runner().current_modified(); end From 10f5339d95918d2296737b0d3713662a1fee5501 Mon Sep 17 00:00:00 2001 From: Xasin Date: Tue, 28 Aug 2018 18:20:21 +0200 Subject: [PATCH 63/81] Added support for "above-dir" triggers --- Ruby/GitRestart/lib/git-restart/task.rb | 8 ++++++-- Ruby/GitRestart/tests/tc_task.rb | 6 ++++++ 2 files changed, 12 insertions(+), 2 deletions(-) diff --git a/Ruby/GitRestart/lib/git-restart/task.rb b/Ruby/GitRestart/lib/git-restart/task.rb index 9afc1401..568a9867 100644 --- a/Ruby/GitRestart/lib/git-restart/task.rb +++ b/Ruby/GitRestart/lib/git-restart/task.rb @@ -83,8 +83,12 @@ def triggered? @watched.each do |regEx| modified().each do |f| - next unless f =~ /#{Regexp.quote(@chdir)}(.*)/ - return true if $1 =~ regEx; + 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 diff --git a/Ruby/GitRestart/tests/tc_task.rb b/Ruby/GitRestart/tests/tc_task.rb index 52c87d40..933f56d6 100644 --- a/Ruby/GitRestart/tests/tc_task.rb +++ b/Ruby/GitRestart/tests/tc_task.rb @@ -136,6 +136,12 @@ def test_triggers() @runner.current_modified = ["Tests/Test1/NotTested.txt"]; refute t.triggered? + @runner.current_modified = ["OutOfCHDir/Test.rb"]; + refute t.triggered? + + t.watch(%r{/OutOfCHDir/.*}); + assert t.triggered? + t.on_branches ["master", "dev"]; assert t.active; t.active = false; From f2b87360d7a5ca631737d45846793c495c3ea31d Mon Sep 17 00:00:00 2001 From: Xasin Date: Tue, 28 Aug 2018 18:21:24 +0200 Subject: [PATCH 64/81] Logfiles in /tmp/ and proper status reporting! --- Ruby/GitRestart/lib/git-restart/task.rb | 25 +++++++++++++++++++++++-- 1 file changed, 23 insertions(+), 2 deletions(-) diff --git a/Ruby/GitRestart/lib/git-restart/task.rb b/Ruby/GitRestart/lib/git-restart/task.rb index 568a9867..79834f6a 100644 --- a/Ruby/GitRestart/lib/git-restart/task.rb +++ b/Ruby/GitRestart/lib/git-restart/task.rb @@ -70,6 +70,8 @@ def initialize() valid? + @status_file ||= "/tmp/TaskLog_#{@name}_#{current_commit()}"; + if(runner().next_tasks[@name]) raise TaskValidityError, "A task of name #{@name} already exists!" else @@ -105,8 +107,25 @@ def valid?() end end + def _rm_logfile() + if File.exist?("/tmp/TaskLog_#{@name}_#{current_commit()}") then + File.delete("/tmp/TaskLog_#{@name}_#{current_commit()}"); + end + end + 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 + def _report_status(status, message = nil) - @status_message = "" + message ||= _get_statusline(); + @status_message = message; return unless @report_status @@ -128,8 +147,9 @@ def start() @targets.each do |target| @statuschange_mutex.synchronize { break if @exiting + _rm_logfile(); options = { - [:out, :err] => "/dev/null" + [:out, :err] => "/tmp/TaskLog_#{@name}_#{current_commit()}" } options[:chdir] = @chdir if @chdir @@ -145,6 +165,7 @@ def start() if(@lastStatus == 0) _report_status(:success); + _rm_logfile(); elsif(!@exiting || @expect_clean_exit) _report_status(:failure); end From 285a2ec9038f46ab521889a65b9108a6da3ffbcf Mon Sep 17 00:00:00 2001 From: Xasin Date: Tue, 28 Aug 2018 18:21:51 +0200 Subject: [PATCH 65/81] Version bump --- Ruby/GitRestart/git-restart.gemspec | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Ruby/GitRestart/git-restart.gemspec b/Ruby/GitRestart/git-restart.gemspec index f57adcbf..e711bd51 100644 --- a/Ruby/GitRestart/git-restart.gemspec +++ b/Ruby/GitRestart/git-restart.gemspec @@ -1,6 +1,6 @@ Gem::Specification.new do |s| s.name = 'git-restart' - s.version = '0.0.11-dev' + s.version = '0.0.1' s.summary = '(Re)start scripts and monitor them on a GitHub push' s.description = 'This gem can be used to (re)start scripts whenever a GitHub push event is recorded. The exit status of scripts can be monitored, and a failure can be sent back, making this capable of running simple tests too!' From 28aef241aea928b1b40875a799e3b9c81c41d637 Mon Sep 17 00:00:00 2001 From: Xasin Date: Tue, 28 Aug 2018 20:58:19 +0200 Subject: [PATCH 66/81] Removed some derpy files --- Ruby/GitRestart/TestOutput.txt | 1 - Ruby/GitRestart/TestRunner.gitrun | 9 --------- Ruby/GitRestart/TestTasks.gittask | 23 ----------------------- 3 files changed, 33 deletions(-) delete mode 100644 Ruby/GitRestart/TestOutput.txt delete mode 100644 Ruby/GitRestart/TestRunner.gitrun delete mode 100644 Ruby/GitRestart/TestTasks.gittask diff --git a/Ruby/GitRestart/TestOutput.txt b/Ruby/GitRestart/TestOutput.txt deleted file mode 100644 index ad2a1f55..00000000 --- a/Ruby/GitRestart/TestOutput.txt +++ /dev/null @@ -1 +0,0 @@ -Hello from GIT_RESTART, modified files: [Ruby/GitRestart/TestOutput.txt] diff --git a/Ruby/GitRestart/TestRunner.gitrun b/Ruby/GitRestart/TestRunner.gitrun deleted file mode 100644 index e0bed100..00000000 --- a/Ruby/GitRestart/TestRunner.gitrun +++ /dev/null @@ -1,9 +0,0 @@ - -require_relative "lib/git-restart/runner.rb" - -$runner = GitRestart::Runner.new do |r| - r.name = "Test Git Runner"; - r.repo = "XasWorks/XasCode"; - - r.mqtt = "mqtt://xasin.hopto.org"; -end diff --git a/Ruby/GitRestart/TestTasks.gittask b/Ruby/GitRestart/TestTasks.gittask deleted file mode 100644 index 0a704a75..00000000 --- a/Ruby/GitRestart/TestTasks.gittask +++ /dev/null @@ -1,23 +0,0 @@ - -GitRestart::Task.new do |t| - t.name = "Failure testing"; - t.report_status = true; - - t.active = true; - t.watch(/.*/); - - t.targets << "echo Hello from #{t.branch}, modified files: #{t.modified} > TestOutput.txt" - t.targets << 'sleep 10' - t.targets << 'exit 1' -end - -GitRestart::Task.new do |t| - t.name = "Git-Task-Tester" - t.active = true; - t.report_status = true; - - t.watch(/task.rb/); - t.watch(/tc_task.rb/); - - t.targets << "ruby tests/tc_task.rb"; -end From a2343b2d6880a7c1dc6c8e1ebc9e668064b0c2bb Mon Sep 17 00:00:00 2001 From: Xasin Date: Tue, 28 Aug 2018 20:58:38 +0200 Subject: [PATCH 67/81] Allowed for any file to be loaded --- Ruby/GitRestart/bin/git-restart | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Ruby/GitRestart/bin/git-restart b/Ruby/GitRestart/bin/git-restart index 66c60c4d..e495d531 100755 --- a/Ruby/GitRestart/bin/git-restart +++ b/Ruby/GitRestart/bin/git-restart @@ -10,7 +10,7 @@ target = ".gitrun" ARGV.each do |t| if(File.extname(t) == ".gitrun") target = t; - elsif(File.extname(t) == ".gittask") + else $taskfiles << t; end end From 1c0b4fa268514a300b746eb8b2f3cda747c92166 Mon Sep 17 00:00:00 2001 From: Xasin Date: Tue, 28 Aug 2018 20:59:24 +0200 Subject: [PATCH 68/81] Fixes for the new status message generation --- Ruby/GitRestart/lib/git-restart/task.rb | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/Ruby/GitRestart/lib/git-restart/task.rb b/Ruby/GitRestart/lib/git-restart/task.rb index 79834f6a..96d4ae82 100644 --- a/Ruby/GitRestart/lib/git-restart/task.rb +++ b/Ruby/GitRestart/lib/git-restart/task.rb @@ -117,7 +117,13 @@ def _get_statusline() sMsg = "" File.open(@status_file, "r") do |sFile| - sFile.each_line do |l| sMsg = l; end + sFile.each_line do |l| + l.chomp! + next if l == ""; + next if l =~ /^\s+/; + + sMsg = l; + end end return sMsg; @@ -144,10 +150,10 @@ def start() @executionThread = Thread.new do _report_status(:pending); + _rm_logfile(); @targets.each do |target| @statuschange_mutex.synchronize { break if @exiting - _rm_logfile(); options = { [:out, :err] => "/tmp/TaskLog_#{@name}_#{current_commit()}" } From ed71b6b16f320bededddae8bba1955dfee2a8b22 Mon Sep 17 00:00:00 2001 From: Xasin Date: Tue, 28 Aug 2018 20:59:32 +0200 Subject: [PATCH 69/81] Testing of the new status message --- Ruby/GitRestart/tests/tc_task.rb | 28 ++++++++++++++++++++++++++++ 1 file changed, 28 insertions(+) diff --git a/Ruby/GitRestart/tests/tc_task.rb b/Ruby/GitRestart/tests/tc_task.rb index 933f56d6..23c4378c 100644 --- a/Ruby/GitRestart/tests/tc_task.rb +++ b/Ruby/GitRestart/tests/tc_task.rb @@ -118,6 +118,34 @@ def test_chdir assert File.exist?("/tmp/TEST_FILE_1"); end + def test_status_msg() + @task = GitRestart::Task.new do |t| + t.name = "TestTask" + t.ci_task = true; + + t.targets << 'echo "Status A!"' + end + + @task.start(); + @task.join(); + + refute File.exist? "/tmp/TaskLog_TestTask_#{@runner.current_commit()}" + assert_equal "Status A!", @task.status_message + + @task = GitRestart::Task.new do |t| + t.name = "TestTask2" + t.ci_task = true; + + t.targets << 'echo "Status A!" && exit 1' + end + + @task.start(); + @task.join(); + + assert File.exist? "/tmp/TaskLog_TestTask2_#{@runner.current_commit()}" + assert_equal "Status A!", @task.status_message + end + def test_triggers() @runner.current_branch = "master"; @runner.current_modified = ["Tests/Test1/Test.rb"]; From b5a7aebe5e626e09cd46b73e319966a8c6dd009e Mon Sep 17 00:00:00 2001 From: Xasin Date: Tue, 28 Aug 2018 20:59:54 +0200 Subject: [PATCH 70/81] Version bump --- Ruby/GitRestart/git-restart.gemspec | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Ruby/GitRestart/git-restart.gemspec b/Ruby/GitRestart/git-restart.gemspec index e711bd51..4eddd079 100644 --- a/Ruby/GitRestart/git-restart.gemspec +++ b/Ruby/GitRestart/git-restart.gemspec @@ -1,6 +1,6 @@ Gem::Specification.new do |s| s.name = 'git-restart' - s.version = '0.0.1' + s.version = '0.0.2' s.summary = '(Re)start scripts and monitor them on a GitHub push' s.description = 'This gem can be used to (re)start scripts whenever a GitHub push event is recorded. The exit status of scripts can be monitored, and a failure can be sent back, making this capable of running simple tests too!' From 295b80bdf91952349566193038fca39b3ceffe09 Mon Sep 17 00:00:00 2001 From: David Bailey Date: Mon, 3 Sep 2018 19:25:28 +0200 Subject: [PATCH 71/81] Added gitignore for yard --- Ruby/GitRestart/.gitignore | 2 ++ 1 file changed, 2 insertions(+) create mode 100644 Ruby/GitRestart/.gitignore diff --git a/Ruby/GitRestart/.gitignore b/Ruby/GitRestart/.gitignore new file mode 100644 index 00000000..d98a4bfe --- /dev/null +++ b/Ruby/GitRestart/.gitignore @@ -0,0 +1,2 @@ +.yardoc +doc From 02e4912ffe61572ed22db80045ddd710737cccd0 Mon Sep 17 00:00:00 2001 From: David Bailey Date: Mon, 3 Sep 2018 19:26:02 +0200 Subject: [PATCH 72/81] Added first GitRun task set --- Ruby/.gitrun | 12 ++++++++++++ Ruby/GitRestart/.gittask | 11 +++++++++++ 2 files changed, 23 insertions(+) create mode 100644 Ruby/.gitrun create mode 100644 Ruby/GitRestart/.gittask diff --git a/Ruby/.gitrun b/Ruby/.gitrun new file mode 100644 index 00000000..018a9de4 --- /dev/null +++ b/Ruby/.gitrun @@ -0,0 +1,12 @@ + +$XAS_CI = true; + +$runner = GitRestart::Runner.new do |r| + r.name = "XasStart CI"; + r.repo = "XasWorks/XasCode"; + + r.mqtt = "mqtt://xasin.hopto.org"; + r.octokit = Octokit::Client.new(netrc: true); + + r.start_on = r.current_branch(); +end diff --git a/Ruby/GitRestart/.gittask b/Ruby/GitRestart/.gittask new file mode 100644 index 00000000..67016072 --- /dev/null +++ b/Ruby/GitRestart/.gittask @@ -0,0 +1,11 @@ + +GitRestart::Task.new do |t| + @ci_task = true; + t.name = "GitRestart Task" + + t.report_status = true; + + t.active = true; + + t.targets << "ruby tests/tc_task.rb" +end From bdcfc1bc7b0d453e9bb073de271c40696d66772c Mon Sep 17 00:00:00 2001 From: David Bailey Date: Mon, 3 Sep 2018 19:46:10 +0200 Subject: [PATCH 73/81] incorrect setting of ci_task --- Ruby/GitRestart/.gittask | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Ruby/GitRestart/.gittask b/Ruby/GitRestart/.gittask index 67016072..9e6f45d6 100644 --- a/Ruby/GitRestart/.gittask +++ b/Ruby/GitRestart/.gittask @@ -1,6 +1,6 @@ GitRestart::Task.new do |t| - @ci_task = true; + t.ci_task = true; t.name = "GitRestart Task" t.report_status = true; From f02bc60aeaab7162437b5d244cc5e7a5db2860e0 Mon Sep 17 00:00:00 2001 From: David Bailey Date: Mon, 3 Sep 2018 19:46:52 +0200 Subject: [PATCH 74/81] Small fix to message name generation --- Ruby/GitRestart/git-restart.gemspec | 2 +- Ruby/GitRestart/lib/git-restart/runner.rb | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/Ruby/GitRestart/git-restart.gemspec b/Ruby/GitRestart/git-restart.gemspec index 4eddd079..da41c46b 100644 --- a/Ruby/GitRestart/git-restart.gemspec +++ b/Ruby/GitRestart/git-restart.gemspec @@ -1,6 +1,6 @@ Gem::Specification.new do |s| s.name = 'git-restart' - s.version = '0.0.2' + s.version = '0.0.3' s.summary = '(Re)start scripts and monitor them on a GitHub push' s.description = 'This gem can be used to (re)start scripts whenever a GitHub push event is recorded. The exit status of scripts can be monitored, and a failure can be sent back, making this capable of running simple tests too!' diff --git a/Ruby/GitRestart/lib/git-restart/runner.rb b/Ruby/GitRestart/lib/git-restart/runner.rb index 1a36c23d..6c738825 100644 --- a/Ruby/GitRestart/lib/git-restart/runner.rb +++ b/Ruby/GitRestart/lib/git-restart/runner.rb @@ -78,13 +78,13 @@ def initialize(fileList = nil) end def update_status(name, newStatus, message = nil) - puts "Task #{@name} assumed a new status: #{newStatus}#{message ? " MSG:#{message}" : ""}" + 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(" ", "_"), + context: "#{name}/#{name}".gsub(" ", "_"), description: message, }) rescue From be05ded8164ed985636ae0883079b2764883ea9c Mon Sep 17 00:00:00 2001 From: David Bailey Date: Mon, 3 Sep 2018 19:57:22 +0200 Subject: [PATCH 75/81] Change to work with "Ruby/.gittask" --- Ruby/GitRestart/bin/git-restart | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Ruby/GitRestart/bin/git-restart b/Ruby/GitRestart/bin/git-restart index e495d531..b9aa7e47 100755 --- a/Ruby/GitRestart/bin/git-restart +++ b/Ruby/GitRestart/bin/git-restart @@ -8,7 +8,7 @@ puts "Starting runner ..." $taskfiles = Array.new(); target = ".gitrun" ARGV.each do |t| - if(File.extname(t) == ".gitrun") + if(t =~ /\.gitrun$/) target = t; else $taskfiles << t; From 4bd6d8bb861a6607f22433e8511db6e88f6b8ff0 Mon Sep 17 00:00:00 2001 From: David Bailey Date: Mon, 3 Sep 2018 19:57:34 +0200 Subject: [PATCH 76/81] First batch of documentation, yay! --- Ruby/GitRestart/lib/git-restart/task.rb | 47 +++++++++++++++++++++++-- 1 file changed, 44 insertions(+), 3 deletions(-) diff --git a/Ruby/GitRestart/lib/git-restart/task.rb b/Ruby/GitRestart/lib/git-restart/task.rb index 96d4ae82..a4a3bc59 100644 --- a/Ruby/GitRestart/lib/git-restart/task.rb +++ b/Ruby/GitRestart/lib/git-restart/task.rb @@ -1,42 +1,82 @@ +# @author Xasin module GitRestart + # The Error-Class used to signal when a Task is set up wrong class TaskValidityError < StandardError end + # This class is used to define "Tasks". Each task represents + # a set of commands, which it executes in chronological order, or until + # a task errors. + # Additionally, it will kill execution of tasks with a specified kill-signal + # when an update was detected from GitHub. + # The failure-status of the tasks can also be reported via Octokit, allowing this + # to be used as a simple CI or Test system for various languages. class Task + # The array of tasks to execute. Each target will be executed in the given + # order via `Process.spawn`. + # @return [Array] attr_reader :targets + # The signal (as String, like Signal.list) to use to kill the process. + # Can be nil to disable killing attr_accessor :signal + # Whether or not to report failure if the currently running target + # has a non-zero exit status after having been killed. Only makes sense + # together with report_status attr_accessor :expect_clean_exit + # Whether or not to report failure/success status to GitHub using Octokit attr_accessor :report_status + # Defines this as a "CI_Task". Such a task will always run on an update, + # regardless what files changed. Useful if you always want a status report + # on GitHub. attr_accessor :ci_task - attr_accessor :name, :status_file - + # Name of the Task. *Required*. Used as *unique* ID, and to report status to GitHub + attr_accessor :name + # The file to use to retrieve a single-line status info for the "description" + # string of the GitHub status. Only the last *non-indented* line is used, + # which allows the output of Minitest to be used directly. + attr_accessor :status_file + + # Whether or not this task is active. Usually set via #on_branches, + # but can be used to manually disable or enable this task based on + # config files, ENV variables etc. attr_accessor :active + # The last status-code of this Task. Used internally. attr_reader :lastStatus + # The last status-message of this task. Used internally. attr_reader :status_message + # @api private def self.runner=(runner) @runner = runner; end + # @api private def self.runner() return @runner; end + # @return [GitRestart::Runner] Responsible Runner class def runner() return self.class.runner(); end + # @return [String] Name of the current branch def branch() runner().current_branch(); end + # @return [String] Full SHA of the current commit def current_commit() runner().current_commit(); end + + # @return [Array] A list of all files that were modified in the commit we are checking def modified() runner().current_modified(); end + # Use this function to specify which files trigger a restart for this Task + # Files can be specified as a RegEx, and can be "local/like.this" or "/reference/from/project.root" def watch(regEx) if(regEx.is_a? String) regEx = Regexp.quote(regEx); @@ -45,9 +85,10 @@ def watch(regEx) @watched << Regexp.new(regEx); end + # Specify which branches to run on. Not needed if "active" is just set to true def on_branches(branches) [branches].flatten.each do |b| - @active |= (b == branch()); + @activ |= (b == branch()); end end From f848ed8aa4e005816061a923fadb12040bd7bc01 Mon Sep 17 00:00:00 2001 From: David Bailey Date: Wed, 5 Sep 2018 18:44:03 +0200 Subject: [PATCH 77/81] Added MQTT Task file --- Ruby/MQTT/.gittask | 11 +++++++++++ Ruby/MQTT/tests/test_all.rb | 2 ++ 2 files changed, 13 insertions(+) create mode 100644 Ruby/MQTT/.gittask create mode 100644 Ruby/MQTT/tests/test_all.rb diff --git a/Ruby/MQTT/.gittask b/Ruby/MQTT/.gittask new file mode 100644 index 00000000..54a6b1d5 --- /dev/null +++ b/Ruby/MQTT/.gittask @@ -0,0 +1,11 @@ + +GitRestart::Task.new do |t| + t.ci_task = true; + t.name = "MQTT CI" + + t.report_status = true; + + t.active = true; + + t.targets << "ruby tests/test_all.rb" +end diff --git a/Ruby/MQTT/tests/test_all.rb b/Ruby/MQTT/tests/test_all.rb new file mode 100644 index 00000000..267890e8 --- /dev/null +++ b/Ruby/MQTT/tests/test_all.rb @@ -0,0 +1,2 @@ + +Dir["tc_*.rb"].each { |f| load f } From 6c48af922c8605296c7d7f38bc8e186ce792a51c Mon Sep 17 00:00:00 2001 From: David Bailey Date: Wed, 5 Sep 2018 18:52:45 +0200 Subject: [PATCH 78/81] Added properly functioning test sequence --- Ruby/MQTT/tests/test_all.rb | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/Ruby/MQTT/tests/test_all.rb b/Ruby/MQTT/tests/test_all.rb index 267890e8..cc5eeefb 100644 --- a/Ruby/MQTT/tests/test_all.rb +++ b/Ruby/MQTT/tests/test_all.rb @@ -1,2 +1,4 @@ -Dir["tc_*.rb"].each { |f| load f } +Dir.chdir(File.dirname(__FILE__)) { + Dir["*.rb"].each { |f| require_relative f } +} From cbd3078cdee2626021c0bcf594b899661956c38b Mon Sep 17 00:00:00 2001 From: David Bailey Date: Wed, 5 Sep 2018 19:30:03 +0200 Subject: [PATCH 79/81] Final bunch of documentation written, README remains --- Ruby/GitRestart/lib/git-restart/runner.rb | 43 +++++++++++++++++++++-- Ruby/GitRestart/lib/git-restart/task.rb | 30 +++++++++++++++- 2 files changed, 70 insertions(+), 3 deletions(-) diff --git a/Ruby/GitRestart/lib/git-restart/runner.rb b/Ruby/GitRestart/lib/git-restart/runner.rb index 6c738825..f675a343 100644 --- a/Ruby/GitRestart/lib/git-restart/runner.rb +++ b/Ruby/GitRestart/lib/git-restart/runner.rb @@ -8,23 +8,41 @@ module GitRestart class Runner + # Sets a name for this Runner, used for reporting to GitHub attr_accessor :name - attr_accessor :repo, :branches, :exclude_branches, :start_on + # Which repository to listen to. Uses "Owner/Repo" syntax. + attr_accessor :repo + # A white- and blacklist of branches. If neither are specified, all are used. + # If the whitelist is used, only it is considered. + attr_accessor :branches, :exclude_branches + # Which branch to start on. + # This not only makes the system switch branch, but it will also execute + # ALL active tasks. Very useful for auto-updating servers. + attr_accessor :start_on + # A list of tasks that this Runner will actually look at. + # If nil, all tasks are allowed. attr_accessor :allowed_tasks attr_reader :next_tasks attr_reader :current_task_file + # The MQTT::SubHandler to use to listen to GitHub updates. + # Can be specified as either MQTT::SubHandler class or String, the latter + # will be interpreted as URI attr_reader :mqtt + # Octokit to use for optional status reporting attr_accessor :octokit + # @return [String] Full SHA of the current commit def current_commit() @git.object("HEAD").sha; end + # @return [String] Name of the current branch def current_branch() @git.current_branch(); end + # @return [Array] A list of all files that were modified in the commit we are checking def current_modified() @current_modified; end @@ -77,6 +95,8 @@ def initialize(fileList = nil) } end + # Update the GitHub status for the task of given name, with optional status message + # Only prints a line if no octokit is specified def update_status(name, newStatus, message = nil) puts "Task #{name} assumed a new status: #{newStatus}#{message ? " MSG:#{message}" : ""}" @@ -91,6 +111,8 @@ def update_status(name, newStatus, message = nil) end end + # Start the task responsible for queueing and executing the individual + # task stop, branch switch, task start cycles def _start_task_thread() @taskThread = Thread.new do loop do @@ -101,7 +123,9 @@ def _start_task_thread() end end.abort_on_exception = true; end + private :_start_task_thread + # Stop all tasks of the given hash, not list. Waits for them to stop. def _stop_tasks(taskList) taskList.each do |name, t| t.stop(); @@ -111,19 +135,27 @@ def _stop_tasks(taskList) @current_tasks.delete(name); end end + private :_stop_tasks def _stop_all_tasks() _stop_tasks(@current_tasks); end + private :_stop_all_tasks + # Stop all tasks that have marked themselves as affected by the + # current set of file-changes. This way, applications are only + # restarted when their own files have been altered. def _stop_triggered_tasks() _stop_tasks(@current_tasks.select {|k,v| v.triggered?}); end + private :_stop_triggered_tasks + # Scan through the file-tree for .gittask files, or use the @allowed_tasks + # list of tasks. def _generate_next_tasks() puts "Generating new tasks..." @next_tasks = Hash.new(); taskFiles = `find ./ -nowarn -iname "*.gittask"` - taskFiles.split("\n").each do |t| + [taskFiles.split("\n"), @allowed_tasks].flatten.each do |t| puts "Looking at: #{t}" t.gsub!(/^\.\//,""); @current_task_file = t; @@ -145,7 +177,10 @@ def _generate_next_tasks() puts "Finished loading! Next tasks: #{@next_tasks.keys}" end + private :_generate_next_tasks + # Start all new tasks that have marked themselves as affected by the current + # set of filechanges def _start_next_tasks() _generate_next_tasks(); @@ -158,7 +193,10 @@ def _start_next_tasks() @current_tasks[name] = t; end end + private :_start_next_tasks + # Perform an entire cycle of git fetch & checkout, stop tasks, pull, restart. + # *CAUTION* A HARD RESET IS USED HERE def _switch_to(branch, commit = nil) puts "\n\nSwitching to branch: #{branch}#{commit ? ",commit: #{commit}" : ""}" @@ -180,6 +218,7 @@ def _switch_to(branch, commit = nil) _start_next_tasks(); end + private :_switch_to def autostart() return unless @start_on; diff --git a/Ruby/GitRestart/lib/git-restart/task.rb b/Ruby/GitRestart/lib/git-restart/task.rb index a4a3bc59..b6e3f978 100644 --- a/Ruby/GitRestart/lib/git-restart/task.rb +++ b/Ruby/GitRestart/lib/git-restart/task.rb @@ -88,10 +88,14 @@ def watch(regEx) # Specify which branches to run on. Not needed if "active" is just set to true def on_branches(branches) [branches].flatten.each do |b| - @activ |= (b == branch()); + @active |= (b == branch()); end end + # Create a new Task. This function does not take any input values, instead, + # one has to set the class up inside the block! + # A validity check will be run directly after yield(), as such, at the very least + # the name and a valid signal must have been specified! def initialize() @statuschange_mutex = Mutex.new(); @@ -120,6 +124,10 @@ def initialize() end end + # Checks whether or not the current set of modified files would require this + # task to be (re)started. Always returns true if @ci_task is set, or if + # the runner just has been started using @start_on + # @api private def triggered? return true if modified().nil? return true if @ci_task @@ -138,6 +146,9 @@ def triggered? return false; end + # Checks whether or not this task has been set up properly. Currently only + # checks the name and abort signal. + # @api private def valid?() unless Signal.list[@signal] or @signal.nil? raise TaskValidityError, "The specified kill-signal is not valid!" @@ -153,6 +164,7 @@ def _rm_logfile() File.delete("/tmp/TaskLog_#{@name}_#{current_commit()}"); end end + private :_rm_logfile def _get_statusline() return "No status specified" unless File.exist? @status_file @@ -169,6 +181,7 @@ def _get_statusline() return sMsg; end + private :_rm_logfile def _report_status(status, message = nil) message ||= _get_statusline(); @@ -180,6 +193,13 @@ def _report_status(status, message = nil) end private :_report_status + # Starts this task. + # Once the task has been started it will run each given + # target one after another, waiting for each one to finish. If @report_status + # is set, it will also do just that. + # Task execution is handled within a thread, meaning that the function + # itself does not block. + # @api private def start() puts "Starting Task: #{@name}" @@ -193,6 +213,7 @@ def start() _rm_logfile(); @targets.each do |target| + # Mutex to ensure there either is no task running or a PID given @statuschange_mutex.synchronize { break if @exiting options = { @@ -222,6 +243,11 @@ def start() sleep 0.01 end + # Stop this task. + # Stopping it means immediately killing the currently running target with + # the specified signal, and not running any further targets. + # *Except* when nil is specified as signal, in which case the stop will be ignored! + # @api private def stop() puts "Stopping Task: #{@name}" return if @signal.nil? @@ -234,6 +260,8 @@ def stop() } end + # Wait for this task to finish execution. + # Either by naturally ending, or by being killed. def join() @executionThread.join(); end From 9df8905603b43e5754af0936cf67b83e67bfe483 Mon Sep 17 00:00:00 2001 From: David Bailey Date: Wed, 5 Sep 2018 19:35:07 +0200 Subject: [PATCH 80/81] Tiny private --- Ruby/GitRestart/lib/git-restart/runner.rb | 1 + 1 file changed, 1 insertion(+) diff --git a/Ruby/GitRestart/lib/git-restart/runner.rb b/Ruby/GitRestart/lib/git-restart/runner.rb index f675a343..dbdc7ddc 100644 --- a/Ruby/GitRestart/lib/git-restart/runner.rb +++ b/Ruby/GitRestart/lib/git-restart/runner.rb @@ -224,6 +224,7 @@ def autostart() return unless @start_on; @branchQueue << {branch: @start_on}; end + private :autostart def mqtt=(mqtt) if(mqtt.is_a? String) From c3febff4f67d60b82d0a8383eaf0e89058c5cc3e Mon Sep 17 00:00:00 2001 From: David Bailey Date: Wed, 5 Sep 2018 19:35:32 +0200 Subject: [PATCH 81/81] Version boomp --- Ruby/GitRestart/git-restart.gemspec | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Ruby/GitRestart/git-restart.gemspec b/Ruby/GitRestart/git-restart.gemspec index da41c46b..c574bcd0 100644 --- a/Ruby/GitRestart/git-restart.gemspec +++ b/Ruby/GitRestart/git-restart.gemspec @@ -1,6 +1,6 @@ Gem::Specification.new do |s| s.name = 'git-restart' - s.version = '0.0.3' + s.version = '0.1.0' s.summary = '(Re)start scripts and monitor them on a GitHub push' s.description = 'This gem can be used to (re)start scripts whenever a GitHub push event is recorded. The exit status of scripts can be monitored, and a failure can be sent back, making this capable of running simple tests too!'