asgard 0.6.0 → 0.6.2

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.
@@ -1,6 +1,7 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  require "time"
4
+ require_relative "../log"
4
5
 
5
6
  # warn is silent when $VERBOSE is nil and bypasses $stderr in Ruby 4.0 (see
6
7
  # base/task_dsl.rb); these messages are for the user, so they always print.
@@ -24,11 +25,14 @@ module Asgard
24
25
  # One `list` row per record, with what the job is doing now.
25
26
  def rows(state) = state.records.map { |record| row(record, state) }
26
27
 
28
+ # verified? goes first: a job that ends between the reads shows
29
+ # its exit, never "gone", and a code read means its time is there.
27
30
  def row(record, state)
28
- name = record[:name]
29
- exited = state.exit_time(name)
30
- status = Jobs.status(exit_code: state.exit_code(name), verified: state.verified?(record),
31
- leftovers: State.leftovers?(record, exited_at: exited))
31
+ name = record[:name]
32
+ verified = state.verified?(record)
33
+ code = state.exit_code(name)
34
+ exited = state.exit_time(name)
35
+ status = Jobs.status(exit_code: code, verified:, leftovers: State.leftovers?(record, exited_at: exited))
32
36
  seconds = (exited - Time.iso8601(record[:started])).round(1) if status.is_a?(Array)
33
37
  { name:, task: record[:task], pid: record[:pid], state: Jobs.describe_status(status, seconds:),
34
38
  started: Time.iso8601(record[:started]).strftime("%Y-%m-%d %H:%M:%S"), command: record[:command] }
@@ -39,6 +43,25 @@ module Asgard
39
43
 
40
44
  # The argv that follows a log the way `tail -f` does, from the top.
41
45
  def tail_argv(path) = ["tail", "-n", "+1", "-f", path]
46
+
47
+ # Whether +word+ is a typed subcommand that names none: not an
48
+ # option, a mapped name, a command or an abbreviation of one.
49
+ def undefined_subcommand?(word)
50
+ return false if word.nil? || word.start_with?("-")
51
+
52
+ !(map.key?(word) || all_commands.key?(word.tr("-", "_")) || find_command_possibilities(word).any?)
53
+ end
54
+
55
+ protected
56
+
57
+ # Thor hands a word that names no subcommand to the default command,
58
+ # list, as its argument; list takes none, so name the word instead,
59
+ # as `schedule` and `watcher` do.
60
+ def dispatch(meth, given_args, given_opts, config)
61
+ word = meth || given_args.first
62
+ handle_no_command_error(word) if config[:invoked_via_subcommand] && undefined_subcommand?(word)
63
+ super
64
+ end
42
65
  end
43
66
 
44
67
  desc "tree", "Print a tree of the jobs subcommands", hide: true
@@ -66,6 +89,17 @@ module Asgard
66
89
 
67
90
  def record_for!(name) = state.read_record(name) || abort("jobs: no such job: #{name} (see `asgard jobs list`)")
68
91
 
92
+ # Whether +path+ is NAME's log to truncate. A job on record that has
93
+ # not written a log yet says so and is skipped; any other name aborts.
94
+ def log_to_truncate?(name, path)
95
+ found = File.file?(path)
96
+ abort "jobs: no such job log: #{name}" if name.include?("/") || !(found || state.read_record(name))
97
+ return true if found
98
+
99
+ $stderr.puts "#{name} has no log yet (#{path}); nothing to truncate"
100
+ false
101
+ end
102
+
69
103
  def stopper = Stopper.new(timeout: options[:timeout] || Stopper::TIMEOUT)
70
104
 
71
105
  # The records `stop` ends, newest start first, and the names that
@@ -73,13 +107,16 @@ module Asgard
73
107
  # depends on, which needs this project's .loki loaded.
74
108
  def stop_targets(names)
75
109
  records, unknown =
76
- if every_project? then [all_states.values.flat_map(&:records), []]
77
- elsif names.empty? then [state.records, []]
110
+ if names.empty? || options[:all] then [listed_states.values.flat_map(&:records), []]
78
111
  else Jobs.resolve(names, state.records, closure: ->(task) { Jobs.closure(task, Tasks._deps) })
