ruby_raider 4.0.0 → 4.1.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/ffmpeg/build.sh +208 -0
- data/.github/ffmpeg/check.sh +72 -0
- data/.github/ffmpeg/frames.rb +34 -0
- data/.github/ffmpeg/probe.sh +17 -0
- data/.github/workflows/ffmpeg.yml +160 -0
- data/README.md +226 -33
- data/RELEASE.md +19 -0
- data/lib/commands/flow_commands.rb +104 -0
- data/lib/generators/templates/common/read_me.tt +6 -7
- data/lib/generators/templates/flows/flow_helper.tt +42 -0
- data/lib/generators/templates/flows/run.tt +332 -0
- data/lib/generators/templates/helpers/video_helper.tt +1 -2
- data/lib/ruby_raider/ffmpeg.rb +38 -11
- data/lib/ruby_raider.rb +16 -0
- data/lib/utilities/flows/next_run.rb +61 -0
- data/lib/utilities/flows/notifier.rb +76 -0
- data/lib/utilities/flows/os/command_runner.rb +73 -0
- data/lib/utilities/flows/os/cron.rb +137 -0
- data/lib/utilities/flows/os/launchd.rb +164 -0
- data/lib/utilities/flows/os/linux.rb +63 -0
- data/lib/utilities/flows/os/systemd.rb +178 -0
- data/lib/utilities/flows/os/task_scheduler.rb +179 -0
- data/lib/utilities/flows/project.rb +170 -0
- data/lib/utilities/flows/run_log.rb +61 -0
- data/lib/utilities/flows/runner.rb +206 -0
- data/lib/utilities/flows/schedule.rb +221 -0
- data/lib/utilities/flows/schedule_store.rb +135 -0
- data/lib/utilities/flows/scheduler.rb +187 -0
- data/lib/version +1 -1
- data/sig/commands/flow_commands.rbs +13 -0
- data/sig/ruby_raider/ffmpeg.rbs +1 -0
- data/sig/ruby_raider.rbs +2 -0
- data/sig/utilities/flows/next_run.rbs +15 -0
- data/sig/utilities/flows/notifier.rbs +29 -0
- data/sig/utilities/flows/os/command_runner.rbs +27 -0
- data/sig/utilities/flows/os/cron.rbs +34 -0
- data/sig/utilities/flows/os/launchd.rbs +45 -0
- data/sig/utilities/flows/os/linux.rbs +27 -0
- data/sig/utilities/flows/os/systemd.rbs +43 -0
- data/sig/utilities/flows/os/task_scheduler.rbs +53 -0
- data/sig/utilities/flows/project.rbs +46 -0
- data/sig/utilities/flows/run_log.rbs +22 -0
- data/sig/utilities/flows/runner.rbs +35 -0
- data/sig/utilities/flows/schedule.rbs +61 -0
- data/sig/utilities/flows/schedule_store.rbs +34 -0
- data/sig/utilities/flows/scheduler.rbs +42 -0
- data/spec/commands/flow_commands_spec.rb +128 -0
- data/spec/generators/flow_helper_spec.rb +40 -0
- data/spec/generators/video_helper_spec.rb +21 -4
- data/spec/integration/content/gemfile_content_spec.rb +2 -2
- data/spec/integration/content/readme_content_spec.rb +7 -2
- data/spec/integration/end_to_end_spec.rb +83 -8
- data/spec/integration/flows/flow_project.rb +62 -0
- data/spec/integration/flows/run_command_spec.rb +91 -0
- data/spec/integration/flows/run_lifecycle_spec.rb +383 -0
- data/spec/utilities/ffmpeg_spec.rb +80 -7
- data/spec/utilities/flows/next_run_spec.rb +119 -0
- data/spec/utilities/flows/notifier_spec.rb +121 -0
- data/spec/utilities/flows/os/command_runner_spec.rb +56 -0
- data/spec/utilities/flows/os/cron_spec.rb +287 -0
- data/spec/utilities/flows/os/launchd_spec.rb +293 -0
- data/spec/utilities/flows/os/linux_spec.rb +149 -0
- data/spec/utilities/flows/os/systemd_spec.rb +329 -0
- data/spec/utilities/flows/os/task_scheduler_spec.rb +505 -0
- data/spec/utilities/flows/project_spec.rb +178 -0
- data/spec/utilities/flows/run_log_spec.rb +47 -0
- data/spec/utilities/flows/runner_spec.rb +313 -0
- data/spec/utilities/flows/schedule_spec.rb +233 -0
- data/spec/utilities/flows/schedule_store_spec.rb +137 -0
- data/spec/utilities/flows/scheduler_spec.rb +329 -0
- data/spec/utilities/video_command_spec.rb +13 -0
- metadata +58 -7
- data/docs/superpowers/plans/2026-10-25-failure-video-replay.md +0 -400
- data/docs/superpowers/plans/2026-10-26-parity-cleanup.md +0 -233
- data/docs/superpowers/plans/2026-10-28-failure-card.md +0 -295
- data/docs/superpowers/specs/2026-10-25-failure-video-replay-design.md +0 -214
- data/docs/superpowers/specs/2026-10-28-failure-card-design.md +0 -210
|
@@ -0,0 +1,221 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require 'digest'
|
|
4
|
+
require 'rbconfig'
|
|
5
|
+
require 'time'
|
|
6
|
+
require_relative 'project'
|
|
7
|
+
require_relative 'runner'
|
|
8
|
+
|
|
9
|
+
module RubyRaider
|
|
10
|
+
module Flows
|
|
11
|
+
# One scheduled flow: the rule (every/at/on), how it runs (retries, inputs) and what the OS entry
|
|
12
|
+
# runs (absolute ruby and bundle, GEM_HOME/GEM_PATH captured when it was scheduled). Immutable.
|
|
13
|
+
# :reek:TooManyInstanceVariables { max_instance_variables: 13 } -- the fields of a schedules.yml entry
|
|
14
|
+
# :reek:TooManyConstants { max_constants: 9 } -- the rule vocabulary, the OS entry parts and the yml keys
|
|
15
|
+
class Schedule
|
|
16
|
+
EVERY = %w[hour day weekday week].freeze
|
|
17
|
+
DAYS = %w[mon tue wed thu fri sat sun].freeze
|
|
18
|
+
DAYS_RULE = 'use mon, tue, wed, thu, fri, sat or sun'
|
|
19
|
+
CLOCK = /\A([01]\d|2[0-3]):([0-5]\d)\z/
|
|
20
|
+
MINUTE = /\A(?:[01]\d|2[0-3])?:([0-5]\d)\z/
|
|
21
|
+
GEM_ENV = %w[GEM_HOME GEM_PATH].freeze
|
|
22
|
+
SYSTEM_PATH = %w[/usr/bin /bin].freeze
|
|
23
|
+
OS_ID_PREFIX = 'dev.rubyraider.flow'
|
|
24
|
+
KEYS = %w[name every at on retries inputs notify os_id ruby bundle gem_env created_at].freeze
|
|
25
|
+
|
|
26
|
+
attr_reader :name, :every, :at, :on, :retries, :inputs, :notify, :os_id, :ruby, :bundle, :gem_env, :project_root, :created_at
|
|
27
|
+
|
|
28
|
+
def self.build(project:, name:, every:, at: nil, on: nil, retries: 0, inputs: [], notify: false, now: Time.now)
|
|
29
|
+
root = project.root
|
|
30
|
+
new(name: name, every: every, at: at, on: on, retries: retries, inputs: inputs, notify: notify, os_id: os_id_for(root, name),
|
|
31
|
+
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
|
|
36
|
+
def self.from_h(hash, project_root)
|
|
37
|
+
raise Error, 'A schedule entry must be a mapping' unless hash.is_a?(Hash)
|
|
38
|
+
|
|
39
|
+
fields = KEYS.to_h { |key| [key.to_sym, hash[key]] }
|
|
40
|
+
schedule = new(**fields, project_root: project_root)
|
|
41
|
+
# Strict: an entry copied from another project, or a moved project, is not this project's entry
|
|
42
|
+
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)
|
|
43
|
+
|
|
44
|
+
schedule
|
|
45
|
+
end
|
|
46
|
+
|
|
47
|
+
def self.os_id_for(project_root, name) = "#{OS_ID_PREFIX}.#{Digest::SHA256.hexdigest(project_root)[0, 10]}.#{name}"
|
|
48
|
+
|
|
49
|
+
def self.bundle_path
|
|
50
|
+
Gem.bin_path('bundler', 'bundle')
|
|
51
|
+
rescue Gem::GemNotFoundException, Gem::MissingSpecError => e
|
|
52
|
+
raise Error, "Bundler not found for this Ruby (#{e.message}): gem install bundler, then try again"
|
|
53
|
+
end
|
|
54
|
+
|
|
55
|
+
# The GEM_HOME and GEM_PATH raider started with (before bundle exec changed them), only when set
|
|
56
|
+
def self.captured_gem_env
|
|
57
|
+
env = defined?(Bundler) ? Bundler.original_env : ENV
|
|
58
|
+
GEM_ENV.each_with_object({}) do |key, captured|
|
|
59
|
+
value = env[key]
|
|
60
|
+
captured[key] = value if value.is_a?(String) && !value.empty?
|
|
61
|
+
end
|
|
62
|
+
end
|
|
63
|
+
|
|
64
|
+
def initialize(name:, every:, at:, on:, retries:, inputs:, os_id:, ruby:, bundle:, gem_env:, project_root:, created_at:, notify: false)
|
|
65
|
+
@name = checked_name(name)
|
|
66
|
+
@every = checked_every(every)
|
|
67
|
+
@at = checked_at(at)
|
|
68
|
+
@on = checked_on(on)
|
|
69
|
+
@retries = checked_retries(retries)
|
|
70
|
+
@inputs = checked_inputs(inputs)
|
|
71
|
+
@notify = checked_notify(notify)
|
|
72
|
+
@os_id = checked_os_id(os_id)
|
|
73
|
+
@ruby = checked_path('ruby', ruby)
|
|
74
|
+
@bundle = checked_path('bundle', bundle)
|
|
75
|
+
@gem_env = checked_gem_env(gem_env)
|
|
76
|
+
@project_root = checked_path('project_root', project_root)
|
|
77
|
+
@created_at = checked_text('created_at', created_at)
|
|
78
|
+
freeze
|
|
79
|
+
end
|
|
80
|
+
|
|
81
|
+
def hour = at[0, 2].to_i
|
|
82
|
+
|
|
83
|
+
def minute = at[3, 2].to_i
|
|
84
|
+
|
|
85
|
+
def days
|
|
86
|
+
case every
|
|
87
|
+
when 'weekday' then DAYS.first(5)
|
|
88
|
+
when 'week' then [on.to_s]
|
|
89
|
+
else DAYS
|
|
90
|
+
end
|
|
91
|
+
end
|
|
92
|
+
|
|
93
|
+
def argv
|
|
94
|
+
command = [ruby, bundle, 'exec', 'raider', 'flow', 'run', name, '--project', project_root, '--trigger', 'schedule']
|
|
95
|
+
command += ['--retries', retries.to_s] if retries.positive?
|
|
96
|
+
command << '--notify' if notify
|
|
97
|
+
command += ['--input', *inputs] unless inputs.empty?
|
|
98
|
+
command
|
|
99
|
+
end
|
|
100
|
+
|
|
101
|
+
def env
|
|
102
|
+
path = [File.dirname(ruby), File.dirname(bundle), *SYSTEM_PATH].uniq.join(File::PATH_SEPARATOR)
|
|
103
|
+
{ 'PATH' => path, 'LANG' => 'en_US.UTF-8' }.merge(gem_env)
|
|
104
|
+
end
|
|
105
|
+
|
|
106
|
+
def working_directory = project_root
|
|
107
|
+
|
|
108
|
+
def log_path = File.join(project_root, '.raider', 'flows', name, 'scheduler.log')
|
|
109
|
+
|
|
110
|
+
def rule_text
|
|
111
|
+
case every
|
|
112
|
+
when 'hour' then "every hour at #{at[2, 3]}"
|
|
113
|
+
when 'week' then "every week on #{on} at #{at}"
|
|
114
|
+
else "every #{every} at #{at}"
|
|
115
|
+
end
|
|
116
|
+
end
|
|
117
|
+
|
|
118
|
+
def to_h
|
|
119
|
+
{ 'name' => name, 'every' => every, 'at' => at, 'on' => on, 'retries' => retries, 'inputs' => inputs.dup, 'notify' => notify,
|
|
120
|
+
'os_id' => os_id, 'ruby' => ruby, 'bundle' => bundle, 'gem_env' => gem_env.dup, 'created_at' => created_at }
|
|
121
|
+
end
|
|
122
|
+
|
|
123
|
+
private
|
|
124
|
+
|
|
125
|
+
def checked_name(name)
|
|
126
|
+
return name.dup.freeze if name.is_a?(String) && Flows.valid_name?(name)
|
|
127
|
+
|
|
128
|
+
raise Error, "Invalid flow name #{name.inspect}: use 1 to 60 of a-z, 0-9 and _ (not env)"
|
|
129
|
+
end
|
|
130
|
+
|
|
131
|
+
def checked_every(every)
|
|
132
|
+
return every.dup.freeze if EVERY.include?(every)
|
|
133
|
+
|
|
134
|
+
raise Error, "Invalid every #{every.inspect}: use hour, day, weekday or week"
|
|
135
|
+
end
|
|
136
|
+
|
|
137
|
+
# HH:MM, or for every hour :MM or HH:MM (the hour ignored, :00 when left out), kept as HH:MM
|
|
138
|
+
def checked_at(at)
|
|
139
|
+
return clock(at) unless every == 'hour'
|
|
140
|
+
return '00:00' if at.nil?
|
|
141
|
+
|
|
142
|
+
match = MINUTE.match(at.to_s)
|
|
143
|
+
raise Error, "Invalid at #{at.inspect}: use :MM or HH:MM for every hour" unless match
|
|
144
|
+
|
|
145
|
+
"00:#{match[1]}"
|
|
146
|
+
end
|
|
147
|
+
|
|
148
|
+
def clock(at)
|
|
149
|
+
raise Error, "Every #{every} needs at: use HH:MM (24h)" if at.nil?
|
|
150
|
+
return at.dup.freeze if at.is_a?(String) && CLOCK.match?(at)
|
|
151
|
+
|
|
152
|
+
raise Error, "Invalid at #{at.inspect}: use HH:MM (24h)"
|
|
153
|
+
end
|
|
154
|
+
|
|
155
|
+
def checked_on(on)
|
|
156
|
+
unless every == 'week'
|
|
157
|
+
raise Error, 'on is only for every week' unless on.nil?
|
|
158
|
+
|
|
159
|
+
return nil
|
|
160
|
+
end
|
|
161
|
+
raise Error, "Every week needs on: #{DAYS_RULE}" if on.nil?
|
|
162
|
+
return on.dup.freeze if DAYS.include?(on)
|
|
163
|
+
|
|
164
|
+
raise Error, "Invalid on #{on.inspect}: #{DAYS_RULE}"
|
|
165
|
+
end
|
|
166
|
+
|
|
167
|
+
def checked_retries(retries)
|
|
168
|
+
return retries if retries.is_a?(Integer) && retries.between?(0, 3)
|
|
169
|
+
|
|
170
|
+
raise Error, "Invalid retries #{retries.inspect}: use 0 to 3"
|
|
171
|
+
end
|
|
172
|
+
|
|
173
|
+
# name=value pairs as `raider flow run --input` takes them; a control character could break an OS entry
|
|
174
|
+
def checked_inputs(inputs)
|
|
175
|
+
raise Error, 'Invalid inputs: use a list of name=value' unless inputs.is_a?(Array)
|
|
176
|
+
|
|
177
|
+
inputs.map do |pair|
|
|
178
|
+
raise Error, "Invalid input #{pair.inspect}: use name=value in valid UTF-8" unless pair.is_a?(String) && pair.valid_encoding?
|
|
179
|
+
|
|
180
|
+
key, value = pair.split('=', 2)
|
|
181
|
+
raise Error, "Invalid input #{key.to_s.inspect}: #{Runner::INPUT_RULE}" unless value && Runner::INPUT.match?(key.to_s)
|
|
182
|
+
raise Error, "Invalid input #{key.inspect}: the value contains a control character" if value.match?(/[[:cntrl:]]/)
|
|
183
|
+
|
|
184
|
+
pair.dup.freeze
|
|
185
|
+
end.freeze
|
|
186
|
+
end
|
|
187
|
+
|
|
188
|
+
# An entry written before notify existed has no key: off
|
|
189
|
+
def checked_notify(notify)
|
|
190
|
+
return notify == true if [true, false, nil].include?(notify)
|
|
191
|
+
|
|
192
|
+
raise Error, "Invalid notify #{notify.inspect}: use true or false"
|
|
193
|
+
end
|
|
194
|
+
|
|
195
|
+
def checked_os_id(os_id)
|
|
196
|
+
pattern = /\A#{Regexp.escape(OS_ID_PREFIX)}\.[0-9a-f]{10}\.#{Regexp.escape(name)}\z/
|
|
197
|
+
return os_id.dup.freeze if os_id.is_a?(String) && pattern.match?(os_id)
|
|
198
|
+
|
|
199
|
+
raise Error, "Invalid os_id #{os_id.inspect} for #{name}"
|
|
200
|
+
end
|
|
201
|
+
|
|
202
|
+
def checked_path(label, path)
|
|
203
|
+
return checked_text(label, path) if path.is_a?(String) && File.absolute_path?(path)
|
|
204
|
+
|
|
205
|
+
raise Error, "Invalid #{label} #{path.inspect}: use an absolute path"
|
|
206
|
+
end
|
|
207
|
+
|
|
208
|
+
def checked_gem_env(gem_env)
|
|
209
|
+
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?
|
|
210
|
+
|
|
211
|
+
gem_env.to_h { |key, value| [key.dup.freeze, checked_text(key, value)] }.freeze
|
|
212
|
+
end
|
|
213
|
+
|
|
214
|
+
def checked_text(label, text)
|
|
215
|
+
return text.dup.freeze if text.is_a?(String) && text.valid_encoding? && !text.empty? && !text.match?(/[[:cntrl:]]/)
|
|
216
|
+
|
|
217
|
+
raise Error, "Invalid #{label} #{text.inspect}"
|
|
218
|
+
end
|
|
219
|
+
end
|
|
220
|
+
end
|
|
221
|
+
end
|
|
@@ -0,0 +1,135 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require 'fileutils'
|
|
4
|
+
require 'securerandom'
|
|
5
|
+
require 'yaml'
|
|
6
|
+
require_relative 'schedule'
|
|
7
|
+
|
|
8
|
+
module RubyRaider
|
|
9
|
+
module Flows
|
|
10
|
+
# .raider/flows/schedules.yml: the project's scheduled flows, sorted by name. The OS entries are made
|
|
11
|
+
# from it; it is written whole through a temp file and a rename, so a reader never sees half a file.
|
|
12
|
+
# :reek:MissingSafeMethod { exclude: [ writable! ] } -- a check that raises, with no safe twin to offer
|
|
13
|
+
class ScheduleStore
|
|
14
|
+
DIR = '.raider/flows'
|
|
15
|
+
FILE = 'schedules.yml'
|
|
16
|
+
NOFOLLOW = defined?(File::NOFOLLOW) ? File::NOFOLLOW : 0
|
|
17
|
+
NONBLOCK = defined?(File::NONBLOCK) ? File::NONBLOCK : 0
|
|
18
|
+
|
|
19
|
+
# What delete returns for an entry it removed but could not read: enough to remove its OS entry
|
|
20
|
+
class Removed
|
|
21
|
+
attr_reader :name, :os_id
|
|
22
|
+
|
|
23
|
+
def initialize(name, os_id)
|
|
24
|
+
@name = name
|
|
25
|
+
@os_id = os_id
|
|
26
|
+
freeze
|
|
27
|
+
end
|
|
28
|
+
end
|
|
29
|
+
|
|
30
|
+
def initialize(project)
|
|
31
|
+
@project = project
|
|
32
|
+
@file = project.path("#{DIR}/#{FILE}")
|
|
33
|
+
end
|
|
34
|
+
|
|
35
|
+
# Every valid entry, sorted by name; a broken entry or file is skipped
|
|
36
|
+
def all
|
|
37
|
+
entries = read_entries
|
|
38
|
+
return [] unless entries
|
|
39
|
+
|
|
40
|
+
entries.filter_map { |entry| from_entry(entry) }.sort_by(&:name)
|
|
41
|
+
end
|
|
42
|
+
|
|
43
|
+
def find(name) = all.find { |schedule| schedule.name == name }
|
|
44
|
+
|
|
45
|
+
# Adds the schedule, or replaces the one with its name
|
|
46
|
+
def save(schedule)
|
|
47
|
+
write(writable_entries.reject { |entry| named?(entry, schedule.name) } + [schedule.to_h])
|
|
48
|
+
schedule
|
|
49
|
+
end
|
|
50
|
+
|
|
51
|
+
# Raises the Error save and delete would, before anything else is changed
|
|
52
|
+
def writable!
|
|
53
|
+
writable_entries
|
|
54
|
+
nil
|
|
55
|
+
end
|
|
56
|
+
|
|
57
|
+
# Removes every entry with the name, readable or not. Returns the removed schedule, a Removed
|
|
58
|
+
# (name and os_id) when the entry could not be read, or nil when there was none.
|
|
59
|
+
def delete(name)
|
|
60
|
+
entries = writable_entries
|
|
61
|
+
named, kept = entries.partition { |entry| named?(entry, name) }
|
|
62
|
+
return nil if named.empty?
|
|
63
|
+
|
|
64
|
+
write(kept)
|
|
65
|
+
named.filter_map { |entry| from_entry(entry) }.first || Removed.new(name, Schedule.os_id_for(@project.root, name))
|
|
66
|
+
end
|
|
67
|
+
|
|
68
|
+
private
|
|
69
|
+
|
|
70
|
+
def from_entry(entry)
|
|
71
|
+
Schedule.from_h(entry, @project.root)
|
|
72
|
+
rescue Error
|
|
73
|
+
nil
|
|
74
|
+
end
|
|
75
|
+
|
|
76
|
+
def named?(entry, name) = entry.is_a?(Hash) && entry['name'] == name
|
|
77
|
+
|
|
78
|
+
# The raw entries, nil when the file is not a schedules file or can't be read (missing reads as empty)
|
|
79
|
+
def read_entries
|
|
80
|
+
text = read_text
|
|
81
|
+
return [] if text.nil?
|
|
82
|
+
return nil unless text
|
|
83
|
+
|
|
84
|
+
data = YAML.safe_load(text)
|
|
85
|
+
return [] if data.nil?
|
|
86
|
+
|
|
87
|
+
entries = data.is_a?(Hash) ? data['schedules'] : nil
|
|
88
|
+
entries.is_a?(Array) ? entries : nil
|
|
89
|
+
rescue Psych::Exception
|
|
90
|
+
nil
|
|
91
|
+
end
|
|
92
|
+
|
|
93
|
+
# The entries to rewrite, keeping ones this version can't read; refuses to overwrite a broken file
|
|
94
|
+
def writable_entries
|
|
95
|
+
read_entries || raise(Error, "#{DIR}/#{FILE} is not a schedules file: fix or remove it, then try again")
|
|
96
|
+
end
|
|
97
|
+
|
|
98
|
+
# Read through one handle that is not a link; nil when there is no file, false when it is a link,
|
|
99
|
+
# not a regular file or can't be read (so a write never replaces what it could not see)
|
|
100
|
+
def read_text
|
|
101
|
+
File.open(@file, File::RDONLY | NONBLOCK | NOFOLLOW) do |file|
|
|
102
|
+
file.stat.file? ? file.read.to_s.force_encoding(Encoding::UTF_8) : false
|
|
103
|
+
end
|
|
104
|
+
rescue Errno::ENOENT
|
|
105
|
+
nil
|
|
106
|
+
rescue SystemCallError
|
|
107
|
+
false
|
|
108
|
+
end
|
|
109
|
+
|
|
110
|
+
def write(entries)
|
|
111
|
+
dir = private_dir
|
|
112
|
+
sorted = entries.sort_by { |entry| entry.is_a?(Hash) ? entry['name'].to_s : '' }
|
|
113
|
+
temp = File.join(dir, ".#{FILE}.#{SecureRandom.hex(6)}.tmp")
|
|
114
|
+
File.open(temp, File::WRONLY | File::CREAT | File::EXCL | NOFOLLOW, 0o600) do |file|
|
|
115
|
+
file.write(YAML.dump({ 'schedules' => sorted }))
|
|
116
|
+
file.flush
|
|
117
|
+
file.fsync
|
|
118
|
+
end
|
|
119
|
+
File.rename(temp, @file)
|
|
120
|
+
ensure
|
|
121
|
+
FileUtils.rm_f(temp) if temp && File.exist?(temp)
|
|
122
|
+
end
|
|
123
|
+
|
|
124
|
+
# A 0700 folder inside the project that is not a link, as flows/support/run.rb makes it
|
|
125
|
+
def private_dir
|
|
126
|
+
dir = @project.path(DIR)
|
|
127
|
+
FileUtils.mkdir_p(dir)
|
|
128
|
+
raise Error, "#{DIR} must be a folder, not a link" unless File.lstat(dir).directory?
|
|
129
|
+
|
|
130
|
+
File.chmod(0o700, dir)
|
|
131
|
+
dir
|
|
132
|
+
end
|
|
133
|
+
end
|
|
134
|
+
end
|
|
135
|
+
end
|
|
@@ -0,0 +1,187 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require 'fileutils'
|
|
4
|
+
require 'json'
|
|
5
|
+
require 'rbconfig'
|
|
6
|
+
require_relative 'next_run'
|
|
7
|
+
require_relative 'run_log'
|
|
8
|
+
require_relative 'schedule'
|
|
9
|
+
require_relative 'schedule_store'
|
|
10
|
+
require_relative 'os/command_runner'
|
|
11
|
+
require_relative 'os/launchd'
|
|
12
|
+
require_relative 'os/task_scheduler'
|
|
13
|
+
require_relative 'os/linux'
|
|
14
|
+
|
|
15
|
+
module RubyRaider
|
|
16
|
+
module Flows
|
|
17
|
+
# `raider flow schedule | unschedule | schedules`: keeps the OS scheduler entries and
|
|
18
|
+
# .raider/flows/schedules.yml in step. The OS entry is installed first and the yml written after,
|
|
19
|
+
# so the yml never names an entry the OS scheduler refused.
|
|
20
|
+
# :reek:TooManyInstanceVariables { max_instance_variables: 6 } -- the project, its store, the adapter and the injected streams and clock
|
|
21
|
+
class Scheduler
|
|
22
|
+
NOT_SET_UP = 'Flows are not set up in this project: run raider flow init'
|
|
23
|
+
RUBY_NOT_FOUND = 'Ruby not found: re-schedule'
|
|
24
|
+
FLOW_NOT_FOUND = 'flow not found: unschedule'
|
|
25
|
+
LAST = %w[status finished_at trigger failed_step results_dir].freeze
|
|
26
|
+
TIME = '%a %-d %b %H:%M'
|
|
27
|
+
|
|
28
|
+
# The OS scheduler for this machine; only Linux asks the system (is the user's systemd there?)
|
|
29
|
+
def self.adapter(runner: Os::CommandRunner.new, host_os: RbConfig::CONFIG['host_os'])
|
|
30
|
+
case host_os
|
|
31
|
+
when /darwin/ then Os::Launchd.new(runner: runner)
|
|
32
|
+
when /mswin|mingw|cygwin/ then Os::TaskScheduler.new(runner: runner)
|
|
33
|
+
when /linux/ then Os::Linux.pick(runner)
|
|
34
|
+
else raise Error, "Scheduling flows is not supported on #{host_os}"
|
|
35
|
+
end
|
|
36
|
+
end
|
|
37
|
+
|
|
38
|
+
# adapter: nil picks the OS scheduler on first use, so a command refused earlier never touches it
|
|
39
|
+
def initialize(project, adapter: nil, store: ScheduleStore.new(project), out: $stdout, err: $stderr, now: -> { Time.now })
|
|
40
|
+
@project = project
|
|
41
|
+
@adapter = adapter
|
|
42
|
+
@store = store
|
|
43
|
+
@out = out
|
|
44
|
+
@err = err
|
|
45
|
+
@now = now
|
|
46
|
+
end
|
|
47
|
+
|
|
48
|
+
def schedule(name, every:, at: nil, on: nil, retries: 0, inputs: [], notify: false)
|
|
49
|
+
@project.flow_file(name)
|
|
50
|
+
raise Error, NOT_SET_UP unless @project.set_up?
|
|
51
|
+
|
|
52
|
+
now = @now.call
|
|
53
|
+
schedule = Schedule.build(project: @project, name: name, every: every, at: at, on: on, retries: retries,
|
|
54
|
+
inputs: inputs, notify: notify, now: now)
|
|
55
|
+
flow_dir(name)
|
|
56
|
+
@store.writable!
|
|
57
|
+
old = @store.find(name)
|
|
58
|
+
adapter.install(schedule)
|
|
59
|
+
save(schedule)
|
|
60
|
+
prefix = old ? "Replaced the schedule for #{name}" : "Scheduled #{name}"
|
|
61
|
+
@out.puts("#{prefix}: #{rule(schedule)}. Next run #{NextRun.after(schedule, now).strftime(TIME)}.")
|
|
62
|
+
end
|
|
63
|
+
|
|
64
|
+
def unschedule(name)
|
|
65
|
+
raise Error, "Invalid flow name #{name.inspect}: use 1 to 60 of a-z, 0-9 and _ (not env)" unless Flows.valid_name?(name)
|
|
66
|
+
|
|
67
|
+
# The OS entry first: when the scheduler refuses, the yml entry stays to retry with.
|
|
68
|
+
# from_h is strict, so a stored entry's os_id is always this one.
|
|
69
|
+
label = adapter.label
|
|
70
|
+
was_there = adapter.remove(Schedule.os_id_for(@project.root, name))
|
|
71
|
+
removed = delete(name, label)
|
|
72
|
+
raise Error, "No schedule for #{name}" unless removed || was_there
|
|
73
|
+
|
|
74
|
+
@out.puts(was_there ? "Unscheduled #{name}" : "Unscheduled #{name} (it was already gone from #{label})")
|
|
75
|
+
return unless removed.is_a?(ScheduleStore::Removed)
|
|
76
|
+
|
|
77
|
+
@err.puts("The schedules.yml entry for #{name} could not be read: a #{label} entry made from a different project path may remain")
|
|
78
|
+
end
|
|
79
|
+
|
|
80
|
+
def schedules(json: false)
|
|
81
|
+
now = @now.call
|
|
82
|
+
listed = @store.all
|
|
83
|
+
rows = listed.map { |schedule| row(schedule, now) } + unlisted_rows(listed.map(&:name))
|
|
84
|
+
return @out.puts(JSON.generate(rows.map { |entry| entry[:json] })) if json
|
|
85
|
+
return @out.puts('No schedules yet: raider flow schedule <name> --every day --at 08:00') if rows.empty?
|
|
86
|
+
|
|
87
|
+
rows.each { |entry| @out.puts(Flows.printable(entry[:text])) }
|
|
88
|
+
note = adapter.respond_to?(:missed_runs_note) ? adapter.missed_runs_note : nil
|
|
89
|
+
@out.puts(note) if note
|
|
90
|
+
end
|
|
91
|
+
|
|
92
|
+
private
|
|
93
|
+
|
|
94
|
+
def adapter = @adapter ||= Scheduler.adapter
|
|
95
|
+
|
|
96
|
+
# Writes the yml; when that fails the OS entry just installed is removed, so neither names the flow
|
|
97
|
+
def save(schedule)
|
|
98
|
+
@store.save(schedule)
|
|
99
|
+
rescue Error, SystemCallError => e
|
|
100
|
+
label = adapter.label
|
|
101
|
+
begin
|
|
102
|
+
adapter.remove(schedule.os_id)
|
|
103
|
+
rescue Error => remove_error
|
|
104
|
+
raise Error, "#{e.message} (and removing the #{label} entry failed: #{remove_error.message})"
|
|
105
|
+
end
|
|
106
|
+
raise Error, "#{e.message}: the #{label} entry was removed too, so #{schedule.name} is not scheduled"
|
|
107
|
+
end
|
|
108
|
+
|
|
109
|
+
# The OS entry is gone by now: say so when the yml can't be written
|
|
110
|
+
def delete(name, label)
|
|
111
|
+
@store.delete(name)
|
|
112
|
+
rescue Error, SystemCallError => e
|
|
113
|
+
raise Error, "#{e.message}: the #{label} entry was already removed, but schedules.yml still lists #{name}"
|
|
114
|
+
end
|
|
115
|
+
|
|
116
|
+
# .raider/flows/<name>/ for scheduler.log: 0700 folders that are not links, as flows/support/run.rb makes them
|
|
117
|
+
def flow_dir(name)
|
|
118
|
+
[ScheduleStore::DIR, "#{ScheduleStore::DIR}/#{name}"].each { |relative| private_dir(relative) }
|
|
119
|
+
end
|
|
120
|
+
|
|
121
|
+
def private_dir(relative)
|
|
122
|
+
dir = @project.path(relative)
|
|
123
|
+
FileUtils.mkdir_p(dir)
|
|
124
|
+
raise Error, "#{relative} must be a folder, not a link" unless File.lstat(dir).directory?
|
|
125
|
+
|
|
126
|
+
File.chmod(0o700, dir)
|
|
127
|
+
rescue SystemCallError => e
|
|
128
|
+
raise Error, "Could not make #{relative}: #{e.message}"
|
|
129
|
+
end
|
|
130
|
+
|
|
131
|
+
def row(schedule, now)
|
|
132
|
+
name = schedule.name
|
|
133
|
+
next_run = NextRun.after(schedule, now)
|
|
134
|
+
state = os_state(schedule)
|
|
135
|
+
last = last_run(name)
|
|
136
|
+
json = { 'name' => name, 'every' => schedule.every, 'at' => schedule.at, 'on' => schedule.on,
|
|
137
|
+
'retries' => schedule.retries, 'inputs' => schedule.inputs, 'notify' => schedule.notify, 'next_run_at' => next_run.iso8601,
|
|
138
|
+
'os_state' => state, 'last' => last }
|
|
139
|
+
{ json: json, text: line(name, rule(schedule), "next #{next_run.strftime(TIME)}", last, state) }
|
|
140
|
+
end
|
|
141
|
+
|
|
142
|
+
# Flows the OS scheduler still runs for this project although schedules.yml has no entry for them
|
|
143
|
+
def unlisted_rows(listed)
|
|
144
|
+
(@project.flow_names - listed).filter_map do |name|
|
|
145
|
+
next unless present?(Schedule.os_id_for(@project.root, name))
|
|
146
|
+
|
|
147
|
+
state = 'missing from schedules.yml'
|
|
148
|
+
last = last_run(name)
|
|
149
|
+
json = { 'name' => name, 'every' => nil, 'at' => nil, 'on' => nil, 'retries' => nil, 'inputs' => nil, 'notify' => nil,
|
|
150
|
+
'next_run_at' => nil, 'os_state' => state, 'last' => last }
|
|
151
|
+
{ json: json, text: line(name, '-', '-', last, state) }
|
|
152
|
+
end
|
|
153
|
+
end
|
|
154
|
+
|
|
155
|
+
# A flow whose state can't be read is left out: only a listed schedule gets an unknown row
|
|
156
|
+
def present?(os_id)
|
|
157
|
+
adapter.present?(os_id)
|
|
158
|
+
rescue Error
|
|
159
|
+
false
|
|
160
|
+
end
|
|
161
|
+
|
|
162
|
+
# The OS entry runs `<ruby> <bundle> ...`: ruby is executed, bundle is only read by it
|
|
163
|
+
def os_state(schedule)
|
|
164
|
+
ruby = schedule.ruby
|
|
165
|
+
bundle = schedule.bundle
|
|
166
|
+
return RUBY_NOT_FOUND unless File.file?(ruby) && File.executable?(ruby) && File.file?(bundle) && File.readable?(bundle)
|
|
167
|
+
return FLOW_NOT_FOUND unless File.file?(@project.path("flows/#{schedule.name}_flow.rb"))
|
|
168
|
+
|
|
169
|
+
adapter.present?(schedule.os_id) ? 'ok' : "missing from #{adapter.label}"
|
|
170
|
+
rescue Error => e
|
|
171
|
+
"unknown (#{e.message})"
|
|
172
|
+
end
|
|
173
|
+
|
|
174
|
+
def rule(schedule) = schedule.notify ? "#{schedule.rule_text} (notify)" : schedule.rule_text
|
|
175
|
+
|
|
176
|
+
def last_run(name)
|
|
177
|
+
last = RunLog.new(@project, name).last_result
|
|
178
|
+
last && LAST.to_h { |key| [key, last[key]] }
|
|
179
|
+
end
|
|
180
|
+
|
|
181
|
+
def line(name, rule, next_run, last, state)
|
|
182
|
+
result = last ? "#{last['status']} #{last['finished_at']}" : 'never run'
|
|
183
|
+
"#{name.ljust(30)} #{rule} #{next_run} #{result} #{state}"
|
|
184
|
+
end
|
|
185
|
+
end
|
|
186
|
+
end
|
|
187
|
+
end
|
data/lib/version
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
4.
|
|
1
|
+
4.1.0
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
# Type definitions for FlowCommands
|
|
2
|
+
class FlowCommands < Thor
|
|
3
|
+
def init: () -> void
|
|
4
|
+
def run_flow: (String name) -> void
|
|
5
|
+
def list: () -> void
|
|
6
|
+
def schedule: (String name) -> void
|
|
7
|
+
def unschedule: (String name) -> void
|
|
8
|
+
def schedules: () -> void
|
|
9
|
+
def project: () -> RubyRaider::Flows::Project
|
|
10
|
+
def scheduler: () -> RubyRaider::Flows::Scheduler
|
|
11
|
+
def list_line: (Hash[String, untyped] flow) -> String
|
|
12
|
+
def stop: (RubyRaider::Flows::Error error) -> bot
|
|
13
|
+
end
|
data/sig/ruby_raider/ffmpeg.rbs
CHANGED
data/sig/ruby_raider.rbs
CHANGED
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
module RubyRaider
|
|
2
|
+
module Flows
|
|
3
|
+
module NextRun
|
|
4
|
+
WDAYS: Array[String]
|
|
5
|
+
SEARCH_DAYS: Integer
|
|
6
|
+
SEARCH_MINUTES: Integer
|
|
7
|
+
|
|
8
|
+
def self?.after: (Schedule schedule, Time now) -> Time
|
|
9
|
+
def self?.next_minute: (Integer minute, Time now) -> Time
|
|
10
|
+
def self?.next_day: (Schedule schedule, Time now) -> Time
|
|
11
|
+
def self?.local: (Date date, Integer hour, Integer minute) -> Time
|
|
12
|
+
def self?.wall: (Time time) -> Array[Integer]
|
|
13
|
+
end
|
|
14
|
+
end
|
|
15
|
+
end
|
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
module RubyRaider
|
|
2
|
+
module Flows
|
|
3
|
+
class Notifier
|
|
4
|
+
MAX: Integer
|
|
5
|
+
TIMEOUT: Integer
|
|
6
|
+
OSASCRIPT: Array[String]
|
|
7
|
+
NOTIFY_SEND: Array[String]
|
|
8
|
+
MARKUP: Hash[String, String]
|
|
9
|
+
TOAST_SCRIPT: String
|
|
10
|
+
POWERSHELL_ARGS: Array[String]
|
|
11
|
+
|
|
12
|
+
@runner: Os::CommandRunner
|
|
13
|
+
@host_os: String
|
|
14
|
+
@err: IO | StringIO
|
|
15
|
+
@warned: bool
|
|
16
|
+
@powershell: String
|
|
17
|
+
|
|
18
|
+
def self.powershell: () -> String
|
|
19
|
+
|
|
20
|
+
def initialize: (?runner: Os::CommandRunner, ?host_os: String, ?err: IO | StringIO, ?powershell: String) -> void
|
|
21
|
+
def notify: (String title, String message) -> bool
|
|
22
|
+
|
|
23
|
+
private
|
|
24
|
+
|
|
25
|
+
def clean: (untyped text) -> String
|
|
26
|
+
def failed: (String reason) -> false
|
|
27
|
+
end
|
|
28
|
+
end
|
|
29
|
+
end
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
module RubyRaider
|
|
2
|
+
module Flows
|
|
3
|
+
module Os
|
|
4
|
+
class CommandRunner
|
|
5
|
+
MISSING: Integer
|
|
6
|
+
TIMED_OUT: Integer
|
|
7
|
+
|
|
8
|
+
class Result
|
|
9
|
+
attr_reader stdout: String
|
|
10
|
+
attr_reader stderr: String
|
|
11
|
+
attr_reader exitstatus: Integer
|
|
12
|
+
|
|
13
|
+
def initialize: (stdout: String, stderr: String, exitstatus: Integer) -> void
|
|
14
|
+
def success?: () -> bool
|
|
15
|
+
end
|
|
16
|
+
|
|
17
|
+
def call: (*String argv, ?stdin_data: String?, ?env: Hash[String, String], ?timeout: Numeric?) -> Result
|
|
18
|
+
|
|
19
|
+
private
|
|
20
|
+
|
|
21
|
+
def reader: (IO io) -> Thread
|
|
22
|
+
def feed: (IO stdin, String? data) -> void
|
|
23
|
+
def timed_out: (String command, Numeric? timeout, untyped wait) -> Result
|
|
24
|
+
end
|
|
25
|
+
end
|
|
26
|
+
end
|
|
27
|
+
end
|
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
module RubyRaider
|
|
2
|
+
module Flows
|
|
3
|
+
module Os
|
|
4
|
+
class Cron
|
|
5
|
+
CRONTAB: String
|
|
6
|
+
TAG: String
|
|
7
|
+
MAX_LINE: Integer
|
|
8
|
+
OS_ID: Regexp
|
|
9
|
+
|
|
10
|
+
@runner: CommandRunner
|
|
11
|
+
|
|
12
|
+
def initialize: (?runner: CommandRunner) -> void
|
|
13
|
+
def label: () -> String
|
|
14
|
+
def missed_runs_note: () -> String
|
|
15
|
+
def line: (Schedule schedule) -> String
|
|
16
|
+
def install: (Schedule schedule) -> true
|
|
17
|
+
def remove: (String os_id) -> bool
|
|
18
|
+
def present?: (String os_id) -> bool
|
|
19
|
+
def usable?: () -> bool
|
|
20
|
+
|
|
21
|
+
private
|
|
22
|
+
|
|
23
|
+
def fields: (Schedule schedule) -> String
|
|
24
|
+
def weekday: (String day) -> Integer
|
|
25
|
+
def escape: (String word) -> String
|
|
26
|
+
def without: (String table, String os_id) -> [String, bool]
|
|
27
|
+
def our_command?: (String text, String? name) -> bool
|
|
28
|
+
def read: () -> String
|
|
29
|
+
def save: (String table) -> void
|
|
30
|
+
def checked_os_id: (untyped os_id) -> String
|
|
31
|
+
end
|
|
32
|
+
end
|
|
33
|
+
end
|
|
34
|
+
end
|