-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
11 changed files
with
437 additions
and
27 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
# Used by "mix format" | ||
[ | ||
inputs: ["{mix,.formatter}.exs", "{config,lib,test}/**/*.{ex,exs}"] | ||
] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,9 +1,24 @@ | ||
/_build | ||
/cover | ||
/deps | ||
/doc | ||
# The directory Mix will write compiled artifacts to. | ||
/_build/ | ||
|
||
# If you run "mix test --cover", coverage assets end up here. | ||
/cover/ | ||
|
||
# The directory Mix downloads your dependencies sources to. | ||
/deps/ | ||
|
||
# Where 3rd-party dependencies like ExDoc output generated docs. | ||
/doc/ | ||
|
||
# Ignore .fetch files in case you like to edit your project deps locally. | ||
/.fetch | ||
|
||
# If the VM crashes, it generates a dump, let's ignore it too. | ||
erl_crash.dump | ||
|
||
# Also ignore archive artifacts (built via "mix archive.build"). | ||
*.ez | ||
*.beam | ||
/config/*.secret.exs | ||
|
||
# Ignore package tarball (built via "mix hex.build"). | ||
deferrable-*.tar | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
The MIT License (MIT) | ||
|
||
Copyright 2019 Peek Travel, Inc. | ||
|
||
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: | ||
|
||
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. | ||
|
||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
# Deferrable | ||
|
||
**This is pre-release and will go through a couple more revisions before considered stable.** | ||
|
||
Library for deferring execution of code until the completion of a transaction block. | ||
|
||
Similar in concept to database transactions with support for nesting and partial-rollbacks. | ||
|
||
## Installation | ||
|
||
If [available in Hex](https://hex.pm/docs/publish), the package can be installed | ||
by adding `deferrable` to your list of dependencies in `mix.exs`: | ||
|
||
```elixir | ||
def deps do | ||
[ | ||
{:deferrable, "~> 0.0.1"} | ||
] | ||
end | ||
``` | ||
|
||
Documentation can be generated with [ExDoc](https://github.com/elixir-lang/ex_doc) | ||
and published on [HexDocs](https://hexdocs.pm). Once published, the docs can | ||
be found at [https://hexdocs.pm/deferrable](https://hexdocs.pm/deferrable). | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
# This file is responsible for configuring your application | ||
# and its dependencies with the aid of the Mix.Config module. | ||
use Mix.Config | ||
|
||
# This configuration is loaded before any dependency and is restricted | ||
# to this project. If another project depends on this project, this | ||
# file won't be loaded nor affect the parent project. For this reason, | ||
# if you want to provide default values for your application for | ||
# 3rd-party users, it should be done in your "mix.exs" file. | ||
|
||
# You can configure your application as: | ||
# | ||
# config :deferrable, key: :value | ||
# | ||
# and access this configuration in your application as: | ||
# | ||
# Application.get_env(:deferrable, :key) | ||
# | ||
# You can also configure a 3rd-party app: | ||
# | ||
# config :logger, level: :info | ||
# | ||
|
||
# It is also possible to import configuration files, relative to this | ||
# directory. For example, you can emulate configuration per environment | ||
# by uncommenting the line below and defining dev.exs, test.exs and such. | ||
# Configuration from the imported file will override the ones defined | ||
# here (which is why it is important to import them last). | ||
# | ||
# import_config "#{Mix.env()}.exs" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,139 @@ | ||
defmodule Peek.Deferrable do | ||
@moduledoc """ | ||
Allows deferring of function calls until a transaction succeeds. | ||
## Example | ||
Deferrable.transaction(fn -> | ||
Deferrable.defer(fn -> "do something later" end) | ||
{:ok, "result"} | ||
end) | ||
""" | ||
|
||
@stack_key :deferrable_stack | ||
|
||
def defer(fun) do | ||
case stack_ref() do | ||
:no_stack -> fun.() | ||
ref -> send(self(), {:deferred, ref, fun}) | ||
end | ||
end | ||
|
||
def transaction(fun) do | ||
stack = push_stack(make_ref()) | ||
|
||
try do | ||
result = | ||
case {stack, fun.()} do | ||
{[_top], {:ok, result}} -> | ||
process_deferred() | ||
{:ok, result} | ||
|
||
{_stack, {:ok, result}} -> | ||
{:ok, result} | ||
|
||
{_stack, {:error, reason}} -> | ||
clear_deferred(stack) | ||
{:error, reason} | ||
end | ||
|
||
result | ||
rescue | ||
err -> | ||
clear_deferred(stack) | ||
|
||
reraise(err, __STACKTRACE__) | ||
after | ||
pop_stack() | ||
end | ||
end | ||
|
||
def process_deferred, do: do_process_deferred([]) | ||
|
||
defp do_process_deferred(results) do | ||
receive do | ||
{:deferred, _ref, fun} -> | ||
try do | ||
do_process_deferred([fun.() | results]) | ||
rescue | ||
# FIXME: Don't clear all later deferred functions when one fails. e.g. losing availability messages | ||
# if another message fails to publish. Running the deferred functions inside tasks could be a way to solve | ||
# this, so each one can fail on its own, raise, and have its own stack-trace. | ||
err -> | ||
clear_deferred() | ||
reraise(err, __STACKTRACE__) | ||
end | ||
after | ||
0 -> Enum.reverse(results) | ||
end | ||
end | ||
|
||
def clear_deferred do | ||
case stack() do | ||
:no_stack -> :ok | ||
stack -> clear_deferred(stack) | ||
end | ||
end | ||
|
||
defp clear_deferred([]), do: :ok | ||
|
||
defp clear_deferred([ref | _rest] = stack) do | ||
child_tree = get_in(tree(), Enum.reverse(stack)) | ||
child_refs = all_keys(child_tree) | ||
do_clear_deferred([ref | child_refs]) | ||
end | ||
|
||
defp do_clear_deferred([]), do: :ok | ||
|
||
defp do_clear_deferred([ref | rest] = refs) do | ||
receive do | ||
{:deferred, ^ref, _fun} -> do_clear_deferred(refs) | ||
after | ||
0 -> do_clear_deferred(rest) | ||
end | ||
end | ||
|
||
defp stack_ref do | ||
case Process.get(@stack_key, :no_stack) do | ||
{[ref | _rest], _popped} -> ref | ||
{[], _popped} -> :no_stack | ||
:no_stack -> :no_stack | ||
end | ||
end | ||
|
||
defp stack do | ||
with {stack, _tree} <- Process.get(@stack_key, :no_stack) do | ||
stack | ||
end | ||
end | ||
|
||
defp tree do | ||
with {_stack, tree} <- Process.get(@stack_key, :no_stack) do | ||
tree | ||
end | ||
end | ||
|
||
defp push_stack(ref) do | ||
{stack, tree} = Process.get(@stack_key, {[], %{}}) | ||
stack = [ref | stack] | ||
tree = put_in(tree, Enum.reverse(stack), %{}) | ||
Process.put(@stack_key, {stack, tree}) | ||
stack | ||
end | ||
|
||
defp pop_stack do | ||
with {[_ref | rest], tree} <- Process.get(@stack_key, :no_stack) do | ||
Process.put(@stack_key, {rest, tree}) | ||
:ok | ||
else | ||
{[], _} -> {:error, :top_of_stack} | ||
end | ||
end | ||
|
||
defp all_keys(map) do | ||
Enum.flat_map(map, fn {key, child_map} -> | ||
[key | all_keys(child_map)] | ||
end) | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
defmodule Deferrable.MixProject do | ||
use Mix.Project | ||
|
||
@version "0.0.1" | ||
@source_url "https://github.com/peek-travel/deferrable" | ||
|
||
def project do | ||
[ | ||
app: :deferrable, | ||
version: @version, | ||
elixir: "~> 1.7", | ||
elixirc_paths: elixirc_paths(Mix.env()), | ||
start_permanent: Mix.env() == :prod, | ||
deps: deps(), | ||
docs: docs(), | ||
description: description(), | ||
package: package() | ||
] | ||
end | ||
|
||
# Run "mix help compile.app" to learn about applications. | ||
def application do | ||
[ | ||
extra_applications: [] | ||
] | ||
end | ||
|
||
defp elixirc_paths(:test), do: ["lib", "test/support"] | ||
defp elixirc_paths(_), do: ["lib"] | ||
|
||
defp docs do | ||
[ | ||
main: "Deferrable", | ||
source_ref: @version, | ||
source_url: @source_url, | ||
extras: ["README.md", "LICENSE.md"] | ||
] | ||
end | ||
|
||
defp description do | ||
""" | ||
Library for deferring execution of code until the completion of a transaction block. | ||
""" | ||
end | ||
|
||
defp package do | ||
[ | ||
files: ["lib", ".formatter.exs", "mix.exs", "README.md", "LICENSE.md"], | ||
maintainers: ["Daniel Hedlund <[email protected]>", "Chris Dosé <[email protected]>"], | ||
licenses: ["MIT"], | ||
links: %{ | ||
"GitHub" => @source_url, | ||
"Readme" => "#{@source_url}/blob/#{@version}/README.md" | ||
} | ||
] | ||
end | ||
|
||
# Run "mix help deps" to learn about dependencies. | ||
defp deps do | ||
[ | ||
{:ex_doc, "~> 0.20", only: :dev, runtime: false} | ||
] | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
%{ | ||
"earmark": {:hex, :earmark, "1.3.2", "b840562ea3d67795ffbb5bd88940b1bed0ed9fa32834915125ea7d02e35888a5", [:mix], [], "hexpm"}, | ||
"ex_doc": {:hex, :ex_doc, "0.20.2", "1bd0dfb0304bade58beb77f20f21ee3558cc3c753743ae0ddbb0fd7ba2912331", [:mix], [{:earmark, "~> 1.3", [hex: :earmark, repo: "hexpm", optional: false]}, {:makeup_elixir, "~> 0.10", [hex: :makeup_elixir, repo: "hexpm", optional: false]}], "hexpm"}, | ||
"makeup": {:hex, :makeup, "0.8.0", "9cf32aea71c7fe0a4b2e9246c2c4978f9070257e5c9ce6d4a28ec450a839b55f", [:mix], [{:nimble_parsec, "~> 0.5.0", [hex: :nimble_parsec, repo: "hexpm", optional: false]}], "hexpm"}, | ||
"makeup_elixir": {:hex, :makeup_elixir, "0.13.0", "be7a477997dcac2e48a9d695ec730b2d22418292675c75aa2d34ba0909dcdeda", [:mix], [{:makeup, "~> 0.8", [hex: :makeup, repo: "hexpm", optional: false]}], "hexpm"}, | ||
"nimble_parsec": {:hex, :nimble_parsec, "0.5.0", "90e2eca3d0266e5c53f8fbe0079694740b9c91b6747f2b7e3c5d21966bba8300", [:mix], [], "hexpm"}, | ||
} |
Oops, something went wrong.