79
112
  end
80
113
  [records.sort_by { |record| [record[:started].to_s, record[:name].to_s] }.reverse, unknown]
81
114
  end
82
115
 
116
+ # The states `list` and a bare `stop` cover, by title: every
117
+ # project's, or this one's under no title.
118
+ def listed_states = every_project? ? all_states : { nil => state }
119
+
83
120
  def no_jobs_message
84
121
  return "No jobs have been started on this machine." if every_project?
85
122
 
@@ -91,7 +128,7 @@ module Asgard
91
128
  desc "list", "Show this project's jobs: running, exited with a status, or gone (--all: every project's)"
92
129
  method_option :all, aliases: "-a", type: :boolean, desc: "List every project's jobs, not just this one's"
93
130
  def list
94
- states = every_project? ? all_states : { nil => state }
131
+ states = listed_states
95
132
  Commands.skipped_lines(states.values).each { |line| $stderr.puts line }
96
133
  groups = states.transform_values { |each_state| Commands.rows(each_state) }.reject { |_, rows| rows.empty? }
97
134
  return puts(no_jobs_message) if groups.empty?
@@ -124,6 +161,19 @@ module Asgard
124
161
  IO.copy_stream(path, $stdout)
125
162
  end
126
163
 
164
+ desc "truncate_log [NAME]", "Keep the last M lines of a job log; no NAME truncates every job log of this project"
165
+ method_option :lines, type: :string, default: "1000", banner: "M", desc: "Lines to keep per log; 0 empties it"
166
+ method_option :dry_run, aliases: "-n", type: :boolean, desc: "Report what would be removed without changing logs"
167
+ def truncate_log(name = nil)
168
+ Log.line_count(options[:lines]) # reject a bad --lines even when there is no log to truncate
169
+ paths = name ? [state.log_path(name)] : Dir.glob(File.join(Asgard.glob_escape(state.dir), "*.log"))
170
+ return if name && !log_to_truncate?(name, paths.first)
171
+
172
+ Log.truncate_logs(paths, lines: options[:lines], dry_run: options[:dry_run])
173
+ rescue ArgumentError, SystemCallError, IOError => e
174
+ abort "jobs: #{e.message}"
175
+ end
176
+
127
177
  desc "restart NAME", "Stop the job, then run its owning task again"
128
178
  method_option :timeout, aliases: "-t", type: :numeric, desc: "Seconds to wait after TERM before KILL (default #{Stopper::TIMEOUT})"
129
179
  def restart(name)
@@ -29,24 +29,29 @@ module Asgard
29
29
  # is alive and runs the trampoline whose command line carries the
30
30
  # script path. A reused pid after a reboot fails this, and so does
31
31
  # one that is now asgard itself, and so does a record that is not
32
- # a job's (Jobs.valid_record?). A ps that cannot tell raises
32
+ # a job's (Jobs.valid_record?). Once the trampoline has written the
33
+ # status (not just opened the file) its pid no longer counts, though
34
+ # its shell may not have exited yet; ps asks first, so a status
35
+ # written in between is still seen. A ps that cannot tell raises
33
36
  # Pid::Unusable rather than calling every job gone.
34
37
  def verified?(record)
35
38
  return false unless Jobs.valid_record?(record)
36
39
 
37
40
  Pid.require_ps!
38
- Pid.matches?(record[:pid], record[:script], self_ok: false) || survives?(record)
41
+ trampoline = Pid.matches?(record[:pid], record[:script], self_ok: false) && !File.size?(status_file(record))
42
+ trampoline || survives?(record)
39
43
  end
40
44
 
41
- # Whether the job outlived its trampoline: some process in its group
42
- # still runs the script. A group id is never reused while the group
43
- # has members, so a match is still the job we started.
44
- def survives?(record, ps: Pid.method(:group_command_lines))
45
+ # Whether the script still runs apart from the trampoline: some
46
+ # other process in its group carries the script path (the script's
47
+ # shell, or a subshell it backgrounded). A group id is never reused
48
+ # while the group has members, so a match is still the job we started.
49
+ def survives?(record, ps: Pid.method(:group_processes))
45
50
  pgid = record[:pgid]
