Current section
Files
Jump to
Current section
Files
lib/worker.ex
defmodule Superintelligence.Worker do
use GenServer
require Logger
def start_link(opts) do
GenServer.start_link(__MODULE__, opts, opts)
end
@impl true
def init(opts) do
worker_name = case Keyword.get(opts, :name) do
{:via, Registry, {_, {:worker, id}}} -> "Worker-#{id}"
_ -> "Worker-#{:erlang.phash2(self())}"
end
Logger.info("🤖 #{worker_name} starting up...")
# Simulate some initialization work
Process.sleep(10)
Logger.info("✓ #{worker_name} ready!")
{:ok, %{
started_at: System.system_time(:second),
worker_name: worker_name
}}
end
@impl true
def handle_call(:ping, _from, state) do
Logger.debug("#{state.worker_name} received ping")
{:reply, :pong, state}
end
@impl true
def handle_call(:status, _from, state) do
uptime = System.system_time(:second) - state.started_at
status = %{
name: state.worker_name,
pid: self(),
started_at: state.started_at,
uptime_seconds: uptime
}
{:reply, status, state}
end
@impl true
def handle_info(:timeout, state) do
{:noreply, state}
end
end