-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathrepeat_try.jl
203 lines (173 loc) · 5.74 KB
/
repeat_try.jl
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
#==============================================================================#
# repeat_try.jl
#
# Copyright Sam O'Connor 2014 - All rights reserved
#==============================================================================#
#------------------------------------------------------------------------------#
#
# @repeat try... re-writes "try_expr" to try again at most "max" times and to
# automatically rethrow() at end of "catch" block (unless exception has been
# set to nothing).
#
# @ignore if... re-writes "if..." to ignore exceptions thrown by the if
# "condition" and to set "exception" = nothing if the "condition" is true
# (to prevent automatic rethrow).
#
# @retry if... re-writes "if..." to ignore exceptions thrown by the if
# "condition" and to try again if the "condition" is true.
#
# @delay_retry if... adds exponentially increasing delay with random jitter...
#
#
# e.g.
#
# @repeat 4 try
#
# return s3(aws, "GET", bucket, path)
#
# catch e
# @delay_retry if e.code in ["NoSuchBucket", "NoSuchKey"] end
# end
#
#
# e.g.
#
# @repeat 4 try
#
# return http_attempt(request)
#
# catch e
#
# @delay_retry if typeof(e) == UVError end
#
# @delay_retry if http_status(e) < 200 && http_status(e) >= 500 end
#
# @retry if http_status(e) in [301, 302, 307]
# request.uri = URI(headers(e)["Location"])
# end
#
# end
#
#
# e.g.
#
# @repeat 4 try
#
# r = sqs(aws, Action = "CreateQueue", QueueName = name)
# return = XML(r)[:QueueUrl]
#
# catch e
#
# @retry if e.code == "QueueAlreadyExists"
# sqs_delete_queue(aws, name)
# end
#
# @retry if e.code == "AWS.SimpleQueueService.QueueDeletedRecently"
# println("""Waiting 1 minute to re-create Queue "$name"...""")
# sleep(60)
# end
# end
#
#
#
#------------------------------------------------------------------------------#
# Check that "expr" is "try ... catch err ... [finalise ...] end"
function check_try_catch(expr::Expr, require_exception_variable::Bool)
if expr.head !== :try
throw(ArgumentError("Expected a `try`/`catch` expression argument"))
end
if require_exception_variable
expr.args[2] isa Symbol || throw(ArgumentError("Expected exception variable name"))
else
if !isa(expr.args[2], Symbol)
@assert expr.args[2] == false
expr.args[2] = :err
end
end
return (expr.args...,)
end
# Check that "expr" is "@macrocall if ... end".
function check_macro_if(expr::Expr)
if !(expr.head == :macrocall && length(expr.args) == 3)
throw(ArgumentError("Expected macro call with a single expression argument"))
end
(macroname::Symbol, lineinfo::LineNumberNode, ifexpr::Expr) = expr.args
if ifexpr.head !== :if
throw(ArgumentError("$macroname: expecting an `if` expression"))
end
if !(length(ifexpr.args) == 2 && ifexpr.args[2].head == :block)
throw(ArgumentError("$macroname: `else` expression is not allowed"))
end
return ifexpr
end
function esc_args!(expr::Expr)
for (i, arg) in enumerate(expr.args)
if isa(arg, Symbol) || !isa(arg, LineNumberNode)
expr.args[i] = esc(arg)
end
end
end
macro repeat(max, try_expr::Expr)
# Extract exception variable and catch block from "try" expression...
(try_block, exception, catch_block) = check_try_catch(try_expr, false)
# Escape everything except catch block...
esc_args!(try_expr)
try_expr.args[3] = catch_block
max = esc(max)
exception = esc(exception)
for (i, expr) in enumerate(catch_block.args)
# Look for "@ignore/@retry if..." expressions in catch block...
if (typeof(expr) == Expr
&& expr.head == :macrocall
&& expr.args[1] in [Symbol("@retry"),
Symbol("@delay_retry"),
Symbol("@ignore")])
handler = string(expr.args[1])
if_expr = check_macro_if(expr)
(condition, action) = if_expr.args
if_expr.args[1] = esc(condition)
esc_args!(action)
# Clear exception variable at end of "@ignore if..." block...
if handler == "@ignore"
push!(action.args, :(ignore = true))
end
# Loop to try again at end of "@retry if..." block...
if handler == "@retry"
push!(action.args, :(if i < $max continue end))
end
# Add exponentially increasing delay with random jitter,
# and loop to try again at end of "@delay_retry if..." block...
if handler == "@delay_retry"
push!(action.args, quote
if i < $max
sleep(delay * (0.8 + (0.4 * rand())))
delay *= 10
continue
end
end)
end
# Replace @ignore/@retry macro call with modified if expression...
catch_block.args[i] = if_expr
elseif !isa(expr, LineNumberNode)
catch_block.args[i] = esc(expr)
end
end
# Check for nothing exception at start of catch block...
insert!(catch_block.args, 2, :($exception == nothing && rethrow()))
pushfirst!(catch_block.args, :(ignore = false))
# Rethrow at end of catch block...
push!(catch_block.args, :(ignore || rethrow($exception)))
# Build retry expression...
quote
local delay = 0.05
local result = false
for i in 1:$max
result = $try_expr
break
end
result
end
end
#==============================================================================#
# End of file.
#==============================================================================#