46
51
  script = record[:script].to_s
47
52
  return false unless job_group?(pgid) && !script.empty?
48
53
 
49
- ps.call(pgid).any? { |command| command.include?(script) }
54
+ ps.call(pgid).any? { |member| member.pid != record[:pid] && member.command.include?(script) }
50
55
  end
51
56
 
52
57
  # Whether +pgid+ could be a job's group at all: a group id above 1
@@ -22,16 +22,10 @@ module Kernel
22
22
  def changes = Asgard::Changes.current
23
23
  module_function :changes
24
24
 
25
+ # The nearest +name+ in the current directory or an ancestor, as a
26
+ # Pathname, or nil. A directory by that name is not it.
25
27
  def loki_up(name = ".loki")
26
- dir = Pathname.new(Dir.pwd)
27
- loop do
28
- candidate = dir + name
29
- return candidate if candidate.file? # a directory by that name is not it
30
- parent = dir.parent
31
- break if parent == dir
32
- dir = parent
33
- end
34
- nil
28
+ Pathname.new(Dir.pwd).ascend.map { it + name }.find(&:file?)
35
29
  end
36
30
  module_function :loki_up
37
31
 
@@ -68,16 +62,12 @@ module Kernel
68
62
  # in is part of a path, not a pattern.
69
63
  def import_up(name = ".loki")
70
64
  if Asgard.glob?(name)
71
- dir = Dir.pwd
72
- loop do
65
+ Pathname.new(Dir.pwd).ascend do |dir|
73
66
  matches = Dir.glob(name, base: dir)
74
- unless matches.empty?
75
- warn "import_up: #{name} → #{dir}" if verbose? || debug?
76
- return matches.map { |p| import(Asgard.glob_escape(File.join(dir, p))) }.any?
77
- end
78
- parent = File.dirname(dir)
79
- break if parent == dir
80
- dir = parent
67
+ next if matches.empty?
68
+
69
+ warn "import_up: #{name} → #{dir}" if verbose? || debug?
70
+ return matches.map { |p| import(Asgard.glob_escape(File.join(dir, p))) }.any?
81
71
  end
82
72
  warn "import_up: #{name} not found" if debug?
83
73
  return false
@@ -7,4 +7,23 @@ module Asgard
7
7
  # before the first word that is not one (the task name). After it, the
8
8
  # word belongs to the task, which may take --version as an argument.
9
9
  def self.leading_flag?(argv, flag) = argv.take_while { |word| word.start_with?("-") }.include?(flag)
10
+
11
+ # The options that set a global for whatever task runs, so they read the
12
+ # same on either side of the task name: --debug and --verbose in every
13
+ # form Thor accepts after it (--debug=VALUE, --no-debug, --skip-debug).
14
+ GLOBAL_FLAG = /\A--(?:(?:debug|verbose)(?:=.*)?|(?:no|skip)-(?:debug|verbose))\z/
15
+
16
+ def self.global_flag?(word) = GLOBAL_FLAG.match?(word)
17
+
18
+ # +argv+ with the task name first: leading global flags move behind the
19
+ # word they precede when the block says that word names a task. Thor
20
+ # hands a command line that starts with an option to the default task,
21
+ # so without this `asgard --verbose build` never reaches `build`.
22
+ def self.task_first(argv)
23
+ flags = argv.take_while { global_flag?(it) }
24
+ word = argv[flags.size]
25
+ return argv if flags.empty? || word.nil? || !yield(word)
26
+
27
+ [word, *flags, *argv.drop(flags.size + 1)]
28
+ end
10
29
  end
