|
| 1 | +defmodule Retry.Annotation do |
| 2 | + defmacro __using__(_opts) do |
| 3 | + quote do |
| 4 | + import Retry.DelayStreams |
| 5 | + import Stream, only: [take: 2] |
| 6 | + require Retry |
| 7 | + require Logger |
| 8 | + |
| 9 | + Module.register_attribute(__MODULE__, :retry_funs, accumulate: true) |
| 10 | + |
| 11 | + @on_definition {Retry.Annotation, :on_def} |
| 12 | + @before_compile {Retry.Annotation, :before_compile} |
| 13 | + end |
| 14 | + end |
| 15 | + |
| 16 | + def on_def(env, kind, name, args, guards, _body) do |
| 17 | + retry_opts = Module.get_attribute(env.module, :retry, :no_retry) |
| 18 | + |
| 19 | + unless retry_opts == :no_retry do |
| 20 | + Module.put_attribute(env.module, :retry_funs, %{ |
| 21 | + kind: kind, |
| 22 | + name: name, |
| 23 | + args: Enum.map(args, &de_underscore_name/1), |
| 24 | + guards: guards, |
| 25 | + retry_opts: Keyword.update(retry_opts, :with, [], &Enum.to_list/1) |
| 26 | + }) |
| 27 | + |
| 28 | + Module.delete_attribute(env.module, :retry) |
| 29 | + end |
| 30 | + end |
| 31 | + |
| 32 | + defmacro before_compile(env) do |
| 33 | + retry_funs = Module.get_attribute(env.module, :retry_funs, []) |
| 34 | + Module.delete_attribute(env.module, :retry_funs) |
| 35 | + |
| 36 | + override_list = |
| 37 | + Enum.map(retry_funs, &gen_override_list/1) |
| 38 | + |> List.flatten() |
| 39 | + |
| 40 | + overrides = |
| 41 | + quote location: :keep do |
| 42 | + defoverridable unquote(override_list) |
| 43 | + end |
| 44 | + |
| 45 | + functions = |
| 46 | + Enum.map(retry_funs, fn fun -> |
| 47 | + body = gen_body(fun) |
| 48 | + gen_function(fun, body) |
| 49 | + end) |
| 50 | + |
| 51 | + [overrides | functions] |
| 52 | + end |
| 53 | + |
| 54 | + defp gen_override_list(%{name: name, args: args}) do |
| 55 | + no_default_args_length = |
| 56 | + Enum.reduce(args, 0, fn |
| 57 | + {:\\, _, _}, acc -> acc |
| 58 | + _, acc -> acc + 1 |
| 59 | + end) |
| 60 | + |
| 61 | + Enum.map(no_default_args_length..length(args), fn i -> {name, i} end) |
| 62 | + end |
| 63 | + |
| 64 | + defp gen_function(%{kind: :def, guards: [], name: name, args: args}, body) do |
| 65 | + quote location: :keep do |
| 66 | + def unquote(name)(unquote_splicing(args)) do |
| 67 | + unquote(body) |
| 68 | + end |
| 69 | + end |
| 70 | + end |
| 71 | + |
| 72 | + defp gen_function(%{kind: :def, guards: guards, name: name, args: args}, body) do |
| 73 | + quote location: :keep do |
| 74 | + def unquote(name)(unquote_splicing(args)) when unquote_splicing(guards) do |
| 75 | + unquote(body) |
| 76 | + end |
| 77 | + end |
| 78 | + end |
| 79 | + |
| 80 | + defp gen_function(%{kind: :defp, guards: [], name: name, args: args}, body) do |
| 81 | + quote location: :keep do |
| 82 | + defp unquote(name)(unquote_splicing(args)) do |
| 83 | + unquote(body) |
| 84 | + end |
| 85 | + end |
| 86 | + end |
| 87 | + |
| 88 | + defp gen_function(%{kind: :defp, guards: guards, name: name, args: args}, body) do |
| 89 | + quote location: :keep do |
| 90 | + defp unquote(name)(unquote_splicing(args)) when unquote_splicing(guards) do |
| 91 | + unquote(body) |
| 92 | + end |
| 93 | + end |
| 94 | + end |
| 95 | + |
| 96 | + defp gen_body(fun) do |
| 97 | + args = |
| 98 | + Enum.map(fun.args, fn |
| 99 | + {:\\, _, [arg_name | _]} -> arg_name |
| 100 | + arg -> arg |
| 101 | + end) |
| 102 | + |
| 103 | + quote location: :keep do |
| 104 | + Retry.retry_while unquote(fun.retry_opts) do |
| 105 | + case super(unquote_splicing(args)) do |
| 106 | + {:error, reason} = error -> |
| 107 | + Logger.info(fn -> |
| 108 | + "#{__MODULE__}: Retrying function #{unquote(fun.name)}: #{inspect(reason)}" |
| 109 | + end) |
| 110 | + |
| 111 | + {:cont, error} |
| 112 | + |
| 113 | + result -> |
| 114 | + {:halt, result} |
| 115 | + end |
| 116 | + end |
| 117 | + end |
| 118 | + end |
| 119 | + |
| 120 | + defp de_underscore_name({:\\, context, [{name, name_context, name_args} | t]} = arg) do |
| 121 | + case to_string(name) do |
| 122 | + "_" <> real_name -> {:\\, context, [{String.to_atom(real_name), name_context, name_args} | t]} |
| 123 | + _ -> arg |
| 124 | + end |
| 125 | + end |
| 126 | + |
| 127 | + defp de_underscore_name({name, context, args} = arg) do |
| 128 | + case to_string(name) do |
| 129 | + "_" <> real_name -> {String.to_atom(real_name), context, args} |
| 130 | + _ -> arg |
| 131 | + end |
| 132 | + end |
| 133 | +end |
0 commit comments