defmodule DaProductApp.Application do # See https://hexdocs.pm/elixir/Application.html # for more information on OTP Applications @moduledoc false use Application require Logger alias DaProductApp.MercuryISO8583.AsyncCorrelatorStartup alias DaProductApp.Startup.{EventSystemInitializer, GracefulShutdown} @impl true def start(_type, _args) do # Validate AsyncCorrelator configuration early case AsyncCorrelatorStartup.validate_configuration() do {:ok, _} -> Logger.info("AsyncCorrelator configuration validation passed") {:error, reason} -> Logger.error("AsyncCorrelator configuration validation failed: #{inspect(reason)}") Logger.warn("Application will start but async correlation will be disabled") end children = [ DaProductAppWeb.Telemetry, DaProductApp.Repo, {DNSCluster, query: Application.get_env(:da_product_app, :dns_cluster_query) || :ignore}, {Phoenix.PubSub, name: DaProductApp.PubSub}, # Start the Finch HTTP client for sending emails {Finch, name: DaProductApp.Finch}, # Start a worker by calling: DaProductApp.Worker.start_link(arg) # {DaProductApp.Worker, arg}, # ISO8583 Switch System - Enhanced Architecture # 0. Event System: Event-driven architecture for payment processing DaProductApp.Events.EventDispatcher, # 0. ProcessorRegistry: Manages configurable message processor pipelines DaProductApp.MercuryISO8583.ProcessorRegistry, # 0.1. NetworkConnectorRegistry: Registry for network connector processes {Registry, keys: :unique, name: DaProductApp.NetworkConnectorRegistry}, # 1. AsyncCorrelator System: Manages async correlation for high-volume networks # Reads configuration from config/async_correlator.exs DaProductApp.MercuryISO8583.AsyncCorrelatorSupervisor, # 1b. AsyncCorrelator Monitor: Runtime monitoring and management DaProductApp.MercuryISO8583.AsyncCorrelatorMonitor, # 2. Enhanced UpstreamSupervisor: Manages upstream network connections (VISA/MasterCard/etc) # Reads upstream configurations from config/upstream_networks.exs DaProductApp.Switch.EnhancedUpstreamSupervisor, # 2.1. Health Event Cleanup: Periodic database cleanup for health monitoring DaProductApp.Switch.HealthEventCleanup, # 3. Enhanced DeviceListenerSupervisor: Manages multiple protocol listeners # Reads listener configurations from config/incoming_listeners.exs DaProductApp.Switch.EnhancedDeviceListenerSupervisor, # Start to serve requests, typically the last entry DaProductAppWeb.Endpoint ] Logger.info("Starting application with ISO8583 switch system - listeners configured via incoming_listeners.exs") # See https://hexdocs.pm/elixir/Supervisor.html # for other strategies and supported options opts = [strategy: :one_for_one, name: DaProductApp.Supervisor] case Supervisor.start_link(children, opts) do {:ok, pid} = result -> Logger.info("Application supervisor started successfully") # Register event listeners after the EventDispatcher is running Task.start(fn -> :timer.sleep(1000) # Give EventDispatcher time to fully initialize EventSystemInitializer.register_event_listeners() end) result {:error, reason} = error -> Logger.error("Failed to start application supervisor: #{inspect(reason)}") error end end # Handle graceful application shutdown @impl true def stop(_state) do Logger.info("Application stopping - delegating to graceful shutdown handler") GracefulShutdown.perform_shutdown(30_000) end # Tell Phoenix to update the endpoint configuration # whenever the application is updated. @impl true def config_change(changed, _new, removed) do DaProductAppWeb.Endpoint.config_change(changed, removed) :ok end end