data/lib/asgard/log.rb ADDED
@@ -0,0 +1,90 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Asgard
4
+ # Keeps the last M lines of a log, rewriting it in place. Every writer
5
+ # (launchd, systemd's append:, jobs, the watcher) opens its log with
6
+ # O_APPEND, so it keeps writing to the same file at the new end. As with
7
+ # logrotate's copytruncate, a write landing between the last read and the
8
+ # truncate can be lost.
9
+ module Log
10
+ CHUNK = 64 * 1024
11
+
12
+ module_function
13
+
14
+ def line_count(value)
15
+ raise ArgumentError, "--lines must be a nonnegative integer" unless value.to_s.match?(/\A[0-9]+\z/)
16
+
17
+ Integer(value.to_s, 10)
18
+ end
19
+
20
+ # The byte offset where the last +lines+ lines of +io+ begin. Scans
21
+ # backwards in bounded chunks; a final newline ends the last line
22
+ # rather than starting an empty one.
23
+ def tail_offset(io, lines)
24
+ position = io.size
25
+ return position if lines.zero? || position.zero?
26
+
27
+ io.seek(-1, IO::SEEK_END)
28
+ remaining = lines + (io.read(1) == "\n" ? 1 : 0)
29
+ while position.positive?
30
+ length = [position, CHUNK].min
31
+ position -= length
32
+ io.seek(position)
33
+ chunk = io.read(length)
34
+ index = chunk.bytesize
35
+ while index.positive? && (index = chunk.rindex("\n", index - 1))
36
+ remaining -= 1
37
+ return position + index + 1 if remaining.zero?
38
+ end
39
+ end
40
+ 0
41
+ end
42
+
43
+ # Bytes removed (or that would be, with +dry_run+).
44
+ def truncate(path, lines:, dry_run: false)
45
+ File.open(path, dry_run ? "rb" : "r+b") do |file|
46
+ offset = tail_offset(file, lines)
47
+ rewrite(file, offset) unless dry_run || offset.zero?
48
+ offset
49
+ end
50
+ end
51
+
52
+ # Slides the bytes from +offset+ on to the start of +file+, a chunk at a
53
+ # time, then cuts the file after them. Each write lands behind the next
54
+ # read, so no copy is needed, and lines appended meanwhile move too.
55
+ # Signals and Thread#raise wait until the file is cut. A failed read or
56
+ # write still cuts the file after the bytes already moved, so it never
57
+ # holds the tail written over its own head.
58
+ def rewrite(file, offset)
59
+ moved = 0
60
+ Thread.handle_interrupt(Object => :never) do
61
+ while (chunk = read_at(file, offset + moved))
62
+ file.seek(moved)
63
+ file.write(chunk)
64
+ moved += chunk.bytesize
65
+ end
66
+ file.truncate(moved)
67
+ rescue StandardError
68
+ file.truncate(moved) if moved.positive?
69
+ raise
70
+ end
71
+ end
72
+
73
+ # Up to CHUNK bytes of +file+ from +position+, or nil at the end.
74
+ def read_at(file, position)
75
+ file.seek(position)
76
+ file.read(CHUNK)
77
+ end
78
+
79
+ def truncate_logs(paths, lines:, dry_run: false, out: $stdout)
80
+ count = line_count(lines)
81
+ found = paths.select { |path| File.file?(path) }.uniq.sort
82
+ return out.puts("No logs found for this project.") if found.empty?
83
+
84
+ found.each do |path|
85
+ bytes = truncate(path, lines: count, dry_run:)
86
+ out.puts "#{dry_run ? 'would truncate' : 'truncated'} #{path}: #{bytes} bytes #{dry_run ? 'would be removed' : 'removed'}"
87
+ end
88
+ end
89
+ end
90
+ end
data/lib/asgard/pid.rb CHANGED
@@ -48,27 +48,10 @@ module Asgard
48
48
  nil
49
49
  end
50
50
 
