Skip to content

Instantly share code, notes, and snippets.

@rlb3
rlb3 / dns.ex
Last active January 15, 2023 05:08
"google.com"
|> String.to_charlist()
|> :inet_res.lookup(:in, :a)
|> Enum.reduce([], fn ip, acc ->
[
ip
|> Tuple.to_list()
|> Enum.join(".")
| acc
]
@rlb3
rlb3 / httpd.ex
Created December 29, 2022 21:50
Erlang web server
:inets.start(:httpd, [port: 0, server_name: 'monkl', server_root: '.', document_root: './docs', bind_address: {127,0,0,1}])
@rlb3
rlb3 / manager.md
Last active December 22, 2022 03:07

Manager Architecture

Mix.install([
  {:kino, "~> 0.7.0"}
])

Worker

@rlb3
rlb3 / start_peer.exs
Created December 18, 2022 06:40 — forked from ityonemo/start_peer.exs
starts a BEAM peer node
:net_kernel.start([:"main@127.0.0.1"])
:erlang.set_cookie(:cook)
{:ok, peer, peername} =
:peer.start(%{connection: 0, name: :peer, host: ~C'127.0.0.1'})
:peer.call(peer, :erlang, :set_cookie, [:cook])
Node.connect(peername)
# add code paths
:rpc.call(peername, :code, :add_paths, [:code.get_path()])
@rlb3
rlb3 / Engineering.md
Created September 12, 2022 20:03 — forked from MithunArunan/Engineering.md
Building a great product

.

@rlb3
rlb3 / console_helpers.ex
Last active August 26, 2022 01:36
In iex redirect stdio to file.
defmodule Console.Helpers do
defmacro capture_io(filename, do: block) do
quote do
filename = unquote(filename)
current_leader = Process.group_leader()
{:ok, fh} = File.open(filename, [:write])
Process.group_leader(self(), fh)
unquote(block)
Process.group_leader(self(), current_leader)
end
@rlb3
rlb3 / slave.sh
Last active July 27, 2022 20:51
Elixir slave nodes
iex --sname a
{:ok, hostname} = :inet.gethostname()
:slave.start(hostname, :b)
:rpc.block_call(hostname, :code, :add_paths, [:code.get_path()])
:rpc.block_call(hostname, :application, :start, [:compiler])
:rpc.block_call(hostname, :application, :start, [:elixir])
:rpc.block_call(hostname, :application, :start, [:iex])
@rlb3
rlb3 / drain_stop.ex
Created June 27, 2022 04:31 — forked from aaronjensen/drain_stop.ex
Phoenix Drain Stop
# ATTENTION: This is now supported in plug_cowboy as of 2.1.0:
# https://hexdocs.pm/plug_cowboy/Plug.Cowboy.Drainer.html
defmodule DrainStop do
@moduledoc """
DrainStop Attempts to gracefully shutdown an endpoint when a normal shutdown
occurs. It first shuts down the acceptor, ensuring that no new requests can be
made. It then waits for all pending requests to complete. If the timeout
expires before this happens, it stops waiting, allowing the supervision tree
to continue its shutdown order.
@rlb3
rlb3 / Dockerfile
Last active June 26, 2022 03:40
Elixir Dockerfile
ARG MIX_ENV="prod"
# build stage
FROM hexpm/elixir:1.12.3-erlang-24.1.2-alpine-3.14.2 AS build
# install build dependencies
RUN apk add --no-cache build-base git python3 curl
# sets work dir
WORKDIR /app
@rlb3
rlb3 / run.sh
Created June 24, 2022 17:25 — forked from bitwalker/run.sh
Handling of UNIX-signals in Erlang/Elixir is not supported, this script provides start-stop management with handling TERM signal for Docker installation.
#!/usr/bin/env bash
set -x
term_handler() {
echo "Stopping the server process with PID $PID"
erl -noshell -name "term@127.0.0.1" -eval "rpc:call('app@127.0.0.1', init, stop, [])" -s init stop
echo "Stopped"
}
trap 'term_handler' TERM INT