class Postjob::Host

Constants

UUID_REGEXP

Attributes

attributes[R]
created_at[R]
id[R]
status[R]

Public Class Methods

clear_storage() click to toggle source
# File lib/postjob/host.rb, line 12
def clear_storage
  @host_id = nil

  File.unlink(storage_path) if File.exist?(storage_path)
end
host_id() click to toggle source
# File lib/postjob/host.rb, line 29
def host_id
  @host_id ||= atomic_set_and_get(storage_path) { register_host(host_id: nil) }
end
host_id=(host_id) click to toggle source
# File lib/postjob/host.rb, line 20
def host_id=(host_id)
  expect! host_id => UUID_REGEXP

  return if @host_id == host_id

  register_host(host_id: host_id)
  @host_id = host_id
end
shutdown!(host_id:) click to toggle source
# File lib/postjob/host.rb, line 33
def shutdown!(host_id:)
  Simple::SQL.ask "UPDATE postjob.hosts SET status='shutdown' WHERE id=$1::uuid", host_id
end

Private Class Methods

atomic_set_and_get(path) { || ... } click to toggle source
# File lib/postjob/host.rb, line 53
def atomic_set_and_get(path)
  File.open(path, File::RDWR | File::CREAT, 0644) do |f|
    f.flock(File::LOCK_EX)

    value = f.read

    if value == "" || value.nil?
      value = yield

      f.rewind
      f.write(value)
      f.flush
      f.truncate(f.pos)

      Postjob.logger.info "Registering new host with host_id #{value}, in #{path}"
    else
      Postjob.logger.info "Reusing host_id #{value}, from #{path}"
    end

    value
  end
end
register_host(host_id:) click to toggle source
# File lib/postjob/host.rb, line 76
def register_host(host_id:)
  expect! host_id => [ nil, UUID_REGEXP ]
  attributes = {}
  Postjob.logger.debug "registering host w/#{attributes.inspect}"
  ::Postjob::Queue.host_register(attributes, host_id: host_id)
end
storage_path() click to toggle source

This method returns the path to a file which will hold the host_id. Two runners with the same host_id should have access to the same part of the file system. Therefore different system users need to have different host ids.

In addition the path returned from the method must be readable and writable by the current user. We choose a tmp location for that reason. (A /var location would be even better - however, our systems do not have a user-writable /var).

# File lib/postjob/host.rb, line 46
def storage_path
  @storage_path ||= begin
    here_hash = Zlib.crc32(Dir.getwd).to_s(36)
    File.join Dir.tmpdir, "postjob.#{Postjob.env}.#{Process.uid}.#{here_hash}.host_id"
  end
end