51
- # The command lines of every process in group +pgid+. ps by group id
52
- # is not portable, so this lists every process with its group and
53
- # keeps the matching ones (see group_commands).
54
- def group_command_lines(pgid)
55
- out, status = Open3.capture2(PS, "-ww", "-A", "-o", "pgid=,command=")
56
- status.success? ? group_commands(out, pgid) : []
57
- rescue SystemCallError
58
- []
59
- end
60
-
61
- # The commands in `ps -o pgid=,command=` output that belong to +pgid+.
62
- def group_commands(ps_output, pgid)
63
- ps_output.each_line.filter_map do |line|
64
- group, command = line.strip.split(/\s+/, 2)
65
- command.to_s if group.to_i == pgid
66
- end
67
- end
68
-
69
- # Every process in group +pgid+ as Members. Like group_command_lines,
70
- # but with each one's age, so a caller can tell a group that has kept
71
- # its members from a later group that reused the id.
51
+ # Every process in group +pgid+ as Members, each with its age, so a
52
+ # caller can tell a group that has kept its members from a later
53
+ # group that reused the id. ps by group id is not portable, so this
54
+ # lists every process with its group and keeps the matching ones.
72
55
  def group_processes(pgid, now: Time.now)
73
56
  out, status = Open3.capture2(PS, "-ww", "-A", "-o", "pid=,pgid=,etime=,command=")
74
57
  status.success? ? parse_group(out, pgid, now:) : []
@@ -1,6 +1,7 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  require "shellwords"
4
+ require_relative "../log"
4
5
 
5
6
  # warn is silent when $VERBOSE is nil and bypasses $stderr in Ruby 4.0 (see
6
7
  # base/task_dsl.rb); these messages are for the user, so they always print.
@@ -22,7 +23,7 @@ module Asgard
22
23
  no_commands do
23
24
  def schedules = Schedule.declarations
24
25
 
25
- def schedule_root = @schedule_root ||= File.realpath((Asgard.find_task_file&.dirname || Pathname.pwd).to_s)
26
+ def schedule_root = @schedule_root ||= Asgard.project_root
26
27
 
27
28
  def schedule_project = @schedule_project ||= Schedule.project_slug(schedule_root)
28
29
 
@@ -51,7 +52,7 @@ module Asgard
51
52
 
52
53
  def schedule_asgard = Schedule.which("asgard", ENV.fetch("PATH", nil)) || abort("schedule: asgard is not on PATH")
53
54
 
54
- def schedule_summary(spec) = "#{Schedule.command_line(spec[:task], spec[:args])} (#{Schedule.describe(**spec)})"
55
+ def schedule_summary(spec) = "#{Schedule.command_line(spec[:task], spec[:args])} (#{Schedule.describe(spec)})"
55
56
 
56
57
  # One `list` row for this project: its declaration (if still declared) plus live state.
57
58
  def schedule_row(name)
@@ -118,6 +119,17 @@ module Asgard
118
119
  find_installed(name) || abort("#{name} is neither declared nor installed (see `asgard schedule list --all`).")
119
120
  end
120
121
 
122
+ # Whether +path+ is NAME's log to truncate. A declared entry that has
123
+ # not run yet says so and is skipped; any other name aborts.
124
+ def log_to_truncate?(name, path)
125
+ found = File.file?(path)
126
+ abort "schedule: no such schedule log: #{name}" if name.include?("/") || !(found || schedules.key?(name))
127
+ return true if found
128
+
129
+ $stderr.puts "#{name} has no log yet (#{path}); nothing to truncate"
130
+ false
131
+ end
132
+
121
133
  # True when list should cover every project: asked for with --all, or
122
134
  # there is no .loki here, so no single project to scope to.
123
135
  def list_all? = options[:all] || Asgard.find_task_file.nil?
@@ -159,16 +171,12 @@ module Asgard
159
171
 
160
172
  def entry_rows(entries) = entries.map { |slug, name| entry_row(slug, name) }
161
173
 
162
- # This project's entries keep their declaration columns; see other_project_row.
174
+ # This project's entries keep their declaration columns. Another
175
+ # project's .loki isn't loaded, so its command and schedule are read
176
+ # back from the installed job files.
163
177
  def entry_row(slug, name)
164
178
  return schedule_row(name) if slug == current_slug
165
179
 
