Skip to content

Instantly share code, notes, and snippets.

View x-ji's full-sized avatar

Xiang Ji x-ji

View GitHub Profile
@x-ji
x-ji / Markdium-elixir.ex
Created December 12, 2019 19:39
Markdium-Elixir process orchestration in Kubernetes with libcluster and swarm
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 December 12, 2019 19:39
Markdium-Elixir process orchestration in Kubernetes with libcluster and swarm
# 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 December 12, 2019 19:39
Markdium-Elixir process orchestration in Kubernetes with libcluster and swarm
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 December 12, 2019 19:39
Markdium-Elixir process orchestration in Kubernetes with libcluster and swarm
### 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 December 12, 2019 19:39
Markdium-Elixir process orchestration in Kubernetes with libcluster and swarm
# 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 December 12, 2019 19:39
Markdium-Elixir process orchestration in Kubernetes with libcluster and swarm
### In mix.exs
defp deps do
[
...
{:libcluster, "~> 3.1"},
...
]
end
### In config/prod.exs
@x-ji
x-ji / Markdium-elixir.ex
Last active December 12, 2019 20:31
Markdium-Elixir process orchestration in Kubernetes with libcluster and swarm
Swarm.register_name(
:unique_name_for_this_worker_process,
Yourapp.YourSupervisor,
:register,
[Yourapp.YourWorker]
)
@x-ji
x-ji / Markdium-YAML.yaml
Created December 12, 2019 19:39
Markdium-Elixir process orchestration in Kubernetes with libcluster and swarm
services:
your_app: &app
build:
context: ./app
ports:
- 4000:4000
# ...
your_app_2:
<<: *app
@x-ji
x-ji / Markdium-elixir.ex
Created December 12, 2019 19:14
Markdium-Elixir process orchestration in Kubernetes with libcluster and swarm
### 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 December 12, 2019 19:14
Markdium-Elixir process orchestration in Kubernetes with libcluster and swarm
### In mix.exs
defp deps do
[
...
{:libcluster, "~> 3.1"},
...
]
end
### In config/prod.exs