Skip to content

Instantly share code, notes, and snippets.

@x-ji
x-ji / Markdium-elixir.ex
Created Dec 12, 2019
Markdium-Elixir process orchestration in Kubernetes with libcluster and swarm
View Markdium-elixir.ex
defmodule Yourapp.YourWorker do
use GenServer
def start_link(state) do
GenServer.start_link(__MODULE__, state)
end
def init(opts) do
initial_state = initialize_worker(opts)
@x-ji
x-ji / Markdium-elixir.ex
Created Dec 12, 2019
Markdium-Elixir process orchestration in Kubernetes with libcluster and swarm
View Markdium-elixir.ex
# Change the handling of :begin_handoff
# This is triggered whenever a registered process is to be killed.
def handle_call({:swarm, :begin_handoff}, _from, current_state) do
{:reply, {:resume, produce_outgoing_state(current_state)}, current_state}
end
# Handle :end_handoff
# This is triggered whenever a process has been restarted on a new node.
def handle_call({:swarm, :end_handoff, incoming_state}, _from, current_state) do
{:noreply, end_handoff_new_state(current_state, incoming_state)}
@x-ji
x-ji / Markdium-elixir.ex
Created Dec 12, 2019
Markdium-Elixir process orchestration in Kubernetes with libcluster and swarm
View Markdium-elixir.ex
defmodule Yourapp.YourSupervisor do
use DynamicSupervisor
# See https://hexdocs.pm/elixir/Application.html
# for more information on OTP Applications
def start_link(state) do
DynamicSupervisor.start_link(__MODULE__, state, name: __MODULE__)
end
def init(_) do
@x-ji
x-ji / Markdium-elixir.ex
Created Dec 12, 2019
Markdium-Elixir process orchestration in Kubernetes with libcluster and swarm
View Markdium-elixir.ex
### In config/config.exs
config :libcluster,
topologies: [
your_app: [
strategy: Cluster.Strategy.Gossip,
config: [
port: 45892,
if_addr: "0.0.0.0",
multicast_addr: "230.1.1.251",
multicast_ttl: 1
@x-ji
x-ji / Markdium-Shell.sh
Created Dec 12, 2019
Markdium-Elixir process orchestration in Kubernetes with libcluster and swarm
View Markdium-Shell.sh
# In Dockerfile:
ENTRYPOINT ["/opt/app/docker-entrypoint-dev.sh"]
# In docker-entrypoint-dev.sh:
if [ -z ${NODE_IP+x} ]; then
export NODE_IP="$(hostname -i | cut -f1 -d' ')"
fi
elixir --name yourapp@${NODE_IP} --cookie "your_dev_erlang_cookie" -S mix phx.server
@x-ji
x-ji / Markdium-elixir.ex
Created Dec 12, 2019
Markdium-Elixir process orchestration in Kubernetes with libcluster and swarm
View Markdium-elixir.ex
### In mix.exs
defp deps do
[
...
{:libcluster, "~> 3.1"},
...
]
end
### In config/prod.exs
@x-ji
x-ji / Markdium-elixir.ex
Last active Dec 12, 2019
Markdium-Elixir process orchestration in Kubernetes with libcluster and swarm
View Markdium-elixir.ex
Swarm.register_name(
:unique_name_for_this_worker_process,
Yourapp.YourSupervisor,
:register,
[Yourapp.YourWorker]
)
@x-ji
x-ji / Markdium-YAML.yaml
Created Dec 12, 2019
Markdium-Elixir process orchestration in Kubernetes with libcluster and swarm
View Markdium-YAML.yaml
services:
your_app: &app
build:
context: ./app
ports:
- 4000:4000
# ...
your_app_2:
<<: *app
@x-ji
x-ji / Markdium-elixir.ex
Created Dec 12, 2019
Markdium-Elixir process orchestration in Kubernetes with libcluster and swarm
View Markdium-elixir.ex
### In config/config.exs
config :libcluster,
topologies: [
your_app: [
strategy: Cluster.Strategy.Gossip,
config: [
port: 45892,
if_addr: "0.0.0.0",
multicast_addr: "230.1.1.251",
multicast_ttl: 1
@x-ji
x-ji / Markdium-elixir.ex
Created Dec 12, 2019
Markdium-Elixir process orchestration in Kubernetes with libcluster and swarm
View Markdium-elixir.ex
### In mix.exs
defp deps do
[
...
{:libcluster, "~> 3.1"},
...
]
end
### In config/prod.exs
You can’t perform that action at this time.