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

MixTestWatch.Watcher runs tasks asynchronously and restarts them #143

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
9 changes: 7 additions & 2 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
Changelog
=========
# Changelog

## v1.1.2 - 2024-30-01

- `MixTestWatch.Watcher` runs tasks asynchronously.
- `MixTestWatch.Watcher` restarts tasks upon detecting watched events.
- `MixTestWatch.Watcher` logging the reason of tasks restarts.

## v1.1.1 - 2023-09-02

Expand Down
43 changes: 33 additions & 10 deletions lib/mix_test_watch/watcher.ex
Original file line number Diff line number Diff line change
Expand Up @@ -26,37 +26,54 @@ defmodule MixTestWatch.Watcher do
# Genserver callbacks
#

@spec init(String.t()) :: {:ok, Keyword.t()}
@spec init(String.t()) :: {:ok, Task.t()}

def init(_) do
opts = [dirs: [Path.absname("")], name: :mix_test_watcher]

case FileSystem.start_link(opts) do
{:ok, _} ->
FileSystem.subscribe(:mix_test_watcher)
{:ok, []}
{:ok, Task.completed(:ok)}

other ->
Logger.warning("Could not start the file system monitor.")
other
end
end

def handle_cast(:run_tasks, state) do
def handle_cast(:run_tasks, prev) do
config = get_config()
MTW.Runner.run(config)
{:noreply, state}

Logger.info("Received :run_tasks message, restarting tasks...")
next = respawn(prev, config)

{:noreply, next}
end

def handle_info({:file_event, _, {path, _events}}, state) do
def handle_info({:file_event, _, {path, _events}}, prev) do
config = get_config()
path = to_string(path)

if MTW.Path.watching?(path, config) do
MTW.Runner.run(config)
MTW.MessageInbox.flush()
end
next =
if MTW.Path.watching?(path, config) do
Logger.info("File #{path}")
Logger.info("Received :file_event message, restarting tasks...")
next = respawn(prev, config)
MTW.MessageInbox.flush()
next
else
prev
end

{:noreply, next}
end

def handle_info({ref, _}, state) when is_reference(ref) do
{:noreply, state}
end

def handle_info({:DOWN, ref, :process, _, _}, state) when is_reference(ref) do
{:noreply, state}
end

Expand All @@ -69,4 +86,10 @@ defmodule MixTestWatch.Watcher do
defp get_config do
Application.get_env(:mix_test_watch, :__config__, %Config{})
end

@spec respawn(Task.t(), %Config{}) :: Task.t()
defp respawn(prev, config) do
Task.shutdown(prev, :brutal_kill)
Task.async(fn -> MTW.Runner.run(config) end)
end
end
2 changes: 1 addition & 1 deletion mix.exs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ defmodule MixTestWatch.Mixfile do
use Mix.Project

@source_url "https://github.com/lpil/mix-test.watch"
@version "1.1.1"
@version "1.1.2"

def project do
[
Expand Down