-
-
Notifications
You must be signed in to change notification settings - Fork 83
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Added reindex command Added a reindex code lens so we can rebuild indexes without resorting to manual operations. As we build the indexing infrastructure out, we'll likely need to rebuild the index a lot. Presently, this means restarting the server, which can take some time, so I thought i'd add a command to do it instead. I tried using code actions, but this was a bit fraught, so instead, I used a code lens on the project definition in your mix.exs file. The code lens disappears when clicked and reappears when the indexing job is done.
- Loading branch information
Showing
25 changed files
with
668 additions
and
22 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -8,6 +8,7 @@ proto_dsl = [ | |
defrequest: 2, | ||
defresponse: 1, | ||
deftype: 1, | ||
server_request: 2, | ||
server_request: 3 | ||
] | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
7 changes: 7 additions & 0 deletions
7
apps/protocol/lib/generated/lexical/protocol/types/code_lens.ex
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
# This file's contents are auto-generated. Do not edit. | ||
defmodule Lexical.Protocol.Types.CodeLens do | ||
alias Lexical.Proto | ||
alias Lexical.Protocol.Types | ||
use Proto | ||
deftype command: optional(Types.Command), data: optional(any()), range: Types.Range | ||
end |
10 changes: 10 additions & 0 deletions
10
apps/protocol/lib/generated/lexical/protocol/types/code_lens/params.ex
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
# This file's contents are auto-generated. Do not edit. | ||
defmodule Lexical.Protocol.Types.CodeLens.Params do | ||
alias Lexical.Proto | ||
alias Lexical.Protocol.Types | ||
use Proto | ||
|
||
deftype partial_result_token: optional(Types.Progress.Token), | ||
text_document: Types.TextDocument.Identifier, | ||
work_done_token: optional(Types.Progress.Token) | ||
end |
10 changes: 10 additions & 0 deletions
10
apps/protocol/lib/generated/lexical/protocol/types/execute_command/params.ex
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
# This file's contents are auto-generated. Do not edit. | ||
defmodule Lexical.Protocol.Types.ExecuteCommand.Params do | ||
alias Lexical.Proto | ||
alias Lexical.Protocol.Types | ||
use Proto | ||
|
||
deftype arguments: optional(list_of(any())), | ||
command: string(), | ||
work_done_token: optional(Types.Progress.Token) | ||
end |
6 changes: 6 additions & 0 deletions
6
apps/protocol/lib/generated/lexical/protocol/types/execute_command/registration/options.ex
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
# This file's contents are auto-generated. Do not edit. | ||
defmodule Lexical.Protocol.Types.ExecuteCommand.Registration.Options do | ||
alias Lexical.Proto | ||
use Proto | ||
deftype commands: list_of(string()), work_done_progress: optional(boolean()) | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
149 changes: 149 additions & 0 deletions
149
apps/remote_control/lib/lexical/remote_control/commands/reindex.ex
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,149 @@ | ||
defmodule Lexical.RemoteControl.Commands.Reindex do | ||
defmodule State do | ||
alias Lexical.Ast.Analysis | ||
alias Lexical.Document | ||
alias Lexical.RemoteControl.Search | ||
alias Lexical.RemoteControl.Search.Indexer | ||
|
||
require Logger | ||
defstruct reindex_fun: nil, index_task: nil, pending_updates: %{} | ||
|
||
def new(reindex_fun) do | ||
%__MODULE__{reindex_fun: reindex_fun} | ||
end | ||
|
||
def set_task(%__MODULE__{} = state, {_, _} = task) do | ||
%__MODULE__{state | index_task: task} | ||
end | ||
|
||
def clear_task(%__MODULE__{} = state) do | ||
%__MODULE__{state | index_task: nil} | ||
end | ||
|
||
def reindex_uri(%__MODULE__{index_task: nil} = state, uri) do | ||
case entries_for_uri(uri) do | ||
{:ok, path, entries} -> | ||
Search.Store.update(path, entries) | ||
|
||
_ -> | ||
:ok | ||
end | ||
|
||
state | ||
end | ||
|
||
def reindex_uri(%__MODULE__{} = state, uri) do | ||
case entries_for_uri(uri) do | ||
{:ok, path, entries} -> | ||
put_in(state.pending_updates[path], entries) | ||
|
||
_ -> | ||
state | ||
end | ||
end | ||
|
||
def flush_pending_updates(%__MODULE__{} = state) do | ||
Enum.each(state.pending_updates, fn {path, entries} -> | ||
Search.Store.update(path, entries) | ||
end) | ||
|
||
%__MODULE__{state | pending_updates: %{}} | ||
end | ||
|
||
defp entries_for_uri(uri) do | ||
with {:ok, %Document{} = document, %Analysis{} = analysis} <- | ||
Document.Store.fetch(uri, :analysis), | ||
{:ok, entries} <- Indexer.Quoted.index(analysis) do | ||
{:ok, document.path, entries} | ||
else | ||
error -> | ||
Logger.error("Could not update index because #{inspect(error)}") | ||
error | ||
end | ||
end | ||
end | ||
|
||
@moduledoc """ | ||
A simple genserver that prevents more than one reindexing job from running at the same time | ||
""" | ||
|
||
alias Lexical.Document | ||
alias Lexical.Project | ||
alias Lexical.RemoteControl.Api | ||
alias Lexical.RemoteControl.Dispatch | ||
alias Lexical.RemoteControl.Search | ||
|
||
use GenServer | ||
import Api.Messages | ||
|
||
def start_link(reindex_fun) when is_function(reindex_fun, 1) do | ||
GenServer.start_link(__MODULE__, reindex_fun, name: __MODULE__) | ||
end | ||
|
||
def start_link(_) do | ||
start_link(&do_reindex/1) | ||
end | ||
|
||
def uri(uri) do | ||
GenServer.cast(__MODULE__, {:reindex_uri, uri}) | ||
end | ||
|
||
def perform(%Project{} = project) do | ||
GenServer.call(__MODULE__, {:perform, project}) | ||
end | ||
|
||
def running? do | ||
GenServer.call(__MODULE__, :running?) | ||
end | ||
|
||
@impl GenServer | ||
def init(reindex_fun) do | ||
{:ok, State.new(reindex_fun)} | ||
end | ||
|
||
@impl GenServer | ||
def handle_call(:running?, _from, %State{index_task: index_task} = state) do | ||
{:reply, match?({_, _}, index_task), state} | ||
end | ||
|
||
def handle_call({:perform, project}, _from, %State{index_task: nil} = state) do | ||
index_task = spawn_monitor(fn -> state.reindex_fun.(project) end) | ||
{:reply, :ok, State.set_task(state, index_task)} | ||
end | ||
|
||
def handle_call({:perform, _project}, _from, state) do | ||
{:reply, {:error, "Already Running"}, state} | ||
end | ||
|
||
@impl GenServer | ||
def handle_cast({:reindex_uri, uri}, %State{} = state) do | ||
{:noreply, State.reindex_uri(state, uri)} | ||
end | ||
|
||
@impl GenServer | ||
def handle_info({:DOWN, ref, :process, pid, _reason}, %State{index_task: {pid, ref}} = state) do | ||
new_state = | ||
state | ||
|> State.flush_pending_updates() | ||
|> State.clear_task() | ||
|
||
{:noreply, new_state} | ||
end | ||
|
||
defp do_reindex(%Project{} = project) do | ||
Dispatch.broadcast(project_reindex_requested(project: project)) | ||
|
||
{elapsed_us, result} = | ||
:timer.tc(fn -> | ||
with {:ok, entries} <- Search.Indexer.create_index(project) do | ||
Search.Store.replace(entries) | ||
end | ||
end) | ||
|
||
Dispatch.broadcast( | ||
project_reindexed(project: project, elapsed_ms: round(elapsed_us / 1000), status: :success) | ||
) | ||
|
||
result | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.