Skip to content

Instantly share code, notes, and snippets.

@myobie
myobie / some_app_migration.ex
Created July 28, 2017 14:20
UTC Timestamps and Paranoid Delete for Ecto Elixir Projects
defmodule SomeApp.Migration do
defmacro __using__(_) do
quote do
import Ecto.Migration, except: [timestamps: 0, timestamps: 1]
@disable_ddl_transaction false
import unquote(__MODULE__)
@before_compile Ecto.Migration
end
end
# how to boot a new VM process
def spawn do
:net_kernel.start([:"primary@127.0.0.1"])
:erl_boot_server.start([])
allow_boot to_char_list("127.0.0.1")
Application.get_env(:highlander,
:spawn_nodes, [])
|> Enum.map(&Task.async(fn ->
{:ok, _node} = spawn_node(&1)
@myobie
myobie / user_and_list.ex
Created June 27, 2017 09:36
An example of how users and lists could know about each other
defmodule User do
use Highlander.Object, type: :user
defobject name: "", email: "", known_lists: []
end
defmodule List do
use Highlander.Object, type: :list
defobject title: "", todos: [], allowed_users: []
@myobie
myobie / list_and_todo.ex
Created June 27, 2017 09:33
An example of a shared list with some todos
defmodule List do
use Highlander.Object, type: :list
defobject title: "", todos: []
end
defmodule Todo do
defstruct title: "", completed: false, color: :blue
end
@myobie
myobie / user.ex
Created June 27, 2017 09:19
An example user module using highlander
defmodule User do
use Highlander.Object, type: :user
defobject name: "", email: ""
end
@myobie
myobie / todo_usage_example.ex
Created June 27, 2017 09:09
How to use the todo.ex highlander object
id = UUID.uuid4()
{:ok, todo} = Todo.get id
assert todo.completed == false
assert todo.title == ""
assert todo.color == :blue
todo = %{todo | title: "Hello"}
@myobie
myobie / todo.ex
Last active June 27, 2017 09:20
An example highlander object - todo
defmodule Todo do
use Highlander.Object, type: :todo
defobject title: "", completed: false, color: :blue
end
@myobie
myobie / supervisor_tree_example.ex
Created June 27, 2017 09:01
The example supervisor tree from the highlander project
opts = [host,
[stop_on_disconnect: true, name: :zk]]
children = [
worker(Zookeeper.Client, opts, []),
worker(Registry.Server, [], []),
worker(Registry.NodeCycleServer, [], []),
supervisor(Object.Supervisor, [], [])
]
@myobie
myobie / znode_init_example.ex
Created June 27, 2017 08:57
When creating a new key (modeled as a ZNode here) we use the distributed lock recipe to make sure we can know if we were first to create a key with this particular name
defmodule ZNode do
def init(name) do
{:ok, path} =
Zookeeper.Client.create(:zk,
prefix(name, UUID.uuid4(:hex)),
to_string(Node.self),
makepath: true,
create_mode: :ephemeral_sequential)
{:ok, %{path: path, name: name}}
end
@myobie
myobie / zk_create_node_example.ex
Created June 27, 2017 08:56
When creating a new key (node) in zookeeper, we check if we were the first to try and fail accordingly
defmodule ZK do
def create_znode(name) do
{:ok, pid} = ZNode.start_link(name)
if ZNode.first?(pid) do
{:ok, pid}
else
:ok = ZNode.delete(pid)
{:error, :already_exists}
end