166
- other_project_row(slug, name)
167
- end
168
-
169
- # A row for an entry of another project: its .loki isn't loaded, so the
170
- # command and schedule are read back from the installed job files.
171
- def other_project_row(slug, name)
172
180
  backend = backend_for(slug)
173
181
  shown = backend.installed_columns(name) || Schedule.declared_columns(nil, "(unreadable)")
174
182
  { name:, **shown, **Schedule.live_columns(backend, name) }
@@ -230,6 +238,20 @@ module Asgard
230
238
  IO.copy_stream(path, $stdout)
231
239
  end
232
240
 
241
+ desc "truncate_log [NAME]", "Keep the last M lines of an entry's log; no NAME truncates every schedule log of this project"
242
+ method_option :lines, type: :string, default: "1000", banner: "M", desc: "Lines to keep per log; 0 empties it"
243
+ method_option :dry_run, aliases: "-n", type: :boolean, desc: "Report what would be removed without changing logs"
244
+ def truncate_log(name = nil)
245
+ Log.line_count(options[:lines]) # reject a bad --lines even when there is no log to truncate
246
+ pattern = scheduler.log_path("*")
247
+ paths = name ? [scheduler.log_path(name)] : Dir.glob(File.join(Asgard.glob_escape(File.dirname(pattern)), File.basename(pattern)))
248
+ return if name && !log_to_truncate?(name, paths.first)
249
+
250
+ Log.truncate_logs(paths, lines: options[:lines], dry_run: options[:dry_run])
251
+ rescue ArgumentError, SystemCallError, IOError => e
252
+ abort "schedule: #{e.message}"
253
+ end
254
+
233
255
  desc "remove [NAME]", "Remove one installed entry (NAME or project/NAME), or all of this project's"
234
256
  def remove(name = nil)
235
257
  return remove_one(name) if name
@@ -63,12 +63,13 @@ module Asgard
63
63
  end
64
64
 
65
65
  class << self
66
- # The runner new backends get. Tests assign one that records commands
67
- # instead of running them; nil restores RUNNER.
68
- attr_writer :runner
69
-
66
+ # The runner new backends get: RUNNER, unless use_runner swapped it.
70
67
  def runner = @runner || RUNNER
71
68
 
69
+ # Tests swap in a runner that records commands instead of running
70
+ # them; nil restores RUNNER.
71
+ def use_runner(runner) = @runner = runner
72
+
72
73
  # The backend class for +platform+ (a RUBY_PLATFORM string).
73
74
  def backend_class(platform = RUBY_PLATFORM)
74
75
  case platform
@@ -94,10 +95,12 @@ module Asgard
94
95
  if every
95
96
  raise ArgumentError, "schedule :#{task} on: only applies to at: times; every: runs on every day" if on
96
97
 
98
+ given = every
97
99
  every = seconds(every)
98
100
  unless every&.positive?
99
101
  raise ArgumentError,
100
- "schedule :#{task} every: must be a positive number of seconds or a Duration (3.minutes)"
102
+ "schedule :#{task} every: must be a whole number of seconds (900) or answer in_seconds " \
103
+ "(ActiveSupport's 15.minutes); got #{given.inspect}"
101
104
  end
102
105
  else
103
106
  on ||= :daily
@@ -159,7 +162,7 @@ module Asgard
159
162
  # The task alone, or a slug of the whole command when it has arguments,
160
163
  # so one task can be scheduled more than once with different flags.
161
164
  def entry_name(task, args, as = nil)
162
- name = (as || (args.empty? ? task : command_slug(task, args))).to_s
165
+ name = (as || (args.empty? ? task : command_slug([task, *args]))).to_s
163
166
  raise ArgumentError, "schedule as: #{name.inspect} may only contain letters, digits, _ . -" unless name.match?(/\A[\w.-]+\z/)
164
167
 
165
168
  name
@@ -172,18 +175,19 @@ module Asgard
172
175
  # `period week` and `report_period` with `week`. Schedule.declare
173
176
  # gives each of two such rivals its hashed_slug, so different commands
174
177
  # never share a name.
