Class: Kaisoku::Preload::Server

Inherits:
Object
  • Object
show all
Defined in:
lib/kaisoku/preload/server.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(configuration: Configuration.new, checksum: Checksum.new) ⇒ Server

Returns a new instance of Server.



10
11
12
13
14
15
16
17
18
19
20
# File 'lib/kaisoku/preload/server.rb', line 10

def initialize(configuration: Configuration.new, checksum: Checksum.new)
  @configuration = configuration
  @checksum = checksum
  @generation = 0
  @last_restart_reason = 'initial boot'
  @last_heartbeat_at = nil
  @worker_failures = 0
  @disabled_reason = nil
  @loaded_checksums = {}
  boot!
end

Instance Attribute Details

#disabled_reasonObject (readonly)

Returns the value of attribute disabled_reason.



8
9
10
# File 'lib/kaisoku/preload/server.rb', line 8

def disabled_reason
  @disabled_reason
end

#generationObject (readonly)

Returns the value of attribute generation.



8
9
10
# File 'lib/kaisoku/preload/server.rb', line 8

def generation
  @generation
end

#last_heartbeat_atObject (readonly)

Returns the value of attribute last_heartbeat_at.



8
9
10
# File 'lib/kaisoku/preload/server.rb', line 8

def last_heartbeat_at
  @last_heartbeat_at
end

#last_restart_reasonObject (readonly)

Returns the value of attribute last_restart_reason.



8
9
10
# File 'lib/kaisoku/preload/server.rb', line 8

def last_restart_reason
  @last_restart_reason
end

#worker_failuresObject (readonly)

Returns the value of attribute worker_failures.



8
9
10
# File 'lib/kaisoku/preload/server.rb', line 8

def worker_failures
  @worker_failures
end

Instance Method Details

#boot!Object



22
23
24
25
26
# File 'lib/kaisoku/preload/server.rb', line 22

def boot!
  @generation += 1
  heartbeat!
  @loaded_checksums = loaded_file_checksums
end

#disable!(reason) ⇒ Object



90
91
92
93
# File 'lib/kaisoku/preload/server.rb', line 90

def disable!(reason)
  @disabled_reason = reason
  @last_restart_reason = reason
end

#disabled?Boolean

Returns:

  • (Boolean)


95
96
97
# File 'lib/kaisoku/preload/server.rb', line 95

def disabled?
  !disabled_reason.nil?
end

#refresh_if_needed(changed_paths) ⇒ Object



35
36
37
38
39
40
41
42
# File 'lib/kaisoku/preload/server.rb', line 35

def refresh_if_needed(changed_paths)
  return false if disabled?
  return false unless stale_for?(changed_paths)

  @last_restart_reason = "changed: #{Array(changed_paths).join(', ')}"
  boot!
  true
end

#runObject



44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
# File 'lib/kaisoku/preload/server.rb', line 44

def run
  raise Error, "preload disabled: #{disabled_reason}" if disabled?

  refresh_if_needed([])
  heartbeat!
  reader, writer = IO.pipe
  pid = fork do
    begin
      reader.close
      Marshal.dump(yield, writer)
    ensure
      writer.close
    end
    exit! 0
  end
  writer.close
  result = Marshal.load(reader)
  Process.wait(pid)
  if $CHILD_STATUS.success?
    @worker_failures = 0
    heartbeat!
    return result
  end

  @worker_failures += 1
  @last_restart_reason = "worker failure: status #{$CHILD_STATUS.exitstatus}"
  boot!
  raise Error, "preload worker exited with status #{$CHILD_STATUS.exitstatus}"
rescue StandardError
  raise
ensure
  reader&.close
end

#stale_for?(changed_paths) ⇒ Boolean

Returns:

  • (Boolean)


28
29
30
31
32
33
# File 'lib/kaisoku/preload/server.rb', line 28

def stale_for?(changed_paths)
  Array(changed_paths).any? do |path|
    relative = @configuration.relative_path(path)
    @configuration.boot_impact_file?(relative) || loaded_file_changed?(relative)
  end
end

#statusObject



78
79
80
81
82
83
84
85
86
87
88
# File 'lib/kaisoku/preload/server.rb', line 78

def status
  {
    generation: generation,
    loaded_files: @loaded_checksums.length,
    last_restart_reason: last_restart_reason,
    last_heartbeat_at: last_heartbeat_at,
    worker_failures: worker_failures,
    disabled_reason: disabled_reason,
    healthy: !disabled?
  }
end