Class: Do::Manager

Inherits:
Object
  • Object
show all
Defined in:
lib/do/manager.rb

Overview

Orchestrates the lifecycle of generated units against the systemd user manager. This is the layer that turns config into reality: writing unit files, detecting and removing stale do-managed units, reloading systemd, and enabling/disabling schedulers.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(systemd: Systemd.new, config: nil) ⇒ Manager

Returns a new instance of Manager.



14
15
16
17
# File 'lib/do/manager.rb', line 14

def initialize(systemd: Systemd.new, config: nil)
  @systemd = systemd
  @config = config
end

Instance Attribute Details

#config=(value) ⇒ Object (writeonly)

Sets the attribute config

Parameters:

  • value

    the value to set the attribute config to.



19
20
21
# File 'lib/do/manager.rb', line 19

def config=(value)
  @config = value
end

#systemdObject (readonly)

Returns the value of attribute systemd.



12
13
14
# File 'lib/do/manager.rb', line 12

def systemd
  @systemd
end

Instance Method Details

#reload(config = @config) ⇒ Object

Apply the configuration to the user systemd manager.

Never overwrites units without the do marker. Refuses to run when the configuration is invalid.



25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/do/manager.rb', line 25

def reload(config = @config)
  Validator.validate!(config)
  dir = @systemd.unit_dir
  FileUtils.mkdir_p(dir)

  expected = {}
  config.tasks.each do |task|
    next unless task.scheduled?

    expected[task.service_unit] =
      UnitGenerator.service_content(task, source_path: config.path)
    expected[task.timer_unit] = UnitGenerator.timer_content(task, source_path: config.path)
  end

  write_units(dir, expected)
  remove_stale_units(dir, expected.keys)
  @systemd.daemon_reload
  apply_enabled(config)
  :ok
end

#remove_units_for(task) ⇒ Object

Remove all units a task owns, regardless of whether the task is scheduled. Used by do remove.



56
57
58
59
60
61
62
63
64
# File 'lib/do/manager.rb', line 56

def remove_units_for(task)
  [task.service_unit, task.timer_unit].each do |unit|
    path = File.join(@systemd.unit_dir, unit)
    FileUtils.rm_f(path) if File.exist?(path) && managed?(path)
    @systemd.disable(unit) if @systemd.exists?(unit)
  end
  @systemd.daemon_reload
  :ok
end

#unschedule(task, _config = @config) ⇒ Object

Remove a task's generated units (if any) and reload. Does not touch the TOML configuration.



48
49
50
51
52
# File 'lib/do/manager.rb', line 48

def unschedule(task, _config = @config)
  remove_units_for(task)
  @systemd.daemon_reload
  :ok
end