Module: Cybertrain::Main

Defined in:
cybertrain/main.rb,
cybertrain/jobs.rb

Overview

Cybertrain::Main – what bin/.rb runs, the one binary an app builds (`spin build blog` -> build/bin/blog; `cybertrain build` -> dist/blog):

./blog serve (port from PORT / config) ./blog server 3000 serve on 3000 (a bare “3000” too: the dev loop’s execv passes the port that way) ./blog migrate apply db/migrate (gen/migrations.rb) ./blog db status any DB::CLI command: status, rollback N, schema, schema:dump, create, backup PATH, vacuum ./blog worker jobs and scheduled tasks, no HTTP (several may share a database) ./blog jobs list any Job::CLI command: list [STATE], retry ID|–all, discard ID, delete ID, prune [–before DAYS] (worker and jobs with require “cybertrain/jobs”: JobsFeature#run_feature_command) ./blog feeds [NAME…] the WebSocket subscriptions (all, or the named ones) ./blog feeds status each feed’s health, from the status rows ./blog console the expression console over the models (Console.run) ./blog dbconsole the sqlite3 shell on the configured database ./blog user a@b.c a command the application registered with Main.command

gen stays a separate bin/gen.rb: it needs the generator and the routes/schema DSL, none of which belong in the production binary.

Class Method Summary collapse

Class Method Details

.command(name, description) {|argv| ... } ⇒ nil

Registers an operational command for the application’s binary: ./<name> NAME ARGS... runs the block instead of the server, and ./<name> help lists it. Call it in bin/.rb before `Cybertrain::Main.run` (or in a file bin/.rb requires).

Before the block runs, Cybertrain::DB is connected to Cybertrain.config.database_path with a pool of one connection (and disconnected afterwards), the way ./<name> db ... works, and the views and the URL resolver are configured as the server’s boot does, so Cybertrain::Views.render_partial, render_component and the Stream.broadcast_*_to(partial: / component:) shortcuts work; no server, scheduler, threads or session secret are set up. A Cybertrain::DB::Error raised by the block (or by the connect) prints NAME failed: MESSAGE and exits 1. Any other exception is not rescued.

The block receives the arguments after the command’s name as its one argument (Spinel has no instance_eval, so there is no implicit receiver, like before_action { |c| ... }) and returns the exit status, an Integer.

Examples:

bin/blog.rb, before exit(Cybertrain::Main.run(...))

Cybertrain::Main.command("prune", "delete comments older than DAYS") do |argv|
  days = argv.empty? ? 30 : argv[0].to_i
  Cybertrain::DB.with do |c|
    c.execute("DELETE FROM comments WHERE created_at < datetime('now', ?)", ["-#{days} days"])
  end
  puts "pruned"
  0
end

Parameters:

  • name (String) —

    the first command-line word; not a built-in one (server, worker, feeds, migrate, db, jobs, console, dbconsole, help), not a port number, not taken

  • description (String) —

    the line help prints next to it

Yield Parameters:

  • argv (Array<String>) —

    the arguments after name

Yield Returns:

  • (Integer) —

    the process’s exit status

Returns:

  • (nil)

Raises:

  • (ArgumentError) —

    for a reserved, numeric, empty or taken name



93
94
95
96
97
98
99
# File 'cybertrain/main.rb', line 93

def self.command(name, description, &block)
  problem = command_name_problem(name)
  raise ArgumentError, "Main.command #{name.inspect}: #{problem}" unless problem.empty?

  COMMANDS << Command.new(name, description, block)
  nil
end

.feeds?(argv) ⇒ Boolean

Whether run runs these arguments as ./<app> feeds [NAME...]: the WebSocket subscriptions, no HTTP and no jobs.

Parameters:

  • argv (Array<String>) —

    the binary’s ARGV

Returns:

  • (Boolean)


333
334
335
# File 'cybertrain/main.rb', line 333

def self.feeds?(argv)
  !argv.empty? && argv[0] == "feeds"
end

.server?(argv) ⇒ Boolean

Whether run starts the server for these arguments: [], [“server”], [“"], ["server", ""]; never a registered command's name, so whatever starts only for a server (the scheduler, a boot-time check) stays off for a one-shot command. Ask it after the commands are registered.

Examples:

bin/.rb, before `exit(Cybertrain::Main.run(...))`

UserCommand.warn_if_no_user if Cybertrain::Main.server?(ARGV)

Parameters:

  • argv (Array<String>) —

    the binary’s ARGV

Returns:

  • (Boolean)


311
312
313
314
315
316
317
# File 'cybertrain/main.rb', line 311

def self.server?(argv)
  return true if argv.empty?
  return false if command_index(argv[0]) >= 0
  return true if argv[0] == "server"

  Application.port_argument(argv) > 0
end

.worker?(argv) ⇒ Boolean

Whether run runs these arguments as ./<app> worker: the jobs and the scheduler, no HTTP.

Parameters:

  • argv (Array<String>) —

    the binary’s ARGV

Returns:

  • (Boolean)


324
325
326
# File 'cybertrain/main.rb', line 324

def self.worker?(argv)
  !argv.empty? && argv[0] == "worker"
end