Skip to content

Commit

Permalink
Add snapshot worker
Browse files Browse the repository at this point in the history
  • Loading branch information
Clivern committed Mar 24, 2024
1 parent c9e377f commit 81a4182
Show file tree
Hide file tree
Showing 3 changed files with 58 additions and 2 deletions.
4 changes: 2 additions & 2 deletions lib/lynx/application.ex
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,9 @@ defmodule Lynx.Application do
# Start the PubSub system
{Phoenix.PubSub, name: Lynx.PubSub},
# Start the Endpoint (http/https)
LynxWeb.Endpoint
LynxWeb.Endpoint,
# Start a worker by calling: Lynx.Worker.start_link(arg)
# {Lynx.Worker, arg}
{Lynx.Workers, %{}}
]

# See https://hexdocs.pm/elixir/Supervisor.html
Expand Down
36 changes: 36 additions & 0 deletions lib/lynx/worker/snapshot.ex
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
# Copyright 2023 Clivern. All rights reserved.
# Use of this source code is governed by the MIT
# license that can be found in the LICENSE file.

defmodule Lynx.Worker.SnapshotWorker do
use GenServer

require Logger

def start_link(state) do
GenServer.start_link(__MODULE__, state, name: __MODULE__)
end

## Callbacks
@impl true
def init(state) do
Logger.info("Snapshor Worker Started")

schedule_work()

{:ok, state}
end

@impl true
def handle_info(:fire, state) do
# Reschedule once more
schedule_work()

{:noreply, state}
end

defp schedule_work do
# We schedule the work to happen in 60 seconds
Process.send_after(self(), :fire, 60 * 1000)
end
end
20 changes: 20 additions & 0 deletions lib/lynx/workers.ex
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
# Copyright 2023 Clivern. All rights reserved.
# Use of this source code is governed by the MIT
# license that can be found in the LICENSE file.

defmodule Lynx.Workers do
use Supervisor

def start_link(init_state) do
Supervisor.start_link(__MODULE__, init_state, name: __MODULE__)
end

@impl true
def init(_init_state) do
children = [
{Lynx.Worker.SnapshotWorker, %{}}
]

Supervisor.init(children, strategy: :one_for_one)
end
end

0 comments on commit 81a4182

Please sign in to comment.