175
- def command_slug(task, args)
176
- words = [task, *args].map(&:to_s)
178
+ def command_slug(words)
179
+ words = words.map(&:to_s)
177
180
  name = slug(words.join(" "))
178
181
  return name if name.length <= NAME_MAX && words.drop(1).all? { |arg| arg.match?(PLAIN_ARG) }
179
182
 
180
- hashed_slug(task, args)
183
+ hashed_slug(words)
181
184
  end
182
185
 
183
- # The slug with a short hash of the exact words appended, capped at
184
- # NAME_MAX: "report-period-week-1a2b3c4d".
185
- def hashed_slug(task, args)
186
- words = [task, *args].map(&:to_s)
186
+ # The slug of a command's +words+ (the task, then its arguments) with a
187
+ # short hash of the exact words appended, capped at NAME_MAX:
188
+ # "report-period-week-1a2b3c4d".
189
+ def hashed_slug(words)
190
+ words = words.map(&:to_s)
187
191
  digest = Digest::SHA256.hexdigest(words.join("\0"))[0, 8]
188
192
  "#{slug(words.join(' '))[0, NAME_MAX - 9].delete_suffix('-')}-#{digest}"
189
193
  end
@@ -207,22 +211,24 @@ module Asgard
207
211
  # For display: the command line the job runs.
208
212
  def command_line(task, args = []) = Shellwords.join(["asgard", task.to_s, *args])
209
213
 
210
- def describe(at: nil, on: :daily, every: nil, **)
211
- return "every #{every}s" if every
214
+ # A declaration's schedule in words: "every 60s", or its times and days
215
+ # ("17:30 weekdays"; daily when +spec+ has no :on).
216
+ def describe(spec)
217
+ return "every #{spec[:every]}s" if spec[:every]
212
218
 
213
- "#{Array(at).join(', ')} #{Array(on).join(', ')}"
219
+ "#{Array(spec[:at]).join(', ')} #{Array(spec[:on] || :daily).join(', ')}"
214
220
  end
215
221
 
216
222
  # For the `list` table: like describe, but a run of hourly times
217
223
  # collapses ("10:00, 11:00 ... 17:00" => "10:00-17:00 hourly").
218
- def describe_compact(at: nil, on: :daily, every: nil, **)
219
- return "every #{every}s" if every
224
+ def describe_compact(spec)
225
+ return "every #{spec[:every]}s" if spec[:every]
220
226
 
221
- "#{compact_times(Array(at))} #{Array(on).join(', ')}"
227
+ "#{compact_times(Array(spec[:at]))} #{Array(spec[:on] || :daily).join(', ')}"
222
228
  end
223
229
 
224
230
  # The command and schedule columns of a `list` row for a declared entry.
225
- def list_columns(spec) = { command: command_line(spec[:task], spec[:args]), schedule: describe_compact(**spec) }
231
+ def list_columns(spec) = { command: command_line(spec[:task], spec[:args]), schedule: describe_compact(spec) }
226
232
 
227
233
  # The command and schedule columns of a `list` row: the declaration's, or
228
234
  # `missing` in the schedule column when there is none.
@@ -259,7 +265,7 @@ module Asgard
259
265
  end
260
266
 
261
267
  def describe_days(days, group)
262
- describe_compact(at: group.map { |entry| format("%<hour>02d:%<minute>02d", entry) }, on: day_names(days))
268
+ describe_compact({ at: group.map { |entry| format("%<hour>02d:%<minute>02d", entry) }, on: day_names(days) })
263
269
  end
264
270
 
265
271
  # The command and schedule columns for a job read back from its files.
@@ -355,9 +361,10 @@ module Asgard
355
361
  # The job's argv. Schedulers need an absolute program. With direnv, the
356
362
  # repo's .envrc (API keys, ...) is loaded at run time instead of being
357
363
  # copied into the job definition, and direnv finds asgard on the job's
