Sha256: ab8f5d12f4e7121c7274016b83e5e927e439fe8a461535f0596bc1e055ce5d9c

Contents?: true

Size: 1.23 KB

Versions: 2

Compression:

Stored size: 1.23 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!
    {
      bus: Async::Bus.new(app_name),
      **container_config
    }.each { container.register(_1, _2) }

    start_metrics_server!
    run!
    info { "Started" }
  rescue StandardError => e
    fatal { e }
    stop
    exit(1)
  end
  # rubocop:enable Style/GlobalVars

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

  def stop
    @task&.stop
    info { "Stopped" }
  end

  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 force_exit!
    fatal { "Forced exit" }
    exit(1)
  end

  def start_metrics_server!
    Metrics::Server.new(prefix: app_name).tap(&:run).tap do |server|
      bus.subscribe("metrics.updated") { server.update_metrics(_1) }
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
async-tools-0.2.4 lib/async/app.rb
async-tools-0.2.2 lib/async/app.rb