Fix ff_transfer_machine actions (#18)

This commit is contained in:
Anton Belyaev 2018-10-08 15:01:06 +03:00 committed by GitHub
parent 2e83eb06a5
commit f34daad30c
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 39 additions and 26 deletions

View File

@ -126,9 +126,9 @@ events(ID, Range) ->
%% ff_transfer_machine behaviour
-spec process_transfer(deposit()) ->
{ok, [ff_transfer_machine:event(ff_transfer:event())]} |
{ok, {ff_transfer_machine:action(), [ff_transfer_machine:event(ff_transfer:event())]}} |
{error, _Reason}.
process_transfer(#{status := pending, p_transfer := #{status := prepared}}) ->
{ok, [{status_changed, succeeded}]};
{ok, {continue, [{status_changed, succeeded}]}};
process_transfer(Transfer) ->
ff_transfer:process_transfer(Transfer).

View File

@ -118,7 +118,7 @@ construct_p_transfer_id(ID) ->
%% ff_transfer_machine behaviour
-spec process_transfer(transfer(_)) ->
{ok, [ff_transfer_machine:event(event())] | poll} |
{ok, {ff_transfer_machine:action(), [ff_transfer_machine:event(event())]}} |
{error, _Reason}.
process_transfer(Transfer) ->
@ -142,13 +142,19 @@ deduce_activity(_) ->
undefined.
process_activity(prepare_transfer, Transfer) ->
with(p_transfer, Transfer, fun ff_postings_transfer:prepare/1);
do(fun () ->
{continue, unwrap(with(p_transfer, Transfer, fun ff_postings_transfer:prepare/1))}
end);
process_activity(commit_transfer, Transfer) ->
with(p_transfer, Transfer, fun ff_postings_transfer:commit/1);
do(fun () ->
{undefined, unwrap(with(p_transfer, Transfer, fun ff_postings_transfer:commit/1))}
end);
process_activity(cancel_transfer, Transfer) ->
with(p_transfer, Transfer, fun ff_postings_transfer:cancel/1).
do(fun () ->
{undefined, unwrap(with(p_transfer, Transfer, fun ff_postings_transfer:cancel/1))}
end).
%%

View File

@ -17,19 +17,21 @@
%% Behaviour definition
-type st(T) :: ff_machine:st(transfer(T)).
-type action() :: poll | continue | undefined.
-export_type([id/0]).
-export_type([ns/0]).
-export_type([action/0]).
-export_type([st/1]).
-export_type([event/1]).
-export_type([events/1]).
-callback process_transfer(transfer(_)) ->
{ok, [event(_)] | poll} |
{ok, {action(), [event(_)]}} |
{error, _Reason}.
-callback process_call(_CallArgs, transfer(_)) ->
{ok, [event(_)] | poll} |
{ok, {action(), [event(_)]}} |
{error, _Reason}.
-optional_callbacks([process_call/2]).
@ -142,21 +144,26 @@ process_call(CallArgs, Machine, _, _Opts) ->
Transfer = transfer(St),
{ok, process_result((ff_transfer:handler(Transfer)):process_call(CallArgs, Transfer), St)}.
process_result({ok, poll}, St) ->
#{
action => set_poll_timer(St)
};
process_result({ok, Events}, _St) ->
#{
events => ff_machine:emit_events(Events),
action => continue
};
process_result({ok, {Action, Events}}, St) ->
genlib_map:compact(#{
events => set_events(Events),
action => set_action(Action, St)
});
process_result({error, Reason}, _St) ->
#{
events => emit_failure(Reason)
}.
set_poll_timer(St) ->
set_events([]) ->
undefined;
set_events(Events) ->
ff_machine:emit_events(Events).
set_action(continue, _St) ->
continue;
set_action(undefined, _St) ->
undefined;
set_action(poll, St) ->
Now = machinery_time:now(),
Timeout = erlang:max(1, machinery_time:interval(Now, ff_machine:updated(St)) div 1000),
{set_timer, {timeout, Timeout}}.

View File

@ -130,7 +130,7 @@ events(ID, Range) ->
%% ff_transfer_machine behaviour
-spec process_transfer(withdrawal()) ->
{ok, [ff_transfer_machine:event(ff_transfer:event())]} |
{ok, {ff_transfer_machine:action(), [ff_transfer_machine:event(ff_transfer:event())]}} |
{error, _Reason}.
process_transfer(Transfer = #{status := pending, session := _}) ->
@ -152,7 +152,7 @@ create_session(Withdrawal) ->
},
do(fun () ->
ok = unwrap(ff_withdrawal_session_machine:create(ID, TransferData, #{destination => destination(Withdrawal)})),
[{session_started, ID}]
{continue, [{session_started, ID}]}
end).
construct_session_id(ID) ->
@ -163,16 +163,16 @@ poll_session_completion(#{session := SID}) ->
do(fun () ->
case ff_withdrawal_session_machine:status(Session) of
active ->
poll;
{poll, []};
{finished, {success, _}} ->
[
{continue, [
{session_finished, SID},
{status_changed, succeeded}
];
]};
{finished, {failed, Failure}} ->
[
{continue, [
{session_finished, SID},
{status_changed, {failed, Failure}}
]
]}
end
end).