Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Send end progress when request is cancelled. #1366

Merged
merged 2 commits into from
Aug 23, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 23 additions & 11 deletions apps/els_lsp/src/els_background_job.erl
Original file line number Diff line number Diff line change
Expand Up @@ -155,6 +155,8 @@ handle_cast(_Request, State) ->

-spec handle_info(any(), any()) ->
{noreply, state()}.
handle_info({exec, InternalState}, State) ->
handle_info(exec, State#{internal_state => InternalState});
handle_info(exec, State) ->
#{
config := #{entries := Entries, task := Task} = Config,
Expand All @@ -171,22 +173,32 @@ handle_info(exec, State) ->
notify_end(Token, Total, ProgressEnabled),
{stop, normal, State};
[Entry | Rest] ->
NewInternalState = Task(Entry, InternalState),
notify_report(
Token,
Current,
Step,
Total,
ProgressEnabled,
ShowPercentages
MainPid = self(),
%% Run the task in a separate process so main process
%% is not blocked from receiving messages, needed for stopping
%% job.
spawn_link(
fun() ->
NewInternalState = Task(Entry, InternalState),
notify_report(
Token,
Current,
Step,
Total,
ProgressEnabled,
ShowPercentages
),
MainPid ! {exec, NewInternalState}
end
),
self() ! exec,
{noreply, State#{
config => Config#{entries => Rest},
current => Current + 1,
internal_state => NewInternalState
current => Current + 1
}}
end;
%% Allow the terminate function to be called if the spawned child processes dies
handle_info({'EXIT', _Sender, Reason}, State) when Reason /= normal ->
{stop, Reason, State};
handle_info(_Request, State) ->
{noreply, State}.

Expand Down
14 changes: 12 additions & 2 deletions apps/els_lsp/src/els_server.erl
Original file line number Diff line number Diff line change
Expand Up @@ -230,6 +230,16 @@ handle_request(
{RequestId, Job} when RequestId =:= Id ->
?LOG_DEBUG("[SERVER] Cancelling request [id=~p] [job=~p]", [Id, Job]),
els_background_job:stop(Job),
Error = #{
code => ?ERR_REQUEST_CANCELLED,
message => <<"Request was cancelled">>
},
ErrorResponse = els_protocol:error(RequestId, Error),
?LOG_DEBUG(
"[SERVER] Sending error response [response=~p]",
[ErrorResponse]
),
send(ErrorResponse, State0),
State0#{
pending => lists:keydelete(Id, 1, Pending),
in_progress => lists:keydelete(Job, 2, InProgress)
Expand Down Expand Up @@ -272,8 +282,8 @@ handle_request(
{async, Uri, BackgroundJob, State} ->
RequestId = maps:get(<<"id">>, Request),
?LOG_DEBUG(
"[SERVER] Suspending response [background_job=~p]",
[BackgroundJob]
"[SERVER] Suspending response [background_job=~p id=~p]",
[BackgroundJob, RequestId]
),
NewPending = [{RequestId, BackgroundJob} | Pending],
State#{
Expand Down
24 changes: 23 additions & 1 deletion apps/els_lsp/test/els_progress_SUITE.erl
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,8 @@
%%==============================================================================
-export([
sample_job/1,
failing_job/1
failing_job/1,
stop_job/1
]).

%%==============================================================================
Expand Down Expand Up @@ -63,6 +64,16 @@ init_per_testcase(sample_job = TestCase, Config) ->
init_per_testcase(failing_job = TestCase, Config) ->
Task = fun(_, _) -> exit(fail) end,
setup_mocks(Task),
[{task, Task} | els_test_utils:init_per_testcase(TestCase, Config)];
init_per_testcase(stop_job = TestCase, Config) ->
Task = fun(_, _) ->
sample_job:task_called(),
timer:sleep(timer:seconds(100))
end,
setup_mocks(Task),
%% Bit of a hack, because meck only count history after mocked function returns it seems,
%% and the above function will not return.
meck:expect(sample_job, task_called, fun() -> ok end),
[{task, Task} | els_test_utils:init_per_testcase(TestCase, Config)].

-spec end_per_testcase(atom(), config()) -> ok.
Expand Down Expand Up @@ -92,6 +103,17 @@ failing_job(_Config) ->
?assertEqual(1, meck:num_calls(sample_job, on_error, '_')),
ok.

-spec stop_job(config()) -> ok.
stop_job(_Config) ->
{ok, Pid} = new_background_job(),
meck:wait(sample_job, task_called, '_', 5000),
els_background_job:stop(Pid),
wait_for_completion(Pid),
?assertEqual(1, meck:num_calls(sample_job, task_called, '_')),
?assertEqual(0, meck:num_calls(sample_job, on_complete, '_')),
?assertEqual(1, meck:num_calls(sample_job, on_error, '_')),
ok.

%%==============================================================================
%% Internal Functions
%%==============================================================================
Expand Down