diff options
| author | Magnus Ahltorp <map@kth.se> | 2014-11-18 00:17:28 +0100 |
|---|---|---|
| committer | Magnus Ahltorp <map@kth.se> | 2014-11-18 00:17:28 +0100 |
| commit | ebcbc33cf659715451bdd1f82d6c8e7204521ecb (patch) | |
| tree | 46e6fb947083bd1e863ee85024a8697bb381c332 /src/util.erl | |
| parent | 1934ec9bef2400aff385ea64874f0d773a625e58 (diff) | |
Move plop:add out of gen_server
Diffstat (limited to 'src/util.erl')
| -rw-r--r-- | src/util.erl | 21 |
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. |
