Skip to content

Commit

Permalink
refactor: replace reporterror with logerror
Browse files Browse the repository at this point in the history
  • Loading branch information
mchitre committed Feb 5, 2024
1 parent 55c290e commit 9af5a95
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 32 deletions.
58 changes: 34 additions & 24 deletions src/container.jl
Original file line number Diff line number Diff line change
Expand Up @@ -701,22 +701,32 @@ _deliver(c::SlaveContainer, msg::Message) = _deliver(c, msg, true)

### stacktrace pretty printing & auto-reconnection

function reporterror(src, ex)
fname = basename(@__FILE__)
bt = String[]
for s stacktrace(catch_backtrace())
push!(bt, " $s")
basename(string(s.file)) == fname && s.func == :run && break
end
bts = join(bt, '\n')
if src === nothing
@error "$(ex)\n Stack trace:\n$(bts)"
else
@error "[$(src)] $(ex)\n Stack trace:\n$(bts)"
"""
logerror(f::Function)
logerror(f::Function, src)
Run function `f()` and log any errors that occur.
"""
function logerror(f::Function, src=nothing)
try
f()
catch ex
logerror(ex, src)
end
end

reporterror(ex) = reporterror(nothing, ex)
"""
logerror(err::Exception)
logerror(err::Exception, src)
Log error `err` with a simple stack trace.
"""
function logerror(ex::Exception, src=nothing)
io = IOBuffer()
src === nothing || print(io, "[$src] ")
Base.showerror(IOContext(io, :limit => true), ex, Base.catch_backtrace())
@error String(take!(io))
end

reconnect(c::StandaloneContainer, ex) = false
function reconnect(c::SlaveContainer, ex)
Expand Down Expand Up @@ -1443,7 +1453,7 @@ function action(b::OneShotBehavior)
b.action === nothing || _mutex_call(b.action, b.agent, b)
b.onend === nothing || _mutex_call(b.onend, b.agent, b)
catch ex
reconnect(container(b.agent), ex) || reporterror(b.agent, ex)
reconnect(container(b.agent), ex) || logerror(ex, b.agent)
end
b.done = true
delete!(b.agent._behaviors, b)
Expand Down Expand Up @@ -1496,7 +1506,7 @@ function action(b::CyclicBehavior)
try
b.action === nothing || _mutex_call(b.action, b.agent, b)
catch ex
reconnect(container(b.agent), ex) || reporterror(b.agent, ex)
reconnect(container(b.agent), ex) || logerror(ex, b.agent)
end
yield()
else
Expand All @@ -1505,7 +1515,7 @@ function action(b::CyclicBehavior)
end
b.onend === nothing || _mutex_call(b.onend, b.agent, b)
catch ex
reconnect(container(b.agent), ex) || reporterror(b.agent, ex)
reconnect(container(b.agent), ex) || logerror(ex, b.agent)
end
b.done = true
delete!(b.agent._behaviors, b)
Expand Down Expand Up @@ -1594,7 +1604,7 @@ function action(b::WakerBehavior)
end
b.onend === nothing || _mutex_call(b.onend, b.agent, b)
catch ex
reconnect(container(b.agent), ex) || reporterror(b.agent, ex)
reconnect(container(b.agent), ex) || logerror(ex, b.agent)
end
b.done = true
delete!(b.agent._behaviors, b)
Expand Down Expand Up @@ -1666,12 +1676,12 @@ function action(b::TickerBehavior)
try
b.done || b.action === nothing || _mutex_call(b.action, b.agent, b)
catch ex
reconnect(container(b.agent), ex) || reporterror(b.agent, ex)
reconnect(container(b.agent), ex) || logerror(ex, b.agent)
end
end
b.onend === nothing || _mutex_call(b.onend, b.agent, b)
catch ex
reconnect(container(b.agent), ex) || reporterror(b.agent, ex)
reconnect(container(b.agent), ex) || logerror(ex, b.agent)
end
b.done = true
delete!(b.agent._behaviors, b)
Expand Down Expand Up @@ -1733,12 +1743,12 @@ function action(b::PoissonBehavior)
try
b.done || b.action === nothing || _mutex_call(b.action, b.agent, b)
catch ex
reconnect(container(b.agent), ex) || reporterror(b.agent, ex)
reconnect(container(b.agent), ex) || logerror(ex, b.agent)
end
end
b.onend === nothing || _mutex_call(b.onend, b.agent, b)
catch ex
reconnect(container(b.agent), ex) || reporterror(b.agent, ex)
reconnect(container(b.agent), ex) || logerror(ex, b.agent)
end
b.done = true
delete!(b.agent._behaviors, b)
Expand Down Expand Up @@ -1814,12 +1824,12 @@ function action(b::MessageBehavior)
msg = take!(ch)
msg === nothing || b.action === nothing || _mutex_call(b.action, b.agent, b, msg)
catch ex
reconnect(container(b.agent), ex) || reporterror(b.agent, ex)
reconnect(container(b.agent), ex) || logerror(ex, b.agent)
end
end
b.onend === nothing || _mutex_call(b.onend, b.agent, b)
catch ex
reconnect(container(b.agent), ex) || reporterror(b.agent, ex)
reconnect(container(b.agent), ex) || logerror(ex, b.agent)
finally
_dont_listen(b.agent, ch)
close(ch)
Expand Down Expand Up @@ -2056,7 +2066,7 @@ function _paramreq_action(a::Agent, b::MessageBehavior, msg::ParameterReq)
end
end
catch ex
reconnect(container(a), ex) || reporterror(a, ex)
reconnect(container(a), ex) || logerror(ex, a)
end
end
rmsg = ParameterRsp(performative=Performative.INFORM, inReplyTo=msg.messageID, recipient=msg.sender, readonly=ro, index=ndx)
Expand Down
8 changes: 2 additions & 6 deletions src/coroutine_behavior.jl
Original file line number Diff line number Diff line change
Expand Up @@ -52,15 +52,13 @@ end
function action(b::CoroutineBehavior)
b.control_task = current_task()
b.action_task = Task() do
try
logerror(b.agent) do
b.action(b.agent, b)
catch e
reporterror(b.agent, e)
end
b.done = true
yieldto(b.control_task)
end
try
logerror(b.agent) do
while !b.done
if !isnothing(b.block)
lock(() -> wait(b.block), b.block)
Expand All @@ -69,8 +67,6 @@ function action(b::CoroutineBehavior)
yieldto(b.action_task)
end
end
catch ex
reporterror(b.agent, ex)
end
b.done = true
b.control_task = nothing
Expand Down
4 changes: 2 additions & 2 deletions src/fsm.jl
Original file line number Diff line number Diff line change
Expand Up @@ -214,15 +214,15 @@ function action(b::FSMBehavior)
_mutex_call(onenter, b.agent, b, b.state)
end
catch ex
reconnect(container(b.agent), ex) || reporterror(b.agent, ex)
reconnect(container(b.agent), ex) || logerror(ex, b.agent)
end
yield()
else
lock(() -> wait(b.block), b.block)
end
end
catch ex
reconnect(container(b.agent), ex) || reporterror(b.agent, ex)
reconnect(container(b.agent), ex) || logerror(ex, b.agent)
end
delete!(b.agent._behaviors, b)
b.agent = nothing
Expand Down

0 comments on commit 9af5a95

Please sign in to comment.