Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

mod_development: Use the inotifywait command found with the "which" call

  • Loading branch information...
commit 47595470e6306d8cdfcefbd321dd99215dde5d4a 1 parent db48a69
Arjan Scherpenisse arjan authored
Showing with 18 additions and 13 deletions.
  1. +18 −13 modules/mod_development/z_filewatcher_inotify.erl
31 modules/mod_development/z_filewatcher_inotify.erl
View
@@ -29,7 +29,7 @@
-export([init/1, handle_call/3, handle_cast/2, handle_info/2, terminate/2, code_change/3]).
-export([start_link/1]).
--record(state, {port, context, timers=[]}).
+-record(state, {port, executable, context, timers=[]}).
%% interface functions
-export([
@@ -45,10 +45,11 @@ start_link(Context=#context{}) ->
case os:cmd("which inotifywait") of
[] ->
{error, "inotifywait not found"};
- _ ->
+ Output ->
Andreas Stenius Owner
kaos added a note

The issue I had is that which responds with a human readable mungo bungo on not found. See examples below (1 = found, 2 = not found):

1> os:cmd("which inotifywait").
"/usr/bin/inotifywait\n"

2> os:cmd("which inotifywaitt").
"which: no inotifywaitt in (/usr/lib64/erlang/erts-5.9.2/bin:/usr/lib64/erlang/bin:/usr/lib64/qt-3.3/bin:/usr/local/bin:/usr/bin:/bin:/usr/local/sbin:/usr/sbin:/usr/lib64/alliance/bin:/usr/libexec/sdcc:/home/kaos/.local/bin:/home/kaos/bin:/usr/lib64/alliance/bin:/usr/libexec/sdcc)\n"
Andreas Stenius Owner
kaos added a note

Ah, but that was on stderr, so change it to

  os:cmd("which inotifywait 2>/dev/null")

and it should work ;)

Arjan Scherpenisse Owner
arjan added a note

done

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
case whereis(?MODULE) of
undefined ->
- gen_server:start_link({local, ?MODULE}, ?MODULE, Context, []);
+ Executable = hd(string:tokens(Output, "\n")),
+ gen_server:start_link({local, ?MODULE}, ?MODULE, [Executable, Context], []);
Pid ->
{ok, Pid}
end
@@ -64,9 +65,10 @@ start_link(Context=#context{}) ->
%% ignore |
%% {stop, Reason}
%% @doc Initiates the server.
-init(Context) ->
+init([Executable, Context]) ->
process_flag(trap_exit, true),
- {ok, #state{context=Context, port=start_inotify()}}.
+ State = #state{context=Context, executable=Executable},
+ {ok, State, 0}.
%% @doc Trap unknown calls
@@ -82,8 +84,6 @@ handle_cast(Message, State) ->
{stop, {unknown_cast, Message}, State}.
-
-
%% @doc Reading a line from the inotifywait program. Sets a timer to
%% prevent duplicate file changed message for the same filename
%% (e.g. if a editor saves a file twice for some reason).
@@ -111,8 +111,12 @@ handle_info({filechange, Verb, Filename}, State=#state{timers=Timers}) ->
{noreply, State#state{timers=proplists:delete(Filename, Timers)}};
handle_info({'EXIT', Port, _}, State=#state{port=Port}) ->
- ?DEBUG("restart inotify"),
- {noreply, State#state{port=start_inotify()}};
+ %% restart after 5 seconds
+ {noreply, State, 5000};
+
+handle_info(timeout, State=#state{context=Context}) ->
+ ?zInfo("Starting inotify file monitor.", Context),
+ {noreply, start_inotify(State)};
handle_info(_Info, State) ->
?DEBUG(_Info),
@@ -139,17 +143,18 @@ code_change(_OldVsn, State, _Extra) ->
%% support functions
%%====================================================================
-
-start_inotify() ->
+start_inotify(State=#state{executable=Executable}) ->
os:cmd("killall inotifywait"),
- Args = ["-e", "modify,create", "-m", "-r",
+ Args = ["-q", "-e", "modify,create", "-m", "-r",
filename:join(os:getenv("ZOTONIC"), "src"),
filename:join(os:getenv("ZOTONIC"), "priv/sites"),
filename:join(os:getenv("ZOTONIC"), "modules")
|
string:tokens(os:cmd("find " ++ z_utils:os_escape(os:getenv("ZOTONIC")) ++ " -type l"), "\n")],
- erlang:open_port({spawn_executable, "/usr/bin/inotifywait"}, [{args, Args}, {line, 1024}]).
+ Port = erlang:open_port({spawn_executable, Executable}, [{args, Args}, {line, 1024}]),
+ State#state{port=Port}.
verb("MODIFY") -> modify;
verb("CREATE") -> create.
+
Andreas Stenius

The issue I had is that which responds with a human readable mungo bungo on not found. See examples below (1 = found, 2 = not found):

1> os:cmd("which inotifywait").
"/usr/bin/inotifywait\n"

2> os:cmd("which inotifywaitt").
"which: no inotifywaitt in (/usr/lib64/erlang/erts-5.9.2/bin:/usr/lib64/erlang/bin:/usr/lib64/qt-3.3/bin:/usr/local/bin:/usr/bin:/bin:/usr/local/sbin:/usr/sbin:/usr/lib64/alliance/bin:/usr/libexec/sdcc:/home/kaos/.local/bin:/home/kaos/bin:/usr/lib64/alliance/bin:/usr/libexec/sdcc)\n"
Andreas Stenius

Ah, but that was on stderr, so change it to

  os:cmd("which inotifywait 2>/dev/null")

and it should work ;)

Please sign in to comment.
Something went wrong with that request. Please try again.