Skip to content

Commit

Permalink
Handle device save down
Browse files Browse the repository at this point in the history
  • Loading branch information
macpie committed Oct 10, 2023
1 parent 3835896 commit a10d73e
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 22 deletions.
49 changes: 27 additions & 22 deletions src/device/router_device_cache.erl
Original file line number Diff line number Diff line change
Expand Up @@ -72,28 +72,33 @@ all_devaddrs_tables() ->
save(Device) ->
DeviceID = router_device:id(Device),
true = ets:insert(?ETS, {DeviceID, Device}),
_ = erlang:spawn_monitor(fun() ->
AddrsEtsMap = maps:from_list(all_devaddrs_tables()),
CurrentDevaddrs = router_device:devaddrs(Device),

%% Build a list of all DevAddrs we can know about.
AllAddrs = lists:usort(maps:keys(AddrsEtsMap) ++ CurrentDevaddrs),

lists:foreach(
fun(Addr) ->
EtsRef =
case maps:get(Addr, AddrsEtsMap, undefined) of
undefined -> make_devaddr_table(Addr);
Ref -> Ref
end,
case lists:member(Addr, CurrentDevaddrs) of
true -> ets:insert(EtsRef, Device);
false -> ets:delete(EtsRef, DeviceID)
end
end,
AllAddrs
)
end),
_ = erlang:spawn_opt(
fun() ->
AddrsEtsMap = maps:from_list(all_devaddrs_tables()),
CurrentDevaddrs = router_device:devaddrs(Device),

%% Build a list of all DevAddrs we can know about.
AllAddrs = lists:usort(maps:keys(AddrsEtsMap) ++ CurrentDevaddrs),

lists:foreach(
fun(Addr) ->
EtsRef =
case maps:get(Addr, AddrsEtsMap, undefined) of
undefined -> make_devaddr_table(Addr);
Ref -> Ref
end,
case lists:member(Addr, CurrentDevaddrs) of
true -> ets:insert(EtsRef, Device);
false -> ets:delete(EtsRef, DeviceID)
end
end,
AllAddrs
)
end,
[
{monitor, [{tag, {'DOWN', device_save}}]}
]
),
{ok, Device}.

-spec delete(binary()) -> ok.
Expand Down
6 changes: 6 additions & 0 deletions src/device/router_device_worker.erl
Original file line number Diff line number Diff line change
Expand Up @@ -1210,6 +1210,12 @@ handle_info(
fcnt = FCnt
}}
end;
handle_info({{'DOWN', device_save}, _Ref, process, _Pid, normal}, State) ->
lager:debug("device saved"),
{noreply, State};
handle_info({{'DOWN', device_save}, _Ref, process, _Pid, Reason}, State) ->
lager:error("device failed to save ~p", [Reason]),
{noreply, State};
handle_info(_Msg, State) ->
lager:warning("rcvd unknown info msg: ~p", [_Msg]),
{noreply, State}.
Expand Down

0 comments on commit a10d73e

Please sign in to comment.