ruby_raider 4.0.1 → 4.2.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/.github/workflows/e2e_tests.yml +3 -0
- data/README.md +58 -1
- data/RELEASE.md +25 -0
- data/lib/commands/schedule_commands.rb +84 -0
- data/lib/generators/templates/common/git_ignore.tt +1 -0
- data/lib/ruby_raider/runtime/retry_reset.rb +32 -0
- data/lib/ruby_raider/runtime/rspec.rb +1 -0
- data/lib/ruby_raider/runtime/show_browser.rb +53 -0
- data/lib/ruby_raider/runtime.rb +2 -0
- data/lib/ruby_raider.rb +31 -0
- data/lib/utilities/schedules/adapter.rb +22 -0
- data/lib/utilities/schedules/base.rb +27 -0
- data/lib/utilities/schedules/kind.rb +74 -0
- data/lib/utilities/schedules/legacy.rb +196 -0
- data/lib/utilities/schedules/legacy_stub.rb +62 -0
- data/lib/utilities/schedules/next_run.rb +61 -0
- data/lib/utilities/schedules/notifier.rb +76 -0
- data/lib/utilities/schedules/os/command_runner.rb +73 -0
- data/lib/utilities/schedules/os/cron.rb +147 -0
- data/lib/utilities/schedules/os/launchd.rb +182 -0
- data/lib/utilities/schedules/os/linux.rb +81 -0
- data/lib/utilities/schedules/os/systemd.rb +194 -0
- data/lib/utilities/schedules/os/task_scheduler.rb +195 -0
- data/lib/utilities/schedules/os_enumerator.rb +30 -0
- data/lib/utilities/schedules/project.rb +70 -0
- data/lib/utilities/schedules/rule.rb +162 -0
- data/lib/utilities/schedules/run_lock.rb +160 -0
- data/lib/utilities/schedules/run_log.rb +122 -0
- data/lib/utilities/schedules/run_report.rb +139 -0
- data/lib/utilities/schedules/run_wrapper.rb +216 -0
- data/lib/utilities/schedules/schedule.rb +89 -0
- data/lib/utilities/schedules/schedule_store.rb +53 -0
- data/lib/utilities/schedules/scheduler.rb +178 -0
- data/lib/utilities/schedules/scheduler_steps.rb +69 -0
- data/lib/utilities/schedules/selection.rb +133 -0
- data/lib/utilities/schedules/spawn.rb +68 -0
- data/lib/utilities/schedules/store.rb +139 -0
- data/lib/version +1 -1
- data/ruby_raider.gemspec +1 -0
- data/sig/commands/schedule_commands.rbs +12 -0
- data/sig/ruby_raider/runtime/retry_reset.rbs +7 -0
- data/sig/ruby_raider/runtime/show_browser.rbs +27 -0
- data/sig/ruby_raider.rbs +6 -0
- data/sig/utilities/schedules/adapter.rbs +5 -0
- data/sig/utilities/schedules/base.rbs +15 -0
- data/sig/utilities/schedules/kind.rbs +32 -0
- data/sig/utilities/schedules/legacy.rbs +45 -0
- data/sig/utilities/schedules/legacy_stub.rbs +15 -0
- data/sig/utilities/schedules/next_run.rbs +15 -0
- data/sig/utilities/schedules/notifier.rbs +29 -0
- data/sig/utilities/schedules/os/command_runner.rbs +27 -0
- data/sig/utilities/schedules/os/cron.rbs +34 -0
- data/sig/utilities/schedules/os/launchd.rbs +46 -0
- data/sig/utilities/schedules/os/linux.rbs +33 -0
- data/sig/utilities/schedules/os/systemd.rbs +44 -0
- data/sig/utilities/schedules/os/task_scheduler.rbs +56 -0
- data/sig/utilities/schedules/os_enumerator.rbs +12 -0
- data/sig/utilities/schedules/project.rbs +22 -0
- data/sig/utilities/schedules/rule.rbs +72 -0
- data/sig/utilities/schedules/run_lock.rbs +29 -0
- data/sig/utilities/schedules/run_log.rbs +33 -0
- data/sig/utilities/schedules/run_report.rbs +34 -0
- data/sig/utilities/schedules/run_wrapper.rbs +45 -0
- data/sig/utilities/schedules/schedule.rbs +27 -0
- data/sig/utilities/schedules/schedule_store.rbs +21 -0
- data/sig/utilities/schedules/scheduler.rbs +49 -0
- data/sig/utilities/schedules/scheduler_steps.rbs +22 -0
- data/sig/utilities/schedules/selection.rbs +36 -0
- data/sig/utilities/schedules/spawn.rbs +10 -0
- data/sig/utilities/schedules/store.rbs +39 -0
- data/spec/commands/schedule_commands_spec.rb +284 -0
- data/spec/integration/content/common_content_spec.rb +8 -2
- data/spec/integration/content/gemfile_content_spec.rb +2 -2
- data/spec/runtime/retry_reset_spec.rb +112 -0
- data/spec/runtime/show_browser_project_spec.rb +70 -0
- data/spec/runtime/show_browser_spec.rb +101 -0
- data/spec/utilities/schedules/kind_spec.rb +81 -0
- data/spec/utilities/schedules/legacy_spec.rb +433 -0
- data/spec/utilities/schedules/legacy_stub_spec.rb +144 -0
- data/spec/utilities/schedules/next_run_spec.rb +119 -0
- data/spec/utilities/schedules/notifier_spec.rb +120 -0
- data/spec/utilities/schedules/os/command_runner_spec.rb +55 -0
- data/spec/utilities/schedules/os/cron_spec.rb +292 -0
- data/spec/utilities/schedules/os/launchd_spec.rb +306 -0
- data/spec/utilities/schedules/os/linux_spec.rb +163 -0
- data/spec/utilities/schedules/os/list_ids_spec.rb +202 -0
- data/spec/utilities/schedules/os/systemd_spec.rb +333 -0
- data/spec/utilities/schedules/os/task_scheduler_spec.rb +511 -0
- data/spec/utilities/schedules/os_entry_spec.rb +54 -0
- data/spec/utilities/schedules/os_enumerator_spec.rb +57 -0
- data/spec/utilities/schedules/project_spec.rb +125 -0
- data/spec/utilities/schedules/run_lock_spec.rb +143 -0
- data/spec/utilities/schedules/run_log_spec.rb +83 -0
- data/spec/utilities/schedules/run_report_spec.rb +150 -0
- data/spec/utilities/schedules/run_wrapper_integration_spec.rb +166 -0
- data/spec/utilities/schedules/run_wrapper_spec.rb +430 -0
- data/spec/utilities/schedules/schedule_spec.rb +120 -0
- data/spec/utilities/schedules/schedule_store_spec.rb +151 -0
- data/spec/utilities/schedules/scheduler_launchd_spec.rb +77 -0
- data/spec/utilities/schedules/scheduler_spec.rb +445 -0
- data/spec/utilities/schedules/selection_spec.rb +176 -0
- data/spec/utilities/schedules/spawn_spec.rb +104 -0
- metadata +106 -2
|
@@ -0,0 +1,70 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require_relative 'base'
|
|
4
|
+
|
|
5
|
+
module RubyRaider
|
|
6
|
+
module Schedules
|
|
7
|
+
# A project folder, by its real path: where its schedules and their files live
|
|
8
|
+
# :reek:MissingSafeMethod { exclude: [ check_show_browser!, check_raider_version! ] } -- checks that raise; runtime_driver? and locked_raider_version are their safe twins
|
|
9
|
+
# :reek:TooManyConstants { max_constants: 8 } -- the driver helper and Gemfile.lock checks
|
|
10
|
+
class Project
|
|
11
|
+
DRIVER_HELPER = 'helpers/driver_helper.rb'
|
|
12
|
+
# This gem's version, read as Raider#current_version reads it
|
|
13
|
+
OWN_VERSION = File.read(File.expand_path('../../version', __dir__)).strip.freeze
|
|
14
|
+
# The ruby_raider spec line of a GEM, PATH or GIT section (LF or CRLF); never evaluated
|
|
15
|
+
LOCKED_RAIDER = /^ {4}ruby_raider \(([0-9]+(?:\.[0-9A-Za-z]+)*)\)\r?$/
|
|
16
|
+
MAX_LOCK_BYTES = 5_000_000
|
|
17
|
+
# Every 4.x Selenium driver_helper loads the runtime, which honours RAIDER_SHOW_BROWSER
|
|
18
|
+
REQUIRES_RUNTIME = %r{^[ \t]*require[ \t]+(['"])ruby_raider/runtime\1}n
|
|
19
|
+
MAX_HELPER_BYTES = 1_000_000
|
|
20
|
+
NO_RUNTIME = 'Show the browser needs a project made with Ruby Raider 4.0 or later'
|
|
21
|
+
|
|
22
|
+
attr_reader :root
|
|
23
|
+
|
|
24
|
+
def initialize(path)
|
|
25
|
+
@root = File.realpath(path)
|
|
26
|
+
rescue SystemCallError
|
|
27
|
+
raise Error, "Project folder not found: #{path}"
|
|
28
|
+
end
|
|
29
|
+
|
|
30
|
+
def path(relative) = File.join(root, relative)
|
|
31
|
+
|
|
32
|
+
# `add --show-browser` refuses a project whose driver_helper can't drop --headless
|
|
33
|
+
def check_show_browser!
|
|
34
|
+
raise Error, NO_RUNTIME unless runtime_driver?
|
|
35
|
+
end
|
|
36
|
+
|
|
37
|
+
# The OS entry runs `bundle exec raider schedule run`, so the project's own bundle must have this command
|
|
38
|
+
def check_raider_version!(own = OWN_VERSION)
|
|
39
|
+
needs = "Scheduling needs ruby_raider #{own} or later in this project"
|
|
40
|
+
raise Error, "#{needs}: no Gemfile found" unless File.file?(path('Gemfile'))
|
|
41
|
+
|
|
42
|
+
locked = locked_raider_version
|
|
43
|
+
raise Error, "#{needs}: run bundle install first" if locked == :no_lock
|
|
44
|
+
return if locked && Gem::Version.new(locked) >= Gem::Version.new(own)
|
|
45
|
+
|
|
46
|
+
raise Error, "#{needs}: bundle update ruby_raider"
|
|
47
|
+
end
|
|
48
|
+
|
|
49
|
+
# The ruby_raider version Gemfile.lock resolves (any source), nil when it lists none, :no_lock without a lock
|
|
50
|
+
def locked_raider_version
|
|
51
|
+
lock = path('Gemfile.lock')
|
|
52
|
+
return :no_lock unless File.file?(lock)
|
|
53
|
+
|
|
54
|
+
File.binread(lock, MAX_LOCK_BYTES).to_s[LOCKED_RAIDER, 1]
|
|
55
|
+
rescue SystemCallError
|
|
56
|
+
:no_lock
|
|
57
|
+
end
|
|
58
|
+
|
|
59
|
+
# true when helpers/driver_helper.rb is a regular file inside the project that requires the runtime
|
|
60
|
+
def runtime_driver?
|
|
61
|
+
real = File.realpath(path(DRIVER_HELPER))
|
|
62
|
+
return false unless real.start_with?("#{root}#{File::SEPARATOR}") && File.file?(real)
|
|
63
|
+
|
|
64
|
+
REQUIRES_RUNTIME.match?(File.binread(real, MAX_HELPER_BYTES).to_s)
|
|
65
|
+
rescue SystemCallError
|
|
66
|
+
false
|
|
67
|
+
end
|
|
68
|
+
end
|
|
69
|
+
end
|
|
70
|
+
end
|
|
@@ -0,0 +1,162 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require 'digest'
|
|
4
|
+
require 'time'
|
|
5
|
+
require_relative 'kind'
|
|
6
|
+
|
|
7
|
+
module RubyRaider
|
|
8
|
+
module Schedules
|
|
9
|
+
# What every schedule shares, tests or 4.1: the rule (every/at/on), retries and notify, and what the OS
|
|
10
|
+
# entry runs (absolute ruby and bundle, GEM_HOME/GEM_PATH captured when it was scheduled). A subclass
|
|
11
|
+
# names its Kind (self.kind), checks its own name and fields in its initialize, and freezes.
|
|
12
|
+
# :reek:TooManyConstants { max_constants: 7 } -- the rule vocabulary and the OS entry parts
|
|
13
|
+
class Rule
|
|
14
|
+
EVERY = %w[hour day weekday week].freeze
|
|
15
|
+
DAYS = Schedules::DAYS
|
|
16
|
+
DAYS_RULE = 'use mon, tue, wed, thu, fri, sat or sun'
|
|
17
|
+
CLOCK = /\A([01]\d|2[0-3]):([0-5]\d)\z/
|
|
18
|
+
MINUTE = /\A(?:[01]\d|2[0-3])?:([0-5]\d)\z/
|
|
19
|
+
GEM_ENV = %w[GEM_HOME GEM_PATH].freeze
|
|
20
|
+
SYSTEM_PATH = %w[/usr/bin /bin].freeze
|
|
21
|
+
|
|
22
|
+
attr_reader :name, :every, :at, :on, :retries, :notify, :os_id, :ruby, :bundle, :gem_env, :project_root, :created_at
|
|
23
|
+
|
|
24
|
+
# The Kind of the subclass's schedules
|
|
25
|
+
def self.kind = raise(NotImplementedError, "#{self} must name its Kind")
|
|
26
|
+
|
|
27
|
+
def self.os_id_for(project_root, name) = "#{os_id_prefix_for(project_root)}#{name}"
|
|
28
|
+
|
|
29
|
+
# What every os_id of this kind for the project starts with: "<kind prefix>.<hash10>."
|
|
30
|
+
def self.os_id_prefix_for(project_root) = "#{kind.os_id_prefix}.#{Digest::SHA256.hexdigest(project_root)[0, 10]}."
|
|
31
|
+
|
|
32
|
+
# The absolute bundle of this Ruby. Gem.bin_path can also answer nil (seen on CI with a bare env).
|
|
33
|
+
def self.bundle_path
|
|
34
|
+
path = Gem.bin_path('bundler', 'bundle')
|
|
35
|
+
return path if path.is_a?(String) && File.absolute_path?(path)
|
|
36
|
+
|
|
37
|
+
raise Error, "Bundler not found for this Ruby (no bundle executable, got #{path.inspect}): gem install bundler, then try again"
|
|
38
|
+
rescue Gem::GemNotFoundException, Gem::MissingSpecError => e
|
|
39
|
+
raise Error, "Bundler not found for this Ruby (#{e.message}): gem install bundler, then try again"
|
|
40
|
+
end
|
|
41
|
+
|
|
42
|
+
# The GEM_HOME and GEM_PATH raider started with (before bundle exec changed them), only when set
|
|
43
|
+
def self.captured_gem_env
|
|
44
|
+
env = defined?(Bundler) ? Bundler.original_env : ENV
|
|
45
|
+
GEM_ENV.each_with_object({}) do |key, captured|
|
|
46
|
+
value = env[key]
|
|
47
|
+
captured[key] = value if value.is_a?(String) && !value.empty?
|
|
48
|
+
end
|
|
49
|
+
end
|
|
50
|
+
|
|
51
|
+
def kind = self.class.kind
|
|
52
|
+
|
|
53
|
+
def hour = at[0, 2].to_i
|
|
54
|
+
|
|
55
|
+
def minute = at[3, 2].to_i
|
|
56
|
+
|
|
57
|
+
def days
|
|
58
|
+
case every
|
|
59
|
+
when 'weekday' then DAYS.first(5)
|
|
60
|
+
when 'week' then [on.to_s]
|
|
61
|
+
else DAYS
|
|
62
|
+
end
|
|
63
|
+
end
|
|
64
|
+
|
|
65
|
+
# `raider <command> run NAME`, as the OS entry runs it
|
|
66
|
+
def argv = [ruby, bundle, 'exec', 'raider', kind.command, 'run', name, '--project', project_root, '--trigger', 'schedule']
|
|
67
|
+
|
|
68
|
+
def env
|
|
69
|
+
path = [File.dirname(ruby), File.dirname(bundle), *SYSTEM_PATH].uniq.join(File::PATH_SEPARATOR)
|
|
70
|
+
{ 'PATH' => path, 'LANG' => 'en_US.UTF-8' }.merge(gem_env)
|
|
71
|
+
end
|
|
72
|
+
|
|
73
|
+
def working_directory = project_root
|
|
74
|
+
|
|
75
|
+
def log_path = File.join(project_root, kind.dir, name, 'scheduler.log')
|
|
76
|
+
|
|
77
|
+
def rule_text
|
|
78
|
+
case every
|
|
79
|
+
when 'hour' then "every hour at #{at[2, 3]}"
|
|
80
|
+
when 'week' then "every week on #{on} at #{at}"
|
|
81
|
+
else "every #{every} at #{at}"
|
|
82
|
+
end
|
|
83
|
+
end
|
|
84
|
+
|
|
85
|
+
private
|
|
86
|
+
|
|
87
|
+
def checked_every(every)
|
|
88
|
+
return every.dup.freeze if EVERY.include?(every)
|
|
89
|
+
|
|
90
|
+
raise Error, "Invalid every #{every.inspect}: use hour, day, weekday or week"
|
|
91
|
+
end
|
|
92
|
+
|
|
93
|
+
# HH:MM, or for every hour :MM or HH:MM (the hour ignored, :00 when left out), kept as HH:MM
|
|
94
|
+
def checked_at(at)
|
|
95
|
+
return clock(at) unless every == 'hour'
|
|
96
|
+
return '00:00' if at.nil?
|
|
97
|
+
|
|
98
|
+
match = MINUTE.match(at.to_s)
|
|
99
|
+
raise Error, "Invalid at #{at.inspect}: use :MM or HH:MM for every hour" unless match
|
|
100
|
+
|
|
101
|
+
"00:#{match[1]}"
|
|
102
|
+
end
|
|
103
|
+
|
|
104
|
+
def clock(at)
|
|
105
|
+
raise Error, "Every #{every} needs at: use HH:MM (24h)" if at.nil?
|
|
106
|
+
return at.dup.freeze if at.is_a?(String) && CLOCK.match?(at)
|
|
107
|
+
|
|
108
|
+
raise Error, "Invalid at #{at.inspect}: use HH:MM (24h)"
|
|
109
|
+
end
|
|
110
|
+
|
|
111
|
+
def checked_on(on)
|
|
112
|
+
unless every == 'week'
|
|
113
|
+
raise Error, 'on is only for every week' unless on.nil?
|
|
114
|
+
|
|
115
|
+
return nil
|
|
116
|
+
end
|
|
117
|
+
raise Error, "Every week needs on: #{DAYS_RULE}" if on.nil?
|
|
118
|
+
return on.dup.freeze if DAYS.include?(on)
|
|
119
|
+
|
|
120
|
+
raise Error, "Invalid on #{on.inspect}: #{DAYS_RULE}"
|
|
121
|
+
end
|
|
122
|
+
|
|
123
|
+
def checked_retries(retries)
|
|
124
|
+
return retries if retries.is_a?(Integer) && retries.between?(0, 3)
|
|
125
|
+
|
|
126
|
+
raise Error, "Invalid retries #{retries.inspect}: use 0 to 3"
|
|
127
|
+
end
|
|
128
|
+
|
|
129
|
+
# An entry written before the flag existed has no key: off
|
|
130
|
+
def checked_flag(label, flag)
|
|
131
|
+
return flag == true if [true, false, nil].include?(flag)
|
|
132
|
+
|
|
133
|
+
raise Error, "Invalid #{label} #{flag.inspect}: use true or false"
|
|
134
|
+
end
|
|
135
|
+
|
|
136
|
+
def checked_os_id(os_id)
|
|
137
|
+
pattern = /\A#{Regexp.escape(kind.os_id_prefix)}\.[0-9a-f]{10}\.#{Regexp.escape(name)}\z/
|
|
138
|
+
return os_id.dup.freeze if os_id.is_a?(String) && pattern.match?(os_id)
|
|
139
|
+
|
|
140
|
+
raise Error, "Invalid os_id #{os_id.inspect} for #{name}"
|
|
141
|
+
end
|
|
142
|
+
|
|
143
|
+
def checked_path(label, path)
|
|
144
|
+
return checked_text(label, path) if path.is_a?(String) && File.absolute_path?(path)
|
|
145
|
+
|
|
146
|
+
raise Error, "Invalid #{label} #{path.inspect}: use an absolute path"
|
|
147
|
+
end
|
|
148
|
+
|
|
149
|
+
def checked_gem_env(gem_env)
|
|
150
|
+
raise Error, 'Invalid gem_env: use a mapping of GEM_HOME and GEM_PATH' unless gem_env.is_a?(Hash) && (gem_env.keys - GEM_ENV).empty?
|
|
151
|
+
|
|
152
|
+
gem_env.to_h { |key, value| [key.dup.freeze, checked_text(key, value)] }.freeze
|
|
153
|
+
end
|
|
154
|
+
|
|
155
|
+
def checked_text(label, text)
|
|
156
|
+
return text.dup.freeze if text.is_a?(String) && text.valid_encoding? && !text.empty? && !text.match?(/[[:cntrl:]]/)
|
|
157
|
+
|
|
158
|
+
raise Error, "Invalid #{label} #{text.inspect}"
|
|
159
|
+
end
|
|
160
|
+
end
|
|
161
|
+
end
|
|
162
|
+
end
|
|
@@ -0,0 +1,160 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require 'json'
|
|
4
|
+
require 'securerandom'
|
|
5
|
+
require_relative 'base'
|
|
6
|
+
require_relative 'kind'
|
|
7
|
+
require_relative 'run_log'
|
|
8
|
+
|
|
9
|
+
module RubyRaider
|
|
10
|
+
module Schedules
|
|
11
|
+
# Who is running the project's tests now, decided before rspec spawns (a busy run must never wipe
|
|
12
|
+
# raider-results/):
|
|
13
|
+
# - .raider/schedules/run.lock, held with flock among `raider schedule run`s; never removed;
|
|
14
|
+
# - .raider/schedules/running.json {pid, name, run_id, started_at}, which the desktop is to write for its
|
|
15
|
+
# own runs too. A live pid there that is not ours means busy. It is read and written only as a regular file
|
|
16
|
+
# (O_NOFOLLOW), made with O_EXCL, and its pid is only checked, never signalled.
|
|
17
|
+
class RunLock
|
|
18
|
+
LOCK = 'run.lock'
|
|
19
|
+
RUNNING = 'running.json'
|
|
20
|
+
MAX_RUNNING_BYTES = 4096
|
|
21
|
+
|
|
22
|
+
def initialize(project)
|
|
23
|
+
@project = project
|
|
24
|
+
@lock = nil
|
|
25
|
+
@run_id = nil
|
|
26
|
+
end
|
|
27
|
+
|
|
28
|
+
# true with the lock held and running.json written; false when another run is going
|
|
29
|
+
def acquire(name:, run_id:, started_at:)
|
|
30
|
+
dir = RunLog.private_dir(@project, Kind::TESTS.dir)
|
|
31
|
+
return false unless lock(File.join(dir, LOCK))
|
|
32
|
+
|
|
33
|
+
running = File.join(dir, RUNNING)
|
|
34
|
+
seen = inspect_running(running)
|
|
35
|
+
return busy if seen && (seen[:changed] || live?(seen[:data]))
|
|
36
|
+
return busy if seen && !remove_stale(running, seen[:id])
|
|
37
|
+
|
|
38
|
+
write_running(running, name, run_id, started_at) || busy
|
|
39
|
+
end
|
|
40
|
+
|
|
41
|
+
# Removes running.json when it is still ours, then frees the lock
|
|
42
|
+
def release
|
|
43
|
+
running = @project.path("#{Kind::TESTS.dir}/#{RUNNING}")
|
|
44
|
+
File.unlink(running) if @run_id && read_running(running)&.fetch('run_id', nil) == @run_id
|
|
45
|
+
rescue SystemCallError
|
|
46
|
+
nil
|
|
47
|
+
ensure
|
|
48
|
+
@run_id = nil
|
|
49
|
+
@lock&.close
|
|
50
|
+
@lock = nil
|
|
51
|
+
end
|
|
52
|
+
|
|
53
|
+
private
|
|
54
|
+
|
|
55
|
+
def lock(path)
|
|
56
|
+
file = File.open(path, File::RDWR | File::CREAT | RunLog::NOFOLLOW | RunLog::NONBLOCK, 0o600)
|
|
57
|
+
@lock = file
|
|
58
|
+
raise Error, "#{Kind::TESTS.dir}/#{LOCK} must be a file" unless file.stat.file?
|
|
59
|
+
|
|
60
|
+
file.flock(File::LOCK_EX | File::LOCK_NB) ? true : busy
|
|
61
|
+
rescue Errno::ELOOP, Errno::EMLINK
|
|
62
|
+
raise Error, "#{Kind::TESTS.dir}/#{LOCK} must be a file, not a link"
|
|
63
|
+
rescue Error
|
|
64
|
+
busy
|
|
65
|
+
raise
|
|
66
|
+
end
|
|
67
|
+
|
|
68
|
+
def busy
|
|
69
|
+
@lock&.close
|
|
70
|
+
@lock = nil
|
|
71
|
+
false
|
|
72
|
+
end
|
|
73
|
+
|
|
74
|
+
# What is at running.json now: its identity (dev, ino, never through a link), its object when it is a regular
|
|
75
|
+
# JSON file, and changed when it was replaced between the lstat and the read. nil when there is none.
|
|
76
|
+
def inspect_running(running)
|
|
77
|
+
stat = File.lstat(running)
|
|
78
|
+
id = [stat.dev, stat.ino]
|
|
79
|
+
data = nil
|
|
80
|
+
changed = false
|
|
81
|
+
if stat.file?
|
|
82
|
+
File.open(running, File::RDONLY | RunLog::NOFOLLOW | RunLog::NONBLOCK) do |file|
|
|
83
|
+
opened = file.stat
|
|
84
|
+
changed = [opened.dev, opened.ino] != id
|
|
85
|
+
data = parse_running(file) unless changed
|
|
86
|
+
end
|
|
87
|
+
end
|
|
88
|
+
{ id: id, data: data, changed: changed }
|
|
89
|
+
rescue Errno::ENOENT
|
|
90
|
+
nil
|
|
91
|
+
rescue SystemCallError
|
|
92
|
+
{ id: nil, data: nil, changed: true }
|
|
93
|
+
end
|
|
94
|
+
|
|
95
|
+
# Moves the stale file aside and removes it only when it is still the one inspected. The desktop takes no
|
|
96
|
+
# lock, so a running.json it wrote in between is put back (link never replaces a newer one) and counts as busy.
|
|
97
|
+
def remove_stale(running, id)
|
|
98
|
+
aside = "#{running}.#{SecureRandom.hex(6)}.stale"
|
|
99
|
+
File.rename(running, aside)
|
|
100
|
+
stat = File.lstat(aside)
|
|
101
|
+
return File.unlink(aside).positive? if [stat.dev, stat.ino] == id
|
|
102
|
+
|
|
103
|
+
restore(aside, running)
|
|
104
|
+
false
|
|
105
|
+
rescue Errno::ENOENT
|
|
106
|
+
true
|
|
107
|
+
end
|
|
108
|
+
|
|
109
|
+
def restore(aside, running)
|
|
110
|
+
File.link(aside, running)
|
|
111
|
+
rescue SystemCallError
|
|
112
|
+
nil
|
|
113
|
+
ensure
|
|
114
|
+
File.unlink(aside)
|
|
115
|
+
end
|
|
116
|
+
|
|
117
|
+
# Another process named by running.json is alive. kill(0) only checks: EPERM means alive but not ours to signal.
|
|
118
|
+
def live?(data)
|
|
119
|
+
pid = data&.fetch('pid', nil)
|
|
120
|
+
return false unless pid.is_a?(Integer) && pid.positive? && pid != Process.pid
|
|
121
|
+
|
|
122
|
+
Process.kill(0, pid)
|
|
123
|
+
true
|
|
124
|
+
rescue Errno::EPERM
|
|
125
|
+
true
|
|
126
|
+
rescue SystemCallError
|
|
127
|
+
false
|
|
128
|
+
end
|
|
129
|
+
|
|
130
|
+
# Its JSON object, nil when it is missing, a link, not a regular file, too big or not an object
|
|
131
|
+
def read_running(running)
|
|
132
|
+
File.open(running, File::RDONLY | RunLog::NOFOLLOW | RunLog::NONBLOCK) { |file| parse_running(file) }
|
|
133
|
+
rescue SystemCallError
|
|
134
|
+
nil
|
|
135
|
+
end
|
|
136
|
+
|
|
137
|
+
# The object in an open running.json, nil when it is not a small regular file holding one
|
|
138
|
+
def parse_running(file)
|
|
139
|
+
stat = file.stat
|
|
140
|
+
return nil unless stat.file? && stat.size <= MAX_RUNNING_BYTES
|
|
141
|
+
|
|
142
|
+
data = JSON.parse(file.read.to_s)
|
|
143
|
+
data.is_a?(Hash) ? data : nil
|
|
144
|
+
rescue JSON::ParserError
|
|
145
|
+
nil
|
|
146
|
+
end
|
|
147
|
+
|
|
148
|
+
# false when another writer made the file first
|
|
149
|
+
def write_running(running, name, run_id, started_at)
|
|
150
|
+
File.open(running, File::WRONLY | File::CREAT | File::EXCL | RunLog::NOFOLLOW, 0o600) do |file|
|
|
151
|
+
file.write(JSON.generate('pid' => Process.pid, 'name' => name, 'run_id' => run_id, 'started_at' => started_at))
|
|
152
|
+
end
|
|
153
|
+
@run_id = run_id
|
|
154
|
+
true
|
|
155
|
+
rescue Errno::EEXIST
|
|
156
|
+
false
|
|
157
|
+
end
|
|
158
|
+
end
|
|
159
|
+
end
|
|
160
|
+
end
|
|
@@ -0,0 +1,122 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require 'fileutils'
|
|
4
|
+
require 'json'
|
|
5
|
+
require 'securerandom'
|
|
6
|
+
require_relative 'base'
|
|
7
|
+
require_relative 'kind'
|
|
8
|
+
|
|
9
|
+
module RubyRaider
|
|
10
|
+
module Schedules
|
|
11
|
+
# <kind dir>/<name>/runs.jsonl: one JSON line per run, oldest first. `raider schedule run` appends to it
|
|
12
|
+
# (status and counts only, never a message).
|
|
13
|
+
# :reek:TooManyConstants { max_constants: 7 } -- the read cap, the line cap, the files and the open flags
|
|
14
|
+
class RunLog
|
|
15
|
+
MAX_BYTES = 1024 * 1024
|
|
16
|
+
MAX_LINES = 50
|
|
17
|
+
FILE = 'runs.jsonl'
|
|
18
|
+
# A busy run appends without the run lock: appends and the trim at MAX_LINES take this one. It is never
|
|
19
|
+
# renamed (the trim renames a new runs.jsonl into place), so a lock on it keeps excluding.
|
|
20
|
+
LOCK = 'runs.jsonl.lock'
|
|
21
|
+
NOFOLLOW = defined?(File::NOFOLLOW) ? File::NOFOLLOW : 0
|
|
22
|
+
NONBLOCK = defined?(File::NONBLOCK) ? File::NONBLOCK : 0
|
|
23
|
+
|
|
24
|
+
# A 0700 folder <dir>/<name> inside the project; no part of it may be a link. Returns its path.
|
|
25
|
+
def self.private_dir(project, *parts)
|
|
26
|
+
path = project.root
|
|
27
|
+
parts.flat_map { |part| part.split('/') }.each do |part|
|
|
28
|
+
path = File.join(path, part)
|
|
29
|
+
FileUtils.mkdir_p(path)
|
|
30
|
+
raise Error, "#{path.delete_prefix("#{project.root}/")} must be a folder, not a link" unless File.lstat(path).directory?
|
|
31
|
+
end
|
|
32
|
+
File.chmod(0o700, path)
|
|
33
|
+
path
|
|
34
|
+
end
|
|
35
|
+
|
|
36
|
+
def initialize(project, name, dir: Kind::TESTS.dir)
|
|
37
|
+
@project = project
|
|
38
|
+
@parts = [dir, name]
|
|
39
|
+
@file = project.path("#{dir}/#{name}/#{FILE}")
|
|
40
|
+
@lock = project.path("#{dir}/#{name}/#{LOCK}")
|
|
41
|
+
end
|
|
42
|
+
|
|
43
|
+
# Every readable line in the last MAX_BYTES, oldest first; a broken line is skipped
|
|
44
|
+
def lines
|
|
45
|
+
tail.lines.filter_map do |line|
|
|
46
|
+
parsed = JSON.parse(line)
|
|
47
|
+
parsed if parsed.is_a?(Hash)
|
|
48
|
+
rescue JSON::ParserError
|
|
49
|
+
nil
|
|
50
|
+
end
|
|
51
|
+
end
|
|
52
|
+
|
|
53
|
+
def last = lines.last
|
|
54
|
+
|
|
55
|
+
# The last run that ran: a busy line only says another run held the lock
|
|
56
|
+
def last_result = lines.reverse.find { |line| line['status'] != 'busy' }
|
|
57
|
+
|
|
58
|
+
def find(run_id) = lines.reverse.find { |line| line['run_id'] == run_id }
|
|
59
|
+
|
|
60
|
+
# Adds one line, keeping the last MAX_LINES. An append while there is room; otherwise the kept lines are
|
|
61
|
+
# written to a temp file that replaces the log, so a reader never sees half of it. Never through a link.
|
|
62
|
+
def append(line)
|
|
63
|
+
RunLog.private_dir(@project, *@parts)
|
|
64
|
+
locked do
|
|
65
|
+
# A line cut short by a crash is ended first, so the new line stays whole
|
|
66
|
+
raw = tail
|
|
67
|
+
text = "#{JSON.generate(line)}\n"
|
|
68
|
+
text = "\n#{text}" unless raw.empty? || raw.end_with?("\n")
|
|
69
|
+
old = raw.lines
|
|
70
|
+
old.size < MAX_LINES ? add(text) : replace(old.last(MAX_LINES - 1).join + text)
|
|
71
|
+
end
|
|
72
|
+
end
|
|
73
|
+
|
|
74
|
+
private
|
|
75
|
+
|
|
76
|
+
# Holds runs.jsonl.lock (a regular file, never through a link) while the block reads and writes the log
|
|
77
|
+
def locked
|
|
78
|
+
File.open(@lock, File::RDWR | File::CREAT | NOFOLLOW | NONBLOCK, 0o600) do |file|
|
|
79
|
+
raise Error, "#{LOCK} must be a file" unless file.stat.file?
|
|
80
|
+
|
|
81
|
+
file.flock(File::LOCK_EX)
|
|
82
|
+
yield
|
|
83
|
+
end
|
|
84
|
+
rescue Errno::ELOOP, Errno::EMLINK
|
|
85
|
+
raise Error, "#{LOCK} must be a file, not a link"
|
|
86
|
+
end
|
|
87
|
+
|
|
88
|
+
def add(text)
|
|
89
|
+
File.open(@file, File::WRONLY | File::APPEND | File::CREAT | NOFOLLOW | NONBLOCK, 0o600) do |file|
|
|
90
|
+
raise Error, "#{FILE} must be a file" unless file.stat.file?
|
|
91
|
+
|
|
92
|
+
file.write(text)
|
|
93
|
+
end
|
|
94
|
+
rescue Errno::ELOOP, Errno::EMLINK
|
|
95
|
+
raise Error, "#{FILE} must be a file, not a link"
|
|
96
|
+
end
|
|
97
|
+
|
|
98
|
+
def replace(text)
|
|
99
|
+
temp = "#{@file}.#{SecureRandom.hex(6)}.tmp"
|
|
100
|
+
File.open(temp, File::WRONLY | File::CREAT | File::EXCL | NOFOLLOW, 0o600) { |file| file.write(text) }
|
|
101
|
+
File.rename(temp, @file)
|
|
102
|
+
ensure
|
|
103
|
+
FileUtils.rm_f(temp) if temp && File.exist?(temp)
|
|
104
|
+
end
|
|
105
|
+
|
|
106
|
+
# Read through one handle that is not a link, so the checks and the read see the same file
|
|
107
|
+
def tail
|
|
108
|
+
File.open(@file, File::RDONLY | NONBLOCK | NOFOLLOW) do |file|
|
|
109
|
+
stat = file.stat
|
|
110
|
+
return '' unless stat.file?
|
|
111
|
+
|
|
112
|
+
skip = [stat.size - MAX_BYTES, 0].max
|
|
113
|
+
file.seek(skip)
|
|
114
|
+
text = file.read.to_s.force_encoding(Encoding::UTF_8)
|
|
115
|
+
skip.positive? ? text.partition("\n").last : text
|
|
116
|
+
end
|
|
117
|
+
rescue SystemCallError
|
|
118
|
+
''
|
|
119
|
+
end
|
|
120
|
+
end
|
|
121
|
+
end
|
|
122
|
+
end
|
|
@@ -0,0 +1,139 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require 'fileutils'
|
|
4
|
+
require 'json'
|
|
5
|
+
require 'securerandom'
|
|
6
|
+
require 'time'
|
|
7
|
+
require_relative 'base'
|
|
8
|
+
require_relative 'run_log'
|
|
9
|
+
|
|
10
|
+
module RubyRaider
|
|
11
|
+
module Schedules
|
|
12
|
+
# raider-results/run.json of the run that just ended, as raider_reporter wrote it when rspec finished.
|
|
13
|
+
# A run that died before that leaves the previous run's file, so it counts only when its run started at
|
|
14
|
+
# (or up to SLACK before) the spawn; without a start time, when the file is not older than the spawn.
|
|
15
|
+
# raider-results/ is only read, never changed, and nothing in it is read through a link.
|
|
16
|
+
# :reek:TooManyConstants { max_constants: 9 } -- the folder, its files, the caps and the slack
|
|
17
|
+
class RunReport
|
|
18
|
+
DIR = 'raider-results'
|
|
19
|
+
RUN = 'run.json'
|
|
20
|
+
INDEX = 'index.html'
|
|
21
|
+
ATTACHMENTS = 'attachments'
|
|
22
|
+
SLACK = 2
|
|
23
|
+
MAX_RUN_BYTES = 256 * 1024 * 1024
|
|
24
|
+
MAX_ATTACHMENT_BYTES = 50 * 1024 * 1024
|
|
25
|
+
# Past these the attachments are left out of the copy (run.json and index.html still go)
|
|
26
|
+
MAX_ATTACHMENT_DEPTH = 8
|
|
27
|
+
MAX_ATTACHMENT_FILES = 1000
|
|
28
|
+
|
|
29
|
+
attr_reader :counts
|
|
30
|
+
|
|
31
|
+
# The report of the run spawned at since, nil when there is none
|
|
32
|
+
def self.load(project, since:)
|
|
33
|
+
dir = project.path(DIR)
|
|
34
|
+
return nil unless File.lstat(dir).directory?
|
|
35
|
+
|
|
36
|
+
bytes, mtime = read_file(File.join(dir, RUN), MAX_RUN_BYTES)
|
|
37
|
+
data = bytes && JSON.parse(bytes)
|
|
38
|
+
return nil unless data.is_a?(Hash) && data['results'].is_a?(Array) && fresh?(data['run'], mtime, since)
|
|
39
|
+
|
|
40
|
+
new(dir, bytes, data['results'])
|
|
41
|
+
rescue SystemCallError, JSON::ParserError
|
|
42
|
+
nil
|
|
43
|
+
end
|
|
44
|
+
|
|
45
|
+
# [content, mtime] of a regular file opened without following a link, nil when it is not one or too big
|
|
46
|
+
def self.read_file(path, max)
|
|
47
|
+
File.open(path, File::RDONLY | RunLog::NOFOLLOW | RunLog::NONBLOCK) do |file|
|
|
48
|
+
stat = file.stat
|
|
49
|
+
stat.file? && stat.size <= max ? [file.read.to_s, stat.mtime] : nil
|
|
50
|
+
end
|
|
51
|
+
rescue SystemCallError
|
|
52
|
+
nil
|
|
53
|
+
end
|
|
54
|
+
|
|
55
|
+
def self.fresh?(run, mtime, since)
|
|
56
|
+
started = run.is_a?(Hash) ? run['started_at'] : nil
|
|
57
|
+
return mtime >= since - SLACK unless started.is_a?(String)
|
|
58
|
+
|
|
59
|
+
Time.iso8601(started) >= since - SLACK
|
|
60
|
+
rescue ArgumentError
|
|
61
|
+
false
|
|
62
|
+
end
|
|
63
|
+
|
|
64
|
+
def initialize(dir, run_json, results)
|
|
65
|
+
@dir = dir
|
|
66
|
+
@run_json = run_json
|
|
67
|
+
statuses = results.map { |result| result.is_a?(Hash) ? result['status'] : nil }
|
|
68
|
+
@counts = { 'total' => statuses.size, 'passed' => statuses.count('passed'),
|
|
69
|
+
'failed' => statuses.count { |status| %w[failed quarantined].include?(status) },
|
|
70
|
+
'skipped' => statuses.count { |status| %w[pending skipped].include?(status) } }.freeze
|
|
71
|
+
end
|
|
72
|
+
|
|
73
|
+
# Copies run.json, index.html and (when they come to at most MAX_ATTACHMENT_BYTES) the attachments to
|
|
74
|
+
# target, replacing what was there: built in a temp folder beside it, then renamed into place.
|
|
75
|
+
def copy_to(target)
|
|
76
|
+
temp = "#{target}.#{SecureRandom.hex(6)}.tmp"
|
|
77
|
+
Dir.mkdir(temp, 0o700)
|
|
78
|
+
File.binwrite(File.join(temp, RUN), @run_json)
|
|
79
|
+
index = RunReport.read_file(File.join(@dir, INDEX), MAX_RUN_BYTES)
|
|
80
|
+
File.binwrite(File.join(temp, INDEX), index.first) if index
|
|
81
|
+
copy_attachments(temp)
|
|
82
|
+
swap(temp, target)
|
|
83
|
+
true
|
|
84
|
+
ensure
|
|
85
|
+
FileUtils.rm_rf(temp) if temp && File.exist?(temp)
|
|
86
|
+
end
|
|
87
|
+
|
|
88
|
+
private
|
|
89
|
+
|
|
90
|
+
def copy_attachments(temp)
|
|
91
|
+
files = regular_files(File.join(@dir, ATTACHMENTS))
|
|
92
|
+
return unless files && files.sum { |_, size| size } <= MAX_ATTACHMENT_BYTES
|
|
93
|
+
|
|
94
|
+
FileUtils.mkdir_p(File.join(temp, ATTACHMENTS))
|
|
95
|
+
files.each { |relative, size| copy_file(File.join(@dir, ATTACHMENTS, relative), File.join(temp, ATTACHMENTS, relative), size) }
|
|
96
|
+
end
|
|
97
|
+
|
|
98
|
+
# Streams one file that is still a regular file when opened (not a link), at most the size counted for
|
|
99
|
+
# it, so a file that grew since does not take the copy past the cap; anything else is skipped
|
|
100
|
+
def copy_file(source, target, size)
|
|
101
|
+
File.open(source, File::RDONLY | RunLog::NOFOLLOW | RunLog::NONBLOCK) do |from|
|
|
102
|
+
next unless from.stat.file?
|
|
103
|
+
|
|
104
|
+
FileUtils.mkdir_p(File.dirname(target))
|
|
105
|
+
File.open(target, File::WRONLY | File::CREAT | File::EXCL | RunLog::NOFOLLOW, 0o600) { |to| IO.copy_stream(from, to, size) }
|
|
106
|
+
end
|
|
107
|
+
rescue Errno::ELOOP, Errno::EMLINK, Errno::ENOENT
|
|
108
|
+
nil
|
|
109
|
+
end
|
|
110
|
+
|
|
111
|
+
# {relative path => size} of the regular files under dir (links and anything else skipped); nil once
|
|
112
|
+
# they pass a cap (bytes, files or folder depth) or when dir is not a folder
|
|
113
|
+
def regular_files(dir, prefix = nil, found = {}, depth = 0)
|
|
114
|
+
return nil if depth > MAX_ATTACHMENT_DEPTH || !File.lstat(dir).directory?
|
|
115
|
+
|
|
116
|
+
Dir.children(dir).sort.each do |child|
|
|
117
|
+
relative = prefix ? File.join(prefix, child) : child
|
|
118
|
+
stat = File.lstat(File.join(dir, child))
|
|
119
|
+
if stat.directory? then return nil unless regular_files(File.join(dir, child), relative, found, depth + 1)
|
|
120
|
+
elsif stat.file? then found[relative] = stat.size
|
|
121
|
+
end
|
|
122
|
+
return nil if found.size > MAX_ATTACHMENT_FILES || found.sum { |_, size| size } > MAX_ATTACHMENT_BYTES
|
|
123
|
+
end
|
|
124
|
+
found
|
|
125
|
+
rescue SystemCallError
|
|
126
|
+
nil
|
|
127
|
+
end
|
|
128
|
+
|
|
129
|
+
# The old copy moves aside first, so target is never half old and half new
|
|
130
|
+
def swap(temp, target)
|
|
131
|
+
old = "#{target}.#{SecureRandom.hex(6)}.old"
|
|
132
|
+
File.rename(target, old) if File.symlink?(target) || File.exist?(target)
|
|
133
|
+
File.rename(temp, target)
|
|
134
|
+
ensure
|
|
135
|
+
FileUtils.rm_rf(old) if old && (File.symlink?(old) || File.exist?(old))
|
|
136
|
+
end
|
|
137
|
+
end
|
|
138
|
+
end
|
|
139
|
+
end
|