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,216 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require 'securerandom'
|
|
4
|
+
require 'time'
|
|
5
|
+
require_relative 'base'
|
|
6
|
+
require_relative 'project'
|
|
7
|
+
require_relative 'schedule_store'
|
|
8
|
+
require_relative 'run_log'
|
|
9
|
+
require_relative 'run_lock'
|
|
10
|
+
require_relative 'run_report'
|
|
11
|
+
require_relative 'notifier'
|
|
12
|
+
require_relative 'spawn'
|
|
13
|
+
|
|
14
|
+
module RubyRaider
|
|
15
|
+
module Schedules
|
|
16
|
+
# `raider schedule run NAME`: what the OS entry runs, and what a person can run by hand. It owns the whole
|
|
17
|
+
# run record (spec §4): it re-validates the entry, takes the lock before rspec spawns (busy never wipes
|
|
18
|
+
# raider-results/), runs `bundle exec rspec` of the selection with retries and headless set through the
|
|
19
|
+
# env, counts raider-results/run.json, appends the run line, copies the report, cleans up and notifies.
|
|
20
|
+
# Exit 0 passed, 1 failed, 2 setup_error (or refused), 3 busy.
|
|
21
|
+
# :reek:TooManyInstanceVariables { max_instance_variables: 7 } -- the project and the injected collaborators
|
|
22
|
+
# :reek:TooManyConstants { max_constants: 6 } -- the exit codes, triggers, results folder, log cap and wording
|
|
23
|
+
class RunWrapper
|
|
24
|
+
EXIT_CODES = { 'passed' => 0, 'failed' => 1, 'setup_error' => 2, 'busy' => 3 }.freeze
|
|
25
|
+
TRIGGERS = %w[manual schedule].freeze
|
|
26
|
+
LOG_BYTES = 1024 * 1024
|
|
27
|
+
MISSING = 'remove the schedule or add it again'
|
|
28
|
+
ZERO = { 'total' => 0, 'passed' => 0, 'failed' => 0, 'skipped' => 0 }.freeze
|
|
29
|
+
# Forward a wrapper INT only when it can't have come from a terminal Ctrl-C, which rspec gets too
|
|
30
|
+
SPAWNER = ->(env, argv, chdir) { Spawn.call(env, argv, chdir, forward_int: !$stdin.tty?) }
|
|
31
|
+
|
|
32
|
+
# spawner: (env, argv, chdir) -> [exit status or nil, stopped]; notifier: nil makes one only when needed
|
|
33
|
+
def initialize(project, out: $stdout, err: $stderr, spawner: SPAWNER, clock: -> { Time.now }, notifier: nil)
|
|
34
|
+
@project = project
|
|
35
|
+
@out = out
|
|
36
|
+
@err = err
|
|
37
|
+
@spawner = spawner
|
|
38
|
+
@clock = clock
|
|
39
|
+
@notifier = notifier
|
|
40
|
+
@lock = RunLock.new(project)
|
|
41
|
+
end
|
|
42
|
+
|
|
43
|
+
# The exit code. Raises Error (exit 2, no run line) for an unknown trigger or a schedule that is not in
|
|
44
|
+
# schedules.yml or no longer valid there.
|
|
45
|
+
def run(name, trigger: 'manual')
|
|
46
|
+
raise Error, "Invalid trigger #{Schedules.printable(trigger.inspect)}: use #{TRIGGERS.join(' or ')}" unless TRIGGERS.include?(trigger)
|
|
47
|
+
|
|
48
|
+
schedule = load(name)
|
|
49
|
+
rotate_log(schedule) if trigger == 'schedule'
|
|
50
|
+
record = { 'run_id' => SecureRandom.hex(8), 'started_at' => stamp, 'trigger' => trigger }
|
|
51
|
+
outcome = attempt(schedule, record)
|
|
52
|
+
finish(schedule, record, outcome)
|
|
53
|
+
end
|
|
54
|
+
|
|
55
|
+
private
|
|
56
|
+
|
|
57
|
+
def load(name)
|
|
58
|
+
schedule = ScheduleStore.new(@project).find(name) if Schedules.valid_name?(name)
|
|
59
|
+
return schedule if schedule
|
|
60
|
+
|
|
61
|
+
raise Error, "No test schedule #{Schedules.printable(name.inspect)} in #{ScheduleStore::DIR}/#{Store::FILE} (or it is not valid): " \
|
|
62
|
+
'see raider schedule list'
|
|
63
|
+
end
|
|
64
|
+
|
|
65
|
+
# {status, counts, report?, reason?, stopped?}; the lock is held from before the spawn to the run line
|
|
66
|
+
def attempt(schedule, record)
|
|
67
|
+
outcome = {}
|
|
68
|
+
schedule.selection.check!(@project.root)
|
|
69
|
+
return { status: 'busy' } unless @lock.acquire(name: schedule.name, run_id: record['run_id'], started_at: record['started_at'])
|
|
70
|
+
|
|
71
|
+
outcome = spawn(schedule)
|
|
72
|
+
outcome[:copied] = copy_report(schedule, outcome[:report])
|
|
73
|
+
# A line that can't be written loses only the line: the stop, counts and exit code stay
|
|
74
|
+
append_quietly(schedule, record, outcome)
|
|
75
|
+
outcome[:recorded] = true
|
|
76
|
+
outcome
|
|
77
|
+
rescue Error, SystemCallError => e
|
|
78
|
+
{ status: 'setup_error', reason: Schedules.printable(e.message) }
|
|
79
|
+
rescue SignalException
|
|
80
|
+
stopped(schedule, record, outcome)
|
|
81
|
+
ensure
|
|
82
|
+
@lock.release
|
|
83
|
+
end
|
|
84
|
+
|
|
85
|
+
# INT or TERM outside the spawn's traps (the check, the lock, the report copy) is a stop: finish records it
|
|
86
|
+
# and the ensure frees the lock and running.json. A run whose line is written already keeps that outcome.
|
|
87
|
+
def stopped(schedule, record, outcome)
|
|
88
|
+
return outcome.merge(recorded: true) if outcome[:recorded] || RunLog.new(@project, schedule.name).find(record['run_id'])
|
|
89
|
+
|
|
90
|
+
outcome.merge(status: 'setup_error', stopped: true)
|
|
91
|
+
end
|
|
92
|
+
|
|
93
|
+
def spawn(schedule)
|
|
94
|
+
since = @clock.call
|
|
95
|
+
code, stopped = @spawner.call(env(schedule), argv(schedule), @project.root)
|
|
96
|
+
report = RunReport.load(@project, since: since)
|
|
97
|
+
{ status: status(code, stopped, report&.counts), report: report, stopped: stopped }
|
|
98
|
+
end
|
|
99
|
+
|
|
100
|
+
# A stop wins over everything: an interrupted rspec exits 1 even when what it ran passed
|
|
101
|
+
def status(code, stopped, counts)
|
|
102
|
+
return 'setup_error' if stopped || counts.nil?
|
|
103
|
+
return 'passed' if code&.zero? && counts['total'].positive?
|
|
104
|
+
return 'failed' if code == 1 && counts['failed'].positive?
|
|
105
|
+
|
|
106
|
+
'setup_error'
|
|
107
|
+
end
|
|
108
|
+
|
|
109
|
+
def argv(schedule)
|
|
110
|
+
selection = schedule.selection
|
|
111
|
+
paths = case selection.kind
|
|
112
|
+
when 'tag' then ['--tag', selection.tag.to_s]
|
|
113
|
+
when 'files' then selection.files
|
|
114
|
+
else []
|
|
115
|
+
end
|
|
116
|
+
[schedule.ruby, schedule.bundle, 'exec', 'rspec', *paths]
|
|
117
|
+
end
|
|
118
|
+
|
|
119
|
+
# Spec §4a: the inherited values are dropped first (nil unsets), then retries and headless are set.
|
|
120
|
+
# config/config.yml is never written.
|
|
121
|
+
def env(schedule)
|
|
122
|
+
show = schedule.show_browser
|
|
123
|
+
gem_env(schedule).merge('BUNDLE_GEMFILE' => @project.path('Gemfile'), 'RETRY_COUNT' => (schedule.retries + 1).to_s,
|
|
124
|
+
'RSPEC_RETRY_RETRY_COUNT' => nil, 'HEADLESS' => show ? nil : 'true', 'RAIDER_SHOW_BROWSER' => show ? '1' : nil)
|
|
125
|
+
end
|
|
126
|
+
|
|
127
|
+
# The GEM_HOME and GEM_PATH captured at add, as the OS entry sets them (a key not captured is unset), so
|
|
128
|
+
# a run by hand from another Ruby uses the scheduled run's gems. Nothing captured: the caller's stay.
|
|
129
|
+
def gem_env(schedule)
|
|
130
|
+
captured = schedule.gem_env
|
|
131
|
+
captured.empty? ? {} : Rule::GEM_ENV.to_h { |key| [key, captured[key]] }
|
|
132
|
+
end
|
|
133
|
+
|
|
134
|
+
# The report's folder relative to the project, nil when there was no report of this run or it could not be copied
|
|
135
|
+
def copy_report(schedule, report)
|
|
136
|
+
return nil unless report
|
|
137
|
+
|
|
138
|
+
relative = "#{Kind::TESTS.dir}/#{schedule.name}/last"
|
|
139
|
+
RunLog.private_dir(@project, Kind::TESTS.dir, schedule.name)
|
|
140
|
+
report.copy_to(@project.path(relative))
|
|
141
|
+
relative
|
|
142
|
+
rescue Error, SystemCallError => e
|
|
143
|
+
@err.puts("Could not copy the report to #{relative}: #{Schedules.printable(e.message)}")
|
|
144
|
+
nil
|
|
145
|
+
end
|
|
146
|
+
|
|
147
|
+
def append(schedule, record, outcome)
|
|
148
|
+
line = { 'run_id' => record['run_id'], 'started_at' => record['started_at'], 'finished_at' => stamp, 'status' => outcome[:status],
|
|
149
|
+
'counts' => outcome[:report]&.counts || ZERO, 'trigger' => record['trigger'], 'results_dir' => RunReport::DIR }
|
|
150
|
+
line['report_copy'] = outcome[:copied] if outcome[:copied]
|
|
151
|
+
RunLog.new(@project, schedule.name).append(line)
|
|
152
|
+
end
|
|
153
|
+
|
|
154
|
+
# Busy and early setup errors get their line here, after the lock is gone; the rest have one already
|
|
155
|
+
def finish(schedule, record, outcome)
|
|
156
|
+
status = outcome[:status]
|
|
157
|
+
append_quietly(schedule, record, outcome) unless outcome[:recorded]
|
|
158
|
+
@out.puts("Schedule #{Schedules.printable(schedule.name)} #{result_text(schedule, record, outcome)}")
|
|
159
|
+
notify(schedule, record, outcome) if schedule.notify && !outcome[:stopped] && %w[failed setup_error].include?(status)
|
|
160
|
+
EXIT_CODES.fetch(status)
|
|
161
|
+
end
|
|
162
|
+
|
|
163
|
+
def append_quietly(schedule, record, outcome)
|
|
164
|
+
append(schedule, record, outcome)
|
|
165
|
+
rescue Error, SystemCallError => e
|
|
166
|
+
@err.puts("Could not record the run: #{Schedules.printable(e.message)}")
|
|
167
|
+
end
|
|
168
|
+
|
|
169
|
+
def result_text(schedule, record, outcome)
|
|
170
|
+
counts = outcome[:report]&.counts || ZERO
|
|
171
|
+
return 'was stopped' if outcome[:stopped]
|
|
172
|
+
|
|
173
|
+
case outcome[:status]
|
|
174
|
+
when 'passed' then "passed: #{tests(counts['total'])}"
|
|
175
|
+
when 'failed' then "failed: #{failures(counts)}"
|
|
176
|
+
when 'busy' then 'is already running'
|
|
177
|
+
else "could not start: #{start_reason(schedule, record, outcome, long: true)}"
|
|
178
|
+
end
|
|
179
|
+
end
|
|
180
|
+
|
|
181
|
+
def failures(counts) = "#{counts['failed']} of #{tests(counts['total'])} failed"
|
|
182
|
+
|
|
183
|
+
def tests(total) = total == 1 ? '1 test' : "#{total} tests"
|
|
184
|
+
|
|
185
|
+
# The notifier never raises, so a notification can't change the exit code
|
|
186
|
+
def notify(schedule, record, outcome)
|
|
187
|
+
counts = outcome[:report]&.counts || ZERO
|
|
188
|
+
body = outcome[:status] == 'failed' ? failures(counts) : "Could not start: #{start_reason(schedule, record, outcome, long: false)}"
|
|
189
|
+
(@notifier ||= Notifier.new).notify("Ruby Raider: #{schedule.name} failed", body)
|
|
190
|
+
end
|
|
191
|
+
|
|
192
|
+
# A refusal names itself ("spec/x_spec.rb not found"); a run that started points at its output. Only a
|
|
193
|
+
# scheduled run outside Windows has a scheduler.log: Windows tasks keep none.
|
|
194
|
+
def start_reason(schedule, record, outcome, long:)
|
|
195
|
+
reason = outcome[:reason]
|
|
196
|
+
return long && reason.end_with?(' not found') ? "#{reason}: #{MISSING}" : reason if reason
|
|
197
|
+
return "see #{Kind::TESTS.dir}/#{schedule.name}/scheduler.log" if record['trigger'] == 'schedule' && !Gem.win_platform?
|
|
198
|
+
|
|
199
|
+
'see the run output'
|
|
200
|
+
end
|
|
201
|
+
|
|
202
|
+
# launchd and cron append to scheduler.log through a handle opened before this process started. Renaming
|
|
203
|
+
# the file keeps that handle (POSIX), so the rest of this run's output lands in scheduler.log.1 and the
|
|
204
|
+
# next run starts a new scheduler.log. A link or anything but a regular file is left alone.
|
|
205
|
+
def rotate_log(schedule)
|
|
206
|
+
log = @project.path("#{Kind::TESTS.dir}/#{schedule.name}/scheduler.log")
|
|
207
|
+
stat = File.lstat(log)
|
|
208
|
+
File.rename(log, "#{log}.1") if stat.file? && stat.size > LOG_BYTES
|
|
209
|
+
rescue SystemCallError
|
|
210
|
+
nil
|
|
211
|
+
end
|
|
212
|
+
|
|
213
|
+
def stamp = @clock.call.getutc.iso8601(3)
|
|
214
|
+
end
|
|
215
|
+
end
|
|
216
|
+
end
|
|
@@ -0,0 +1,89 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require 'rbconfig'
|
|
4
|
+
require_relative 'rule'
|
|
5
|
+
require_relative 'selection'
|
|
6
|
+
require_relative 'project'
|
|
7
|
+
|
|
8
|
+
module RubyRaider
|
|
9
|
+
module Schedules
|
|
10
|
+
# One scheduled test run: the shared rule (Rule) plus which tests run and how (show_browser). The OS
|
|
11
|
+
# entry runs `raider schedule run NAME` only; the selection, retries, notify and show_browser are
|
|
12
|
+
# read from schedules.yml at run time. Immutable.
|
|
13
|
+
# :reek:TooManyInstanceVariables { max_instance_variables: 14 } -- the fields of a schedules.yml entry
|
|
14
|
+
class Schedule < Rule
|
|
15
|
+
KIND = Kind::TESTS
|
|
16
|
+
KEYS = %w[name every at on retries notify show_browser selection os_id ruby bundle gem_env created_at].freeze
|
|
17
|
+
LINUX = /linux/
|
|
18
|
+
NO_BROWSER_ON_LINUX = "Show the browser isn't available for scheduled runs on Linux"
|
|
19
|
+
|
|
20
|
+
attr_reader :show_browser, :selection
|
|
21
|
+
|
|
22
|
+
def self.kind = KIND
|
|
23
|
+
|
|
24
|
+
# Linux schedules run from systemd or cron, with no display: show_browser is refused there
|
|
25
|
+
def self.build(project:, name:, every:, at: nil, on: nil, retries: 0, notify: false, show_browser: false, selection: Selection.all,
|
|
26
|
+
now: Time.now, host_os: RbConfig::CONFIG['host_os'])
|
|
27
|
+
raise Error, NO_BROWSER_ON_LINUX if show_browser == true && host_os.match?(LINUX)
|
|
28
|
+
|
|
29
|
+
root = project.root
|
|
30
|
+
new(name: name, every: every, at: at, on: on, retries: retries, notify: notify, show_browser: show_browser, selection: selection,
|
|
31
|
+
os_id: os_id_for(root, name), ruby: RbConfig.ruby, bundle: bundle_path, gem_env: captured_gem_env,
|
|
32
|
+
project_root: root, created_at: now.getutc.iso8601)
|
|
33
|
+
end
|
|
34
|
+
|
|
35
|
+
# A schedules.yml entry; raises Error when it is not one this class wrote. A spec file that is gone
|
|
36
|
+
# still loads (selection.missing_file names it).
|
|
37
|
+
def self.from_h(hash, project_root, host_os: RbConfig::CONFIG['host_os'])
|
|
38
|
+
raise Error, 'A schedule entry must be a mapping' unless hash.is_a?(Hash)
|
|
39
|
+
|
|
40
|
+
fields = KEYS.to_h { |key| [key.to_sym, hash[key]] }
|
|
41
|
+
fields[:selection] = Selection.from_h(hash['selection'], root: project_root, host_os: host_os)
|
|
42
|
+
schedule = new(**fields, project_root: project_root)
|
|
43
|
+
# Strict: an entry copied from another project, or a moved project, is not this project's entry
|
|
44
|
+
raise Error, "Invalid os_id #{schedule.os_id.inspect} for #{schedule.name} in #{project_root}" unless schedule.os_id == os_id_for(project_root, schedule.name)
|
|
45
|
+
|
|
46
|
+
schedule
|
|
47
|
+
end
|
|
48
|
+
|
|
49
|
+
def initialize(name:, every:, at:, on:, retries:, selection:, os_id:, ruby:, bundle:, gem_env:, project_root:, created_at:, notify: false,
|
|
50
|
+
show_browser: false)
|
|
51
|
+
super()
|
|
52
|
+
@name = checked_name(name)
|
|
53
|
+
@every = checked_every(every)
|
|
54
|
+
@at = checked_at(at)
|
|
55
|
+
@on = checked_on(on)
|
|
56
|
+
@retries = checked_retries(retries)
|
|
57
|
+
@notify = checked_flag('notify', notify)
|
|
58
|
+
@show_browser = checked_flag('show_browser', show_browser)
|
|
59
|
+
@selection = checked_selection(selection)
|
|
60
|
+
@os_id = checked_os_id(os_id)
|
|
61
|
+
@ruby = checked_path('ruby', ruby)
|
|
62
|
+
@bundle = checked_path('bundle', bundle)
|
|
63
|
+
@gem_env = checked_gem_env(gem_env)
|
|
64
|
+
@project_root = checked_path('project_root', project_root)
|
|
65
|
+
@created_at = checked_text('created_at', created_at)
|
|
66
|
+
freeze
|
|
67
|
+
end
|
|
68
|
+
|
|
69
|
+
def to_h
|
|
70
|
+
{ 'name' => name, 'every' => every, 'at' => at, 'on' => on, 'retries' => retries, 'notify' => notify, 'show_browser' => show_browser,
|
|
71
|
+
'selection' => selection.to_h, 'os_id' => os_id, 'ruby' => ruby, 'bundle' => bundle, 'gem_env' => gem_env.dup, 'created_at' => created_at }
|
|
72
|
+
end
|
|
73
|
+
|
|
74
|
+
private
|
|
75
|
+
|
|
76
|
+
def checked_name(name)
|
|
77
|
+
return name.dup.freeze if name.is_a?(String) && KIND.valid_name?(name)
|
|
78
|
+
|
|
79
|
+
raise Error, "Invalid schedule name #{Schedules.printable(name.inspect)}: use 1 to 60 of a-z, 0-9 and _"
|
|
80
|
+
end
|
|
81
|
+
|
|
82
|
+
def checked_selection(selection)
|
|
83
|
+
return selection if selection.is_a?(Selection)
|
|
84
|
+
|
|
85
|
+
raise Error, 'Invalid selection: use a Selection'
|
|
86
|
+
end
|
|
87
|
+
end
|
|
88
|
+
end
|
|
89
|
+
end
|
|
@@ -0,0 +1,53 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require_relative 'store'
|
|
4
|
+
require_relative 'schedule'
|
|
5
|
+
|
|
6
|
+
module RubyRaider
|
|
7
|
+
module Schedules
|
|
8
|
+
# .raider/schedules/schedules.yml: the project's test schedules (Store with Schedule)
|
|
9
|
+
class ScheduleStore < Store
|
|
10
|
+
DIR = Schedule::KIND.dir
|
|
11
|
+
GITIGNORE_LINE = "#{DIR}/".freeze
|
|
12
|
+
|
|
13
|
+
def initialize(project)
|
|
14
|
+
super(project, Schedule)
|
|
15
|
+
end
|
|
16
|
+
|
|
17
|
+
# Appends .raider/schedules/ to the project's .gitignore (made when missing) unless that exact line is
|
|
18
|
+
# there; every byte already in the file is kept. The yml holds machine paths. true when it added it.
|
|
19
|
+
# Read and written through one handle that is not a link, so a linked .gitignore is never written through.
|
|
20
|
+
def ignore_in_git
|
|
21
|
+
File.open(gitignore, File::RDWR | File::APPEND | File::CREAT | NOFOLLOW | NONBLOCK, 0o644, binmode: true) do |file|
|
|
22
|
+
content = gitignore_text(file)
|
|
23
|
+
return false if ignored?(content)
|
|
24
|
+
|
|
25
|
+
prefix = content.empty? || content.end_with?("\n") ? '' : "\n"
|
|
26
|
+
file.write("#{prefix}#{GITIGNORE_LINE}\n")
|
|
27
|
+
end
|
|
28
|
+
true
|
|
29
|
+
rescue Errno::EACCES, Errno::EROFS
|
|
30
|
+
# A .gitignore we may not write is fine when it already has the line
|
|
31
|
+
return false if ignored?(File.open(gitignore, File::RDONLY | NOFOLLOW | NONBLOCK, binmode: true) { |file| gitignore_text(file) })
|
|
32
|
+
|
|
33
|
+
raise
|
|
34
|
+
rescue Errno::ELOOP, Errno::EMLINK
|
|
35
|
+
raise Error, ".gitignore is a link: add #{GITIGNORE_LINE} to it yourself"
|
|
36
|
+
rescue Errno::EISDIR
|
|
37
|
+
raise Error, '.gitignore must be a file'
|
|
38
|
+
end
|
|
39
|
+
|
|
40
|
+
private
|
|
41
|
+
|
|
42
|
+
def gitignore = project.path('.gitignore')
|
|
43
|
+
|
|
44
|
+
def gitignore_text(file)
|
|
45
|
+
raise Error, '.gitignore must be a file' unless file.stat.file?
|
|
46
|
+
|
|
47
|
+
file.read.to_s.b
|
|
48
|
+
end
|
|
49
|
+
|
|
50
|
+
def ignored?(content) = content.each_line.any? { |line| line.chomp == GITIGNORE_LINE }
|
|
51
|
+
end
|
|
52
|
+
end
|
|
53
|
+
end
|
|
@@ -0,0 +1,178 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require 'json'
|
|
4
|
+
require 'rbconfig'
|
|
5
|
+
require_relative 'adapter'
|
|
6
|
+
require_relative 'next_run'
|
|
7
|
+
require_relative 'os_enumerator'
|
|
8
|
+
require_relative 'project'
|
|
9
|
+
require_relative 'run_log'
|
|
10
|
+
require_relative 'schedule'
|
|
11
|
+
require_relative 'schedule_store'
|
|
12
|
+
require_relative 'scheduler_steps'
|
|
13
|
+
require_relative 'selection'
|
|
14
|
+
|
|
15
|
+
module RubyRaider
|
|
16
|
+
module Schedules
|
|
17
|
+
# `raider schedule add | remove | list`: keeps the OS scheduler entries and .raider/schedules/schedules.yml
|
|
18
|
+
# in step (4.1's order). The OS entry is installed first and the yml written after, so the yml never names
|
|
19
|
+
# an entry the OS scheduler refused; remove takes the OS entry first, so a refusal keeps the yml entry to
|
|
20
|
+
# retry with. The OS entry is always built fresh (this ruby, bundle and gem env), never from the yml.
|
|
21
|
+
# :reek:TooManyInstanceVariables { max_instance_variables: 7 } -- the project, its store, the adapter, the streams, clock and OS
|
|
22
|
+
# :reek:TooManyConstants { max_constants: 7 } -- the kind, two time formats, the row keys and the list wording
|
|
23
|
+
class Scheduler
|
|
24
|
+
include SchedulerSteps
|
|
25
|
+
|
|
26
|
+
KIND = Schedule::KIND
|
|
27
|
+
TIME = '%a %H:%M'
|
|
28
|
+
LIST_TIME = '%a %-d %b %H:%M'
|
|
29
|
+
LAST = %w[status finished_at trigger counts].freeze
|
|
30
|
+
EMPTY = 'No schedules yet: raider schedule add <name> --every day --at 08:00'
|
|
31
|
+
UNLISTED = 'missing from schedules.yml'
|
|
32
|
+
RULE_KEYS = %w[every at on retries notify show_browser selection next_run_at].freeze
|
|
33
|
+
|
|
34
|
+
# The OS scheduler for this machine, for test schedules
|
|
35
|
+
def self.adapter(runner: Os::CommandRunner.new, host_os: RbConfig::CONFIG['host_os'])
|
|
36
|
+
Schedules.adapter(kind: KIND, runner: runner, host_os: host_os)
|
|
37
|
+
end
|
|
38
|
+
|
|
39
|
+
# adapter: nil picks the OS scheduler once add has checked everything else, so a refused command never touches it
|
|
40
|
+
def initialize(project, adapter: nil, store: ScheduleStore.new(project), out: $stdout, err: $stderr, now: -> { Time.now },
|
|
41
|
+
host_os: RbConfig::CONFIG['host_os'])
|
|
42
|
+
@project = project
|
|
43
|
+
@adapter = adapter
|
|
44
|
+
@store = store
|
|
45
|
+
@out = out
|
|
46
|
+
@err = err
|
|
47
|
+
@now = now
|
|
48
|
+
@host_os = host_os
|
|
49
|
+
end
|
|
50
|
+
|
|
51
|
+
# Adds the schedule, or replaces the one with its name. Every check runs before a folder, the yml or
|
|
52
|
+
# the OS scheduler is touched.
|
|
53
|
+
def add(name, every:, files: [], tag: nil, at: nil, on: nil, retries: 0, notify: false, show_browser: false)
|
|
54
|
+
checked_name(name)
|
|
55
|
+
now = @now.call
|
|
56
|
+
selection = Selection.parse(files: files, tag: tag, root: @project.root, host_os: @host_os)
|
|
57
|
+
schedule = Schedule.build(project: @project, name: name, every: every, at: at, on: on, retries: retries, notify: notify,
|
|
58
|
+
show_browser: show_browser, selection: selection, now: now, host_os: @host_os)
|
|
59
|
+
@project.check_show_browser! if schedule.show_browser
|
|
60
|
+
@project.check_raider_version!
|
|
61
|
+
current = adapter
|
|
62
|
+
schedule_dirs(name)
|
|
63
|
+
@store.writable!
|
|
64
|
+
old = @store.find(name)
|
|
65
|
+
current.install(schedule)
|
|
66
|
+
save(schedule)
|
|
67
|
+
ignore_in_git
|
|
68
|
+
@out.puts("#{old ? 'Replaced' : 'Scheduled'} #{name}: #{schedule.rule_text}, next run #{NextRun.after(schedule, now).strftime(TIME)}")
|
|
69
|
+
end
|
|
70
|
+
|
|
71
|
+
def remove(name)
|
|
72
|
+
checked_name(name)
|
|
73
|
+
label = adapter.label
|
|
74
|
+
was_there = adapter.remove(Schedule.os_id_for(@project.root, name))
|
|
75
|
+
removed = delete(name, label)
|
|
76
|
+
raise Error, "No schedule #{name}" unless removed || was_there
|
|
77
|
+
|
|
78
|
+
@out.puts(was_there ? "Removed #{name}" : "Removed #{name} (it was already gone from #{label})")
|
|
79
|
+
@err.puts(unreadable_warning(name, label)) if removed.is_a?(Store::Removed)
|
|
80
|
+
end
|
|
81
|
+
|
|
82
|
+
# notice: the 4.1 cleanup's line (Legacy) for the JSON; as text it is already on stderr
|
|
83
|
+
def list(json: false, notice: nil)
|
|
84
|
+
now = @now.call
|
|
85
|
+
listed = @store.all
|
|
86
|
+
rows = listed.map { |schedule| row(schedule, now) } + unlisted_rows(listed.map(&:name))
|
|
87
|
+
return @out.puts(JSON.generate({ 'notice' => notice, 'schedules' => rows.map { |entry| entry[:json] } })) if json
|
|
88
|
+
|
|
89
|
+
return @out.puts(EMPTY) if rows.empty?
|
|
90
|
+
|
|
91
|
+
rows.each { |entry| @out.puts(Schedules.printable(entry[:text])) }
|
|
92
|
+
note = missed_runs_note
|
|
93
|
+
@out.puts(note) if note
|
|
94
|
+
end
|
|
95
|
+
|
|
96
|
+
private
|
|
97
|
+
|
|
98
|
+
attr_reader :store
|
|
99
|
+
|
|
100
|
+
def adapter = @adapter ||= Scheduler.adapter(host_os: @host_os)
|
|
101
|
+
|
|
102
|
+
def checked_name(name)
|
|
103
|
+
return if name.is_a?(String) && KIND.valid_name?(name)
|
|
104
|
+
|
|
105
|
+
raise Error, "Invalid schedule name #{Schedules.printable(name.inspect)}: use 1 to 60 of a-z, 0-9 and _"
|
|
106
|
+
end
|
|
107
|
+
|
|
108
|
+
# .raider/schedules/ and .raider/schedules/<name>/ (scheduler.log): 0700, no part of either a link
|
|
109
|
+
def schedule_dirs(name)
|
|
110
|
+
RunLog.private_dir(@project, KIND.dir)
|
|
111
|
+
RunLog.private_dir(@project, KIND.dir, name)
|
|
112
|
+
rescue SystemCallError => e
|
|
113
|
+
raise Error, "Could not make #{KIND.dir}/#{name}: #{e.message}"
|
|
114
|
+
end
|
|
115
|
+
|
|
116
|
+
# The schedule is installed and saved by now, so a .gitignore that can't take the line only warns
|
|
117
|
+
def ignore_in_git
|
|
118
|
+
@store.ignore_in_git
|
|
119
|
+
rescue Error, SystemCallError => e
|
|
120
|
+
@err.puts("Could not add #{ScheduleStore::GITIGNORE_LINE} to .gitignore: #{Schedules.printable(e.message)}")
|
|
121
|
+
end
|
|
122
|
+
|
|
123
|
+
def row(schedule, now)
|
|
124
|
+
name = schedule.name
|
|
125
|
+
next_run = NextRun.after(schedule, now)
|
|
126
|
+
state = os_state(schedule)
|
|
127
|
+
last = last_run(name)
|
|
128
|
+
json = { 'name' => name, 'every' => schedule.every, 'at' => schedule.at, 'on' => schedule.on, 'retries' => schedule.retries,
|
|
129
|
+
'notify' => schedule.notify, 'show_browser' => schedule.show_browser, 'selection' => schedule.selection.to_h,
|
|
130
|
+
'next_run_at' => next_run.iso8601, 'os_state' => state, 'last' => last }
|
|
131
|
+
{ json: json, text: line(name, rule(schedule), "next #{next_run.strftime(LIST_TIME)}", last, state) }
|
|
132
|
+
end
|
|
133
|
+
|
|
134
|
+
# This project's OS entries that schedules.yml has no entry for, found by enumerating the OS scheduler
|
|
135
|
+
def unlisted_rows(listed)
|
|
136
|
+
(unlisted_names - listed).map do |name|
|
|
137
|
+
last = last_run(name)
|
|
138
|
+
json = { 'name' => name, **RULE_KEYS.to_h { |key| [key, nil] }, 'os_state' => UNLISTED, 'last' => last }
|
|
139
|
+
{ json: json, text: line(name, '-', '-', last, UNLISTED) }
|
|
140
|
+
end
|
|
141
|
+
end
|
|
142
|
+
|
|
143
|
+
def unlisted_names
|
|
144
|
+
OsEnumerator.new(adapter, schedule_class: Schedule, root: @project.root).names
|
|
145
|
+
rescue Error
|
|
146
|
+
[]
|
|
147
|
+
end
|
|
148
|
+
|
|
149
|
+
# The ruby first, then the spec files (no need to ask the OS about a schedule that can't run), then the OS
|
|
150
|
+
def os_state(schedule)
|
|
151
|
+
return RUBY_NOT_FOUND unless ruby_found?(schedule)
|
|
152
|
+
|
|
153
|
+
missing = schedule.selection.missing_file(@project.root)
|
|
154
|
+
return "test file not found: #{missing}" if missing
|
|
155
|
+
|
|
156
|
+
os_entry_state(schedule.os_id)
|
|
157
|
+
rescue Error => e
|
|
158
|
+
"unknown (#{Schedules.printable(e.message)})"
|
|
159
|
+
end
|
|
160
|
+
|
|
161
|
+
def rule(schedule)
|
|
162
|
+
selection = schedule.selection
|
|
163
|
+
which = case selection.kind
|
|
164
|
+
when 'tag' then "tag #{selection.tag}"
|
|
165
|
+
when 'files' then selection.files.size == 1 ? selection.files.first : "#{selection.files.size} files"
|
|
166
|
+
else 'all tests'
|
|
167
|
+
end
|
|
168
|
+
flags = [('notify' if schedule.notify), ('browser shown' if schedule.show_browser)].compact
|
|
169
|
+
"#{which}, #{schedule.rule_text}#{" (#{flags.join(', ')})" unless flags.empty?}"
|
|
170
|
+
end
|
|
171
|
+
|
|
172
|
+
def last_run(name)
|
|
173
|
+
last = RunLog.new(@project, name, dir: KIND.dir).last_result
|
|
174
|
+
last && LAST.to_h { |key| [key, last[key]] }
|
|
175
|
+
end
|
|
176
|
+
end
|
|
177
|
+
end
|
|
178
|
+
end
|
|
@@ -0,0 +1,69 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require_relative 'base'
|
|
4
|
+
|
|
5
|
+
module RubyRaider
|
|
6
|
+
module Schedules
|
|
7
|
+
# The steps every scheduler shares (4.1 shared them too): the yml written after the OS entry with a
|
|
8
|
+
# rollback, the yml entry removed after the OS one, and the parts of a list row that ask the OS.
|
|
9
|
+
# The includer gives `adapter` and `store`.
|
|
10
|
+
module SchedulerSteps
|
|
11
|
+
RUBY_NOT_FOUND = 'Ruby not found: re-schedule'
|
|
12
|
+
|
|
13
|
+
private
|
|
14
|
+
|
|
15
|
+
# Writes the yml; when that fails the OS entry just installed is removed, so neither names the schedule
|
|
16
|
+
def save(schedule)
|
|
17
|
+
store.save(schedule)
|
|
18
|
+
rescue Error, SystemCallError => e
|
|
19
|
+
label = adapter.label
|
|
20
|
+
begin
|
|
21
|
+
adapter.remove(schedule.os_id)
|
|
22
|
+
rescue Error => remove_error
|
|
23
|
+
raise Error, "#{e.message} (and removing the #{label} entry failed: #{remove_error.message})"
|
|
24
|
+
end
|
|
25
|
+
raise Error, "#{e.message}: the #{label} entry was removed too, so #{schedule.name} is not scheduled"
|
|
26
|
+
end
|
|
27
|
+
|
|
28
|
+
# The OS entry is gone by now: say so when the yml can't be written
|
|
29
|
+
def delete(name, label)
|
|
30
|
+
store.delete(name)
|
|
31
|
+
rescue Error, SystemCallError => e
|
|
32
|
+
raise Error, "#{e.message}: the #{label} entry was already removed, but schedules.yml still lists #{name}"
|
|
33
|
+
end
|
|
34
|
+
|
|
35
|
+
def unreadable_warning(name, label)
|
|
36
|
+
"The schedules.yml entry for #{name} could not be read: a #{label} entry made from a different project path may remain"
|
|
37
|
+
end
|
|
38
|
+
|
|
39
|
+
# The OS entry runs `<ruby> <bundle> ...`: ruby is executed, bundle is only read by it
|
|
40
|
+
def ruby_found?(schedule)
|
|
41
|
+
ruby = schedule.ruby
|
|
42
|
+
bundle = schedule.bundle
|
|
43
|
+
File.file?(ruby) && File.executable?(ruby) && File.file?(bundle) && File.readable?(bundle)
|
|
44
|
+
end
|
|
45
|
+
|
|
46
|
+
def os_entry_state(os_id) = adapter.present?(os_id) ? 'ok' : "missing from #{adapter.label}"
|
|
47
|
+
|
|
48
|
+
# An entry whose state can't be read is left out: only a listed schedule gets an unknown row
|
|
49
|
+
def present?(os_id)
|
|
50
|
+
adapter.present?(os_id)
|
|
51
|
+
rescue Error
|
|
52
|
+
false
|
|
53
|
+
end
|
|
54
|
+
|
|
55
|
+
# nil also when there is no OS scheduler to ask (the rows already say unknown)
|
|
56
|
+
def missed_runs_note
|
|
57
|
+
current = adapter
|
|
58
|
+
current.respond_to?(:missed_runs_note) ? current.missed_runs_note : nil
|
|
59
|
+
rescue Error
|
|
60
|
+
nil
|
|
61
|
+
end
|
|
62
|
+
|
|
63
|
+
def line(name, rule, next_run, last, state)
|
|
64
|
+
result = last ? "#{last['status']} #{last['finished_at']}" : 'never run'
|
|
65
|
+
"#{name.ljust(30)} #{rule} #{next_run} #{result} #{state}"
|
|
66
|
+
end
|
|
67
|
+
end
|
|
68
|
+
end
|
|
69
|
+
end
|