aboutsummaryrefslogtreecommitdiff
path: root/shard/lib/manager.ex
diff options
context:
space:
mode:
authorAlex Auvolat <alex@adnab.me>2018-11-06 11:57:09 +0100
committerAlex Auvolat <alex@adnab.me>2018-11-06 11:57:09 +0100
commitc4f6cbab20b0b1d08755073d93365e5bd00dc755 (patch)
tree384dd05c59a810d503790a2af1b5c49e1aed5ccf /shard/lib/manager.ex
parent0e5b82f3348508eb7f4291a08722522a49edd752 (diff)
downloadshard-c4f6cbab20b0b1d08755073d93365e5bd00dc755.tar.gz
shard-c4f6cbab20b0b1d08755073d93365e5bd00dc755.zip
Shard deletion ; update README and TODO
Diffstat (limited to 'shard/lib/manager.ex')
-rw-r--r--shard/lib/manager.ex31
1 files changed, 30 insertions, 1 deletions
diff --git a/shard/lib/manager.ex b/shard/lib/manager.ex
index c178e2f..c3897a3 100644
--- a/shard/lib/manager.ex
+++ b/shard/lib/manager.ex
@@ -82,6 +82,27 @@ defmodule Shard.Manager do
{:reply, pid, state}
end
+ def handle_call({:delete, shard_id}, _from, state) do
+ case :dets.lookup(@shard_db, shard_id) do
+ [] ->
+ {:reply, {:error, :not_found}, state}
+ [{^shard_id, manifest, {:cached, _}, _}] ->
+ pid = case :ets.lookup(:shard_procs, {shard_id, nil}) do
+ [] ->
+ {:ok, pid} = apply(Shard.Manifest.module(manifest), :start_link, [manifest])
+ pid
+ [{{^shard_id, nil}, pid}] ->
+ :ets.delete(:shard_procs, {shard_id, nil})
+ pid
+ end
+ GenServer.call(pid, :delete_shard)
+ :dets.delete(@shard_db, shard_id)
+ {:reply, :ok, state}
+ [{^shard_id, _, _, _}] ->
+ {:reply, {:error, :pinned}, state}
+ end
+ end
+
def handle_cast({:dispatch_to, shard_id, path, pid}, state) do
:ets.insert(:shard_procs, { {shard_id, path}, pid })
state = Map.put(state, pid, {shard_id, path})
@@ -105,6 +126,7 @@ defmodule Shard.Manager do
case :dets.lookup(@shard_db, shard_id) do
[{^shard_id, manifest, why_have_it, _old_state}] ->
:dets.insert(@shard_db, {shard_id, manifest, why_have_it, shst})
+ :dets.sync(@shard_db)
end
{:noreply, state}
end
@@ -169,7 +191,7 @@ defmodule Shard.Manager do
for [id] <- shards do
case :ets.lookup(:shard_procs, {id, nil}) do
[{{^id, nil}, pid}] ->
- GenServer.cast(pid, :delete_shard)
+ GenServer.call(pid, :delete_shard)
_ -> nil
end
:dets.delete(@shard_db, id)
@@ -366,6 +388,13 @@ defmodule Shard.Manager do
end
@doc"""
+ Delete a shard
+ """
+ def delete(shard_id) do
+ GenServer.call(__MODULE__, {:delete, shard_id})
+ end
+
+ @doc"""
Return the list of all shards. Returns a list of tuples:
{id, manifest, why_have_it}