358
- # PATH. Each argument is its own element, so no shell re-splits them.
359
- def program_arguments(task, args = [], root:, asgard:, direnv: nil)
360
- argv = [task.to_s, *args.map(&:to_s)]
364
+ # PATH. +words+ is the task, then its arguments; each is its own
365
+ # element, so no shell re-splits them.
366
+ def program_arguments(words, root:, asgard:, direnv: nil)
367
+ argv = words.map(&:to_s)
361
368
  direnv ? [direnv, "exec", root, "asgard", *argv] : [asgard, *argv]
362
369
  end
363
370
  end
@@ -1,6 +1,7 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  require "fileutils"
4
+ require_relative "../state_dir"
4
5
  require_relative "job_files"
5
6
 
6
7
  module Asgard
@@ -128,7 +129,7 @@ module Asgard
128
129
  klass = self.class
129
130
  plist = klass.plist(
130
131
  label: label(name),
131
- arguments: Schedule.program_arguments(spec[:task], spec[:args], root: @root, asgard:, direnv:),
132
+ arguments: Schedule.program_arguments([spec[:task], *spec[:args]], root: @root, asgard:, direnv:),
132
133
  working_directory: @root,
133
134
  environment: Schedule.environment(spec),
134
135
  log_path: log_path(name),
@@ -147,10 +148,11 @@ module Asgard
147
148
  log = log_path(name)
148
149
  content = files(spec, asgard:, direnv:).fetch(plist)
149
150
  Schedule.check_lengths!([plist, "#{plist}.new", log])
151
+ Schedule.writing { StateDir.private_dir File.dirname(log) } # the logs hold the task's output
150
152
  return :active if self.class.unchanged?(plist, content) && loaded?(name)
151
153
 
152
154
  Schedule.writing do
153
- FileUtils.mkdir_p [File.dirname(plist), File.dirname(log)]
155
+ FileUtils.mkdir_p File.dirname(plist)
154
156
  write_linted(plist, content)
155
157
  end
156
158
  unload(name)
@@ -246,20 +248,17 @@ module Asgard
246
248
  return unless loaded?(name)
247
249
 
248
250
  out, ok = run("launchctl", "bootout", target(name))
249
- return if ok && unloaded?(name)
250
- return unless loaded?(name)
251
+ return if (ok && unloaded?(name)) || !loaded?(name)
251
252
 
252
253
  raise Error, "launchctl bootout #{target(name)} left #{label(name)} still loaded: #{out.strip}"
253
254
  end
254
255
 
255
256
  # Polls for up to two seconds; true once the job is gone.
256
257
  def unloaded?(name)
257
- 20.times do
258
- return true unless loaded?(name)
259
-
260
- sleep 0.1
258
+ 20.times.any? do |attempt|
259
+ sleep 0.1 if attempt.positive?
260
+ !loaded?(name)
261
261
  end
262
- false
263
262
  end
264
263
 
265
264
  def run(*argv) = @runner.call(*argv)
@@ -2,6 +2,7 @@
2
2
 
3
3
  require "fileutils"
4
4
  require "etc"
5
+ require_relative "../state_dir"
5
6
  require_relative "job_files"
6
7
 
7
8
  module Asgard
@@ -159,7 +160,7 @@ module Asgard
159
160
  description = "#{Schedule.command_line(task, args)} (#{@project})"
160
161
  service = klass.service_unit(
161
162
  description:,
162
- arguments: Schedule.program_arguments(task, args, root: @root, asgard:, direnv:),
163
+ arguments: Schedule.program_arguments([task, *args], root: @root, asgard:, direnv:),
163
164
  working_directory: @root,
164
165
  environment: Schedule.environment(spec),
165
166
  log_path: log_path(name)
@@ -266,7 +267,8 @@ module Asgard
266
267
  def write_changed(job, name)
267
268
  changed = self.class.changed_files(job)
268
269
  Schedule.writing do
269
- FileUtils.mkdir_p [File.dirname(timer_path(name)), File.dirname(log_path(name))]
270
+ FileUtils.mkdir_p File.dirname(timer_path(name))
271
+ StateDir.private_dir File.dirname(log_path(name)) # the logs hold the task's output
270
272
  changed.each { |path, content| Schedule.write_private(path, content) }
271
273
  end
272
274
  end