Module: JobCLI

Defined in:
lib/ruby-progress/cli/job_cli.rb

Overview

JobCLI - sends control messages to backgrounded progress indicators

Usage:

prg job stop --daemon-name mytask [--message "Done!"] [--checkmark]
prg job advance --daemon-name mytask [--amount 10]
prg job status --daemon-name mytask

Class Method Summary collapse

Class Method Details

.advance(argv) ⇒ Object



93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
# File 'lib/ruby-progress/cli/job_cli.rb', line 93

def self.advance(argv)
  opts = parse_advance_options(argv)
  pid_file = resolve_pid_file(opts)

  unless File.exist?(pid_file)
    warn "PID file #{pid_file} not found. Is the daemon running?"
    exit 1
  end

  # Write advance command to control message file
  cmf = RubyProgress::Daemon.control_message_file(pid_file)
  control_data = {
    action: 'advance',
    amount: opts[:amount] || 1,
    total: opts[:total]
  }.compact

  File.write(cmf, JSON.generate(control_data))

  # Send signal to daemon to check for messages
  pid = File.read(pid_file).strip.to_i
  begin
    Process.kill('USR2', pid)
  rescue Errno::ESRCH
    # Process doesn't exist
  end

  # Silent operation for script-friendly usage
end

.parse_advance_options(argv) ⇒ Object



155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
# File 'lib/ruby-progress/cli/job_cli.rb', line 155

def self.parse_advance_options(argv)
  options = {}
  opt = OptionParser.new do |o|
    o.banner = 'Usage: prg job advance [options]'
    o.on('--pid-file PATH', 'Path to daemon PID file') do |v|
      options[:pid_file] = v
    end
    o.on('--daemon-name NAME', 'Daemon name (maps to /tmp/ruby-progress/NAME.pid)') do |v|
      options[:daemon_name] = v
    end
    o.on('--amount N', Integer, 'Amount to advance (default: 1)') do |v|
      options[:amount] = v
    end
    o.on('--total N', Integer, 'Update total if needed') do |v|
      options[:total] = v
    end
  end

  opt.parse(argv)
  options
end

.parse_status_options(argv) ⇒ Object



177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
# File 'lib/ruby-progress/cli/job_cli.rb', line 177

def self.parse_status_options(argv)
  options = {}
  opt = OptionParser.new do |o|
    o.banner = 'Usage: prg job status [options]'
    o.on('--pid-file PATH', 'Path to daemon PID file') do |v|
      options[:pid_file] = v
    end
    o.on('--daemon-name NAME', 'Daemon name (maps to /tmp/ruby-progress/NAME.pid)') do |v|
      options[:daemon_name] = v
    end
  end

  opt.parse(argv)
  options
end

.parse_stop_options(argv) ⇒ Object



130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
# File 'lib/ruby-progress/cli/job_cli.rb', line 130

def self.parse_stop_options(argv)
  options = {}
  opt = OptionParser.new do |o|
    o.banner = 'Usage: prg job stop [options]'
    o.on('--pid-file PATH', 'Path to daemon PID file') do |v|
      options[:pid_file] = v
    end
    o.on('--daemon-name NAME', 'Daemon name (maps to /tmp/ruby-progress/NAME.pid)') do |v|
      options[:daemon_name] = v
    end
    o.on('--message MSG', 'Optional completion message to display') do |v|
      options[:message] = v
    end
    o.on('--checkmark', 'Display a checkmark on completion') do
      options[:checkmark] = true
    end
    o.on('--error', 'Mark completion as error state') do
      options[:error] = true
    end
  end

  opt.parse(argv)
  options
end


46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
# File 'lib/ruby-progress/cli/job_cli.rb', line 46

def self.print_help
  puts 'Usage: prg job [subcommand] [options]'
  puts
  puts 'Subcommands:'
  puts '  stop     Stop a running progress indicator'
  puts '  advance  Advance a fill progress bar'
  puts '  status   Check status of a running indicator'
  puts
  puts 'Common Options:'
  puts '  --daemon-name NAME   Name of the daemon to control'
  puts '  --pid-file PATH      Path to daemon PID file'
  puts
  puts 'Examples:'
  puts '  prg job stop --daemon-name mytask --message "Complete!"'
  puts '  prg job advance --daemon-name mybar --amount 10'
  puts '  prg job status --daemon-name mytask'
end

.resolve_pid_file(opts) ⇒ Object



64
65
66
67
68
69
70
71
72
# File 'lib/ruby-progress/cli/job_cli.rb', line 64

def self.resolve_pid_file(opts)
  if opts[:pid_file]
    opts[:pid_file]
  elsif opts[:daemon_name]
    "/tmp/ruby-progress/#{opts[:daemon_name]}.pid"
  else
    RubyProgress::Daemon.default_pid_file
  end
end

.run(argv = ARGV) ⇒ Object



18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/ruby-progress/cli/job_cli.rb', line 18

def self.run(argv = ARGV)
  if argv.empty?
    print_help
    exit 1
  end

  subcommand = argv.shift

  case subcommand
  when 'stop'
    stop(argv)
  when 'advance'
    advance(argv)
  when 'status'
    status(argv)
  when 'send'
    # Backward compatibility: 'send' is now 'stop'
    warn "Warning: 'prg job send' is deprecated. Use 'prg job stop' instead."
    stop(argv)
  when '--help', '-h'
    print_help
  else
    warn "Error: Unknown subcommand '#{subcommand}'"
    print_help
    exit 1
  end
end

.send(argv = ARGV) ⇒ Object

Backward compatibility



194
195
196
197
# File 'lib/ruby-progress/cli/job_cli.rb', line 194

def self.send(argv = ARGV)
  warn "Warning: 'JobCLI.send' is deprecated. Use 'JobCLI.stop' instead."
  stop(argv)
end

.status(argv) ⇒ Object



123
124
125
126
127
128
# File 'lib/ruby-progress/cli/job_cli.rb', line 123

def self.status(argv)
  opts = parse_status_options(argv)
  pid_file = resolve_pid_file(opts)

  RubyProgress::Daemon.show_status(pid_file)
end

.stop(argv) ⇒ Object



74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
# File 'lib/ruby-progress/cli/job_cli.rb', line 74

def self.stop(argv)
  opts = parse_stop_options(argv)
  pid_file = resolve_pid_file(opts)

  unless File.exist?(pid_file)
    warn "PID file #{pid_file} not found. Is the daemon running?"
    exit 1
  end

  RubyProgress::Daemon.stop_daemon_by_pid_file(
    pid_file,
    message: opts[:message],
    checkmark: opts[:checkmark] || false,
    error: opts[:error] || false
  )

  # Don't output confirmation - the daemon itself shows the completion message
end