This commit is contained in:
Marc-Éric Martel 2023-10-22 10:39:08 -04:00
commit a8ffb43ec8
12 changed files with 231 additions and 0 deletions

View File

@ -0,0 +1,4 @@
# Used by "mix format"
[
inputs: ["{mix,.formatter}.exs", "{config,lib,test}/**/*.{ex,exs}"]
]

26
global_bck_job/.gitignore vendored Normal file
View File

@ -0,0 +1,26 @@
# The directory Mix will write compiled artifacts to.
/_build/
# If you run "mix test --cover", coverage assets end up here.
/cover/
# The directory Mix downloads your dependencies sources to.
/deps/
# Where third-party dependencies like ExDoc output generated docs.
/doc/
# Ignore .fetch files in case you like to edit your project deps locally.
/.fetch
# If the VM crashes, it generates a dump, let's ignore it too.
erl_crash.dump
# Also ignore archive artifacts (built via "mix archive.build").
*.ez
# Ignore package tarball (built via "mix hex.build").
global_bck_job-*.tar
# Temporary files, for example, from tests.
/tmp/

21
global_bck_job/README.md Normal file
View File

@ -0,0 +1,21 @@
# GlobalBckJob
**TODO: Add description**
## Installation
If [available in Hex](https://hex.pm/docs/publish), the package can be installed
by adding `global_bck_job` to your list of dependencies in `mix.exs`:
```elixir
def deps do
[
{:global_bck_job, "~> 0.1.0"}
]
end
```
Documentation can be generated with [ExDoc](https://github.com/elixir-lang/ex_doc)
and published on [HexDocs](https://hexdocs.pm). Once published, the docs can
be found at <https://hexdocs.pm/global_bck_job>.

View File

@ -0,0 +1,18 @@
defmodule GlobalBackgroundJob do
@moduledoc """
Documentation for `GlobalBckJob`.
"""
@doc """
Hello world.
## Examples
iex> GlobalBackgroundJob.hello()
:world
"""
def hello do
:world
end
end

View File

@ -0,0 +1,42 @@
defmodule GlobalBackgroundJob.Application do
# See https://hexdocs.pm/elixir/Application.html
# for more information on OTP Applications
@moduledoc false
use Application
@impl true
def start(_type, _args) do
children = [
# Starts a worker by calling: GlobalBckJob.Worker.start_link(arg)
# {GlobalBackgroundJob.Worker, arg}
{Cluster.Supervisor, [topologies(), [name: GlobalBackgroundJob.ClusterSupervisor]]},
{GlobalBackgroundJob.DatabaseCleaner.Starter,[timeout: :timer.seconds(2)]}
]
# See https://hexdocs.pm/elixir/Supervisor.html
# for other strategies and supported options
opts = [strategy: :one_for_one, name: GlobalBackgroundJob.Supervisor]
Supervisor.start_link(children, opts)
end
defp topologies do
[
background_job: [
strategy: Cluster.Strategy.Gossip,
config: [
# port: 45892,
if_addr: "10.13.4.143",
multicast_if: "10.13.4.143", #addr locale
multicast_addr: "255.255.255.255",
multicast_ttl: 1,
secret: "secret",
broadcast_only: true
]
]
]
end
end

View File

@ -0,0 +1,29 @@
defmodule GlobalBackgroundJob.DatabaseCleaner do
use GenServer
require Logger
alias __MODULE__.Runner
@impl GenServer
def init(args \\ []) do
timeout = Keyword.get(args, :timeout)
schedule(timeout)
{:ok, timeout}
end
@impl GenServer
def handle_info(:execute, timeout) do
Task.start(Runner, :execute, [])
schedule(timeout)
{:noreply, timeout}
end
defp schedule(timeout) do
Process.send_after(self(), :execute, timeout)
end
end

View File

@ -0,0 +1,12 @@
defmodule GlobalBackgroundJob.DatabaseCleaner.Runner do
require Logger
def execute do
random = :rand.uniform(1_000)
Process.sleep(random)
Logger.info("#{__MODULE__} #{random} Mucho trabajo!")
end
end

View File

@ -0,0 +1,36 @@
defmodule GlobalBackgroundJob.DatabaseCleaner.Starter do
use GenServer
alias GlobalBackgroundJob.DatabaseCleaner
def start_link(opts) do
GenServer.start_link(__MODULE__, opts, name: __MODULE__)
end
@impl GenServer
def init(opts) do
pid = start_and_monitor(opts)
{:ok, {pid, opts}}
end
@impl GenServer
def handle_info({:DOWN, _, :process, pid, _reason}, {pid,opts} = state) do
{:noreply, {start_and_monitor(opts), opts}}
end
defp start_and_monitor(opts) do
pid =
case GenServer.start_link(DatabaseCleaner, opts, name: {:global, DatabaseCleaner}) do
{:ok, pid} ->
pid
{:error, {:already_started, pid}} ->
pid
end
Process.monitor(pid)
pid
end
end

30
global_bck_job/mix.exs Normal file
View File

@ -0,0 +1,30 @@
defmodule GlobalBckJob.MixProject do
use Mix.Project
def project do
[
app: :global_bck_job,
version: "0.1.0",
elixir: "~> 1.15",
start_permanent: Mix.env() == :prod,
deps: deps()
]
end
# Run "mix help compile.app" to learn about applications.
def application do
[
extra_applications: [:logger],
mod: {GlobalBackgroundJob.Application, []}
]
end
# Run "mix help deps" to learn about dependencies.
defp deps do
[
{:libcluster, "~> 3.3"}
# {:dep_from_hexpm, "~> 0.3.0"},
# {:dep_from_git, git: "https://github.com/elixir-lang/my_dep.git", tag: "0.1.0"}
]
end
end

4
global_bck_job/mix.lock Normal file
View File

@ -0,0 +1,4 @@
%{
"jason": {:hex, :jason, "1.4.1", "af1504e35f629ddcdd6addb3513c3853991f694921b1b9368b0bd32beb9f1b63", [:mix], [{:decimal, "~> 1.0 or ~> 2.0", [hex: :decimal, repo: "hexpm", optional: true]}], "hexpm", "fbb01ecdfd565b56261302f7e1fcc27c4fb8f32d56eab74db621fc154604a7a1"},
"libcluster": {:hex, :libcluster, "3.3.3", "a4f17721a19004cfc4467268e17cff8b1f951befe428975dd4f6f7b84d927fe0", [:mix], [{:jason, "~> 1.1", [hex: :jason, repo: "hexpm", optional: false]}], "hexpm", "7c0a2275a0bb83c07acd17dab3c3bfb4897b145106750eeccc62d302e3bdfee5"},
}

View File

@ -0,0 +1,8 @@
defmodule GlobalBckJobTest do
use ExUnit.Case
doctest GlobalBckJob
test "greets the world" do
assert GlobalBckJob.hello() == :world
end
end

View File

@ -0,0 +1 @@
ExUnit.start()