summaryrefslogtreecommitdiff
path: root/src/util.erl
diff options
context:
space:
mode:
authorMagnus Ahltorp <map@kth.se>2014-11-18 00:17:28 +0100
committerMagnus Ahltorp <map@kth.se>2014-11-19 05:03:19 +0100
commit4c4f904ee1a7fe2b61137532de3668ba5ad3a6d8 (patch)
tree6a0e1126aeffca6a3f0101078c948554a8a42970 /src/util.erl
parenteabc9b4bdca8409601400276018eb9eec6a162d0 (diff)
Move plop:add out of gen_server
Diffstat (limited to 'src/util.erl')
-rw-r--r--src/util.erl21
1 files changed, 20 insertions, 1 deletions
diff --git a/src/util.erl b/src/util.erl
index 435dbc8..2125d5e 100644
--- a/src/util.erl
+++ b/src/util.erl
@@ -3,7 +3,8 @@
-module(util).
-export([tempfilename/1, fsync/1, exit_with_error/3,
- check_error/3, write_tempfile_and_rename/3]).
+ check_error/3, write_tempfile_and_rename/3,
+ spawn_and_wait/1]).
-spec tempfilename(string()) -> string().
tempfilename(Base) ->
@@ -51,3 +52,21 @@ write_tempfile_and_rename(Name, NurseryName, Content) ->
exit_with_error(writefile, Error,
"Error when creating tempfile")
end.
+
+spawn_and_wait(Fun) ->
+ ParentPid = self(),
+ ChildPid = spawn_link(fun () ->
+ try
+ Result = Fun(),
+ ParentPid ! {result, self(), Result}
+ catch
+ Type:What ->
+ [CrashFunction | Stack] = erlang:get_stacktrace(),
+ lager:error("Crashed process: ~p ~p~n ~p~n ~p~n", [Type, What, CrashFunction, Stack]),
+ ParentPid ! {result, self(), crash}
+ end
+ end),
+ receive
+ {result, ChildPid, Result} ->
+ Result
+ end.