Sha256: 3c208c7f9866bfb3fd0826d36fae42c0512bac40f4d1a9fb666c823a576a7039

Contents?: true

Size: 1.42 KB

Versions: 2

Compression:

Stored size: 1.42 KB

Contents

# frozen_string_literal: true

class Async::App
  extend Async::App::Injector

  include Async::Logger

  inject :bus

  # rubocop:disable Style/GlobalVars
  def initialize
    raise "only one instance of #{self.class} is allowed" if $__ASYNC_APP

    $__ASYNC_APP = self
    @task = Async::Task.current

    set_traps!
    init_container!

    start_event_logger!
    start_web_server!

    start_runtime_metrics_collector!

    run!

    info { "Started" }
    bus.publish("health.updated", true)
  rescue StandardError => e
    fatal { e }
    stop
    exit(1)
  end

  def container = @container ||= Dry::Container.new
  def run! = nil
  def container_config = {}
  def app_name = :async_app

  def stop
    @task&.stop
    $__ASYNC_APP = nil
    info { "Stopped" }
  end

  # rubocop:enable Style/GlobalVars

  private

  def set_traps!
    trap("INT") do
      force_exit! if @stopping
      @stopping = true
      warn { "Interrupted, stopping. Press ^C once more to force exit." }
      stop
    end

    trap("TERM") { stop }
  end

  def init_container!
    {
      bus: Async::Bus.new,
      **container_config
    }.each { container.register(_1, _2) }
  end

  def force_exit!
    fatal { "Forced exit" }
    exit(1)
  end

  def start_web_server! = WebServer.new(metrics_prefix: app_name).run
  def start_event_logger! = EventLogger.new.run
  def start_runtime_metrics_collector! = Async::App::Metrics::RubyRuntimeMetricsCollector.new.run
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
async-tools-0.2.9 lib/async/app.rb
async-tools-0.2.8 lib/async/app.rb