Class: LogBrew::WorkerLifecycle

Inherits:
Object
  • Object
show all
Defined in:
lib/logbrew/worker_lifecycle.rb

Overview

Explicit delivery boundaries for serialized prefork worker loops.

Constant Summary collapse

SAFE_DELIVERY_CODES =
%w[
  delivery_error
  flush_error
  network_failure
  shutdown_error
  transport_error
  unauthenticated
  validation_error
].freeze

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(client:, transport:, on_delivery_failure:, owner_process_id:) ⇒ WorkerLifecycle

Returns a new instance of WorkerLifecycle.



59
60
61
62
63
64
65
66
67
# File 'lib/logbrew/worker_lifecycle.rb', line 59

def initialize(client:, transport:, on_delivery_failure:, owner_process_id:)
  @client = client
  @transport = transport
  @on_delivery_failure = on_delivery_failure
  @owner_process_id = owner_process_id
  @state_mutex = Mutex.new
  @operation_active = false
  @shutdown_response = nil
end

Class Method Details

.create(client:, transport:, on_delivery_failure: nil) ⇒ Object



30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/logbrew/worker_lifecycle.rb', line 30

def self.create(client:, transport:, on_delivery_failure: nil)
  unless client.is_a?(Client)
    raise SdkError.new("validation_error", "client must be a LogBrew::Client")
  end
  unless transport.respond_to?(:send)
    raise SdkError.new("validation_error", "transport must respond to send")
  end
  if !on_delivery_failure.nil? && !on_delivery_failure.respond_to?(:call)
    raise SdkError.new("validation_error", "on_delivery_failure must be callable")
  end

  new(
    client: client,
    transport: transport,
    on_delivery_failure: on_delivery_failure,
    owner_process_id: current_process_id
  )
end

Instance Method Details

#runObject



70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
# File 'lib/logbrew/worker_lifecycle.rb', line 70

def run
  assert_process_ownership
  begin_run
  begin
    application_error = nil
    result = nil
    begin
      result = yield
    rescue Exception => error # rubocop:disable Lint/RescueException
      application_error = error
    ensure
      finish_work_boundary(application_error)
    end

    raise application_error unless application_error.nil?

    result
  ensure
    end_operation
  end
end

#shutdownObject



92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
# File 'lib/logbrew/worker_lifecycle.rb', line 92

def shutdown
  assert_process_ownership
  cached_response = begin_shutdown
  return cached_response unless cached_response.nil?

  completed = false
  begin
    begin
      response = @client.shutdown(@transport)
    rescue StandardError => delivery_error
      report_delivery_failure("shutdown", delivery_error)
      raise delivery_error
    end

    complete_shutdown(response)
    completed = true
    response
  ensure
    end_operation unless completed
  end
end