Module: Cybertrain::Background

Defined in:
cybertrain/background.rb

Overview

Cybertrain::Background – services Application#serve starts and stops without application.rb requiring them.

A file that runs something long-lived beside the server (the WebSocket client’s subscriptions, cybertrain/websocket.rb) subclasses Service and registers an instance when it is required:

class FeedService < Cybertrain::Background::Service def service_name = “feeds” def service_start(config) = Feeds.start!(config) def service_stop = Feeds.stop! end Cybertrain::Background.register(FeedService.new)

Application#serve calls Background.start_all right after Scheduler.start! (the database is connected, the port bound and SPINEL_WORKERS set: spikes/NOTES.md rule 22) and Background.stop_all right after Scheduler.stop!. ./<app> migrate, db ..., Main.command and Test::Client never start them. The same pattern as Net.tls_connector: a do-nothing default here, the real one in the file that is required only by the programs that use it (so a program that never opens a WebSocket does not link it, nor libssl).

Defined Under Namespace

Classes: Service

Class Method Summary collapse

Class Method Details

.boot_problem(config) ⇒ String

The first registered service’s Cybertrain::Background::Service#service_problem, “” when none has one, or when Config#worker is off (nothing starts then). Application#serve calls it before binding the port and fails the boot with it.

Parameters:

Returns:

  • (String)


103
104
105
106
107
108
109
110
111
112
113
114
# File 'cybertrain/background.rb', line 103

def self.boot_problem(config)
  return "" unless config.worker

  i = 0
  while i < SERVICES.size
    problem = SERVICES[i].service_problem
    return problem unless problem.empty?

    i += 1
  end
  ""
end

.register(service) ⇒ nil

Adds a service for start_all to start. Register before the server starts (when the file defining it is required).

Parameters:

Returns:

  • (nil)

Raises:

  • (ArgumentError) —

    when a service of the same name is registered



78
79
80
81
82
83
84
# File 'cybertrain/background.rb', line 78

def self.register(service)
  name = service.service_name
  raise ArgumentError, "Background.register #{name.inspect}: already registered" if service_index(name) >= 0

  SERVICES << service
  nil
end

.start_all(config) ⇒ Integer

Starts every registered service, in registration order, unless Config#worker is off (CYBERTRAIN_WORKER=0: the process that runs the background work is another one).

Parameters:

Returns:

  • (Integer) —

    how many services’ service_start returned true



122
123
124
125
126
127
128
129
130
131
132
# File 'cybertrain/background.rb', line 122

def self.start_all(config)
  return 0 unless config.worker

  started = 0
  i = 0
  while i < SERVICES.size
    started += 1 if SERVICES[i].service_start(config)
    i += 1
  end
  started
end

.stop_all ⇒ nil

Asks every registered service to end, in registration order.

Returns:

  • (nil)


137
138
139
140
141
142
143
144
# File 'cybertrain/background.rb', line 137

def self.stop_all
  i = 0
  while i < SERVICES.size
    SERVICES[i].service_stop
    i += 1
  end
  nil
end