commit a8ffb43ec893cd2c1c0b4616524b6703cd8c660c Author: Marc-Eric Martel Date: Sun Oct 22 10:39:08 2023 -0400 init diff --git a/global_bck_job/.formatter.exs b/global_bck_job/.formatter.exs new file mode 100644 index 0000000..d2cda26 --- /dev/null +++ b/global_bck_job/.formatter.exs @@ -0,0 +1,4 @@ +# Used by "mix format" +[ + inputs: ["{mix,.formatter}.exs", "{config,lib,test}/**/*.{ex,exs}"] +] diff --git a/global_bck_job/.gitignore b/global_bck_job/.gitignore new file mode 100644 index 0000000..0fba284 --- /dev/null +++ b/global_bck_job/.gitignore @@ -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/ diff --git a/global_bck_job/README.md b/global_bck_job/README.md new file mode 100644 index 0000000..9e676d4 --- /dev/null +++ b/global_bck_job/README.md @@ -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 . + diff --git a/global_bck_job/lib/global_bck_job.ex b/global_bck_job/lib/global_bck_job.ex new file mode 100644 index 0000000..e15db3e --- /dev/null +++ b/global_bck_job/lib/global_bck_job.ex @@ -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 diff --git a/global_bck_job/lib/global_bck_job/application.ex b/global_bck_job/lib/global_bck_job/application.ex new file mode 100644 index 0000000..9ec69c5 --- /dev/null +++ b/global_bck_job/lib/global_bck_job/application.ex @@ -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 diff --git a/global_bck_job/lib/global_bck_job/dbase_cleaner.ex b/global_bck_job/lib/global_bck_job/dbase_cleaner.ex new file mode 100644 index 0000000..9bff1c4 --- /dev/null +++ b/global_bck_job/lib/global_bck_job/dbase_cleaner.ex @@ -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 diff --git a/global_bck_job/lib/global_bck_job/runner.ex b/global_bck_job/lib/global_bck_job/runner.ex new file mode 100644 index 0000000..3cd918b --- /dev/null +++ b/global_bck_job/lib/global_bck_job/runner.ex @@ -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 diff --git a/global_bck_job/lib/global_bck_job/starter.ex b/global_bck_job/lib/global_bck_job/starter.ex new file mode 100644 index 0000000..f23ece9 --- /dev/null +++ b/global_bck_job/lib/global_bck_job/starter.ex @@ -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 diff --git a/global_bck_job/mix.exs b/global_bck_job/mix.exs new file mode 100644 index 0000000..3afd7f4 --- /dev/null +++ b/global_bck_job/mix.exs @@ -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 diff --git a/global_bck_job/mix.lock b/global_bck_job/mix.lock new file mode 100644 index 0000000..e38cdc0 --- /dev/null +++ b/global_bck_job/mix.lock @@ -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"}, +} diff --git a/global_bck_job/test/global_bck_job_test.exs b/global_bck_job/test/global_bck_job_test.exs new file mode 100644 index 0000000..e2c48ae --- /dev/null +++ b/global_bck_job/test/global_bck_job_test.exs @@ -0,0 +1,8 @@ +defmodule GlobalBckJobTest do + use ExUnit.Case + doctest GlobalBckJob + + test "greets the world" do + assert GlobalBckJob.hello() == :world + end +end diff --git a/global_bck_job/test/test_helper.exs b/global_bck_job/test/test_helper.exs new file mode 100644 index 0000000..869559e --- /dev/null +++ b/global_bck_job/test/test_helper.exs @@ -0,0 +1 @@ +ExUnit.start()