-
Notifications
You must be signed in to change notification settings - Fork 2.9k
/
server.ex
295 lines (237 loc) · 7.33 KB
/
server.ex
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
defmodule Phoenix.CodeReloader.Server do
@moduledoc false
use GenServer
require Logger
alias Phoenix.CodeReloader.Proxy
def start_link(_) do
GenServer.start_link(__MODULE__, false, name: __MODULE__)
end
def check_symlinks do
GenServer.call(__MODULE__, :check_symlinks, :infinity)
end
def reload!(endpoint) do
GenServer.call(__MODULE__, {:reload!, endpoint}, :infinity)
end
@doc """
Synchronizes with the code server if it is alive.
If it is not running, it also returns true.
"""
def sync do
pid = Process.whereis(__MODULE__)
ref = Process.monitor(pid)
GenServer.cast(pid, {:sync, self(), ref})
receive do
^ref -> :ok
{:DOWN, ^ref, _, _, _} -> :ok
end
end
## Callbacks
def init(false) do
{:ok, false}
end
def handle_call(:check_symlinks, _from, checked?) do
if not checked? and Code.ensure_loaded?(Mix.Project) and not Mix.Project.umbrella?() do
priv_path = "#{Mix.Project.app_path()}/priv"
case :file.read_link(priv_path) do
{:ok, _} ->
:ok
{:error, _} ->
if can_symlink?() do
File.rm_rf(priv_path)
Mix.Project.build_structure()
else
Logger.warn(
"Phoenix is unable to create symlinks. Phoenix' code reloader will run " <>
"considerably faster if symlinks are allowed." <> os_symlink(:os.type())
)
end
end
end
{:reply, :ok, true}
end
def handle_call({:reload!, endpoint}, from, state) do
compilers = endpoint.config(:reloadable_compilers)
reloadable_apps = endpoint.config(:reloadable_apps) || default_reloadable_apps()
backup = load_backup(endpoint)
froms = all_waiting([from], endpoint)
{res, out} =
proxy_io(fn ->
try do
mix_compile(Code.ensure_loaded(Mix.Task), compilers, reloadable_apps)
catch
:exit, {:shutdown, 1} ->
:error
kind, reason ->
IO.puts(Exception.format(kind, reason, __STACKTRACE__))
:error
end
end)
reply =
case res do
:ok ->
:ok
:error ->
write_backup(backup)
{:error, out}
end
Enum.each(froms, &GenServer.reply(&1, reply))
{:noreply, state}
end
def handle_cast({:sync, pid, ref}, state) do
send(pid, ref)
{:noreply, state}
end
def handle_info(_, state) do
{:noreply, state}
end
defp default_reloadable_apps() do
if Mix.Project.umbrella?() do
Enum.map(Mix.Dep.Umbrella.cached(), & &1.app)
else
[Mix.Project.config()[:app]]
end
end
defp os_symlink({:win32, _}),
do:
" On Windows, the lack of symlinks may even cause empty assets to be served. " <>
"Luckily, you can address this issue by starting your Windows terminal at least " <>
"once with \"Run as Administrator\" and then running your Phoenix application."
defp os_symlink(_),
do: ""
defp can_symlink?() do
build_path = Mix.Project.build_path()
symlink = Path.join(Path.dirname(build_path), "__phoenix__")
case File.ln_s(build_path, symlink) do
:ok ->
File.rm_rf(symlink)
true
{:error, :eexist} ->
File.rm_rf(symlink)
true
{:error, _} ->
false
end
end
defp load_backup(mod) do
mod
|> :code.which()
|> read_backup()
end
defp read_backup(path) when is_list(path) do
case File.read(path) do
{:ok, binary} -> {:ok, path, binary}
_ -> :error
end
end
defp read_backup(_path), do: :error
defp write_backup({:ok, path, file}), do: File.write!(path, file)
defp write_backup(:error), do: :ok
defp all_waiting(acc, endpoint) do
receive do
{:"$gen_call", from, {:reload!, ^endpoint}} -> all_waiting([from | acc], endpoint)
after
0 -> acc
end
end
defp mix_compile({:module, Mix.Task}, compilers, apps_to_reload) do
config = Mix.Project.config()
path = Mix.Project.consolidation_path(config)
purge = fn -> purge_consolidated(path) end
purge = mix_compile_deps(Mix.Dep.cached(), apps_to_reload, compilers, purge)
mix_compile_project(config[:app], apps_to_reload, compilers, purge)
Code.prepend_path(path)
:ok
end
defp mix_compile({:error, _reason}, _, _) do
raise "the Code Reloader is enabled but Mix is not available. If you want to " <>
"use the Code Reloader in production or inside an escript, you must add " <>
":mix to your applications list. Otherwise, you must disable code reloading " <>
"in such environments"
end
defp mix_compile_deps(deps, apps_to_reload, compilers, purge) do
Enum.reduce(deps, purge, fn dep, purge ->
if dep.app in apps_to_reload do
Mix.Dep.in_dependency(dep, fn _ ->
mix_compile_unless_stale_config(compilers, purge)
end)
else
purge
end
end)
end
defp mix_compile_project(nil, _, _, _), do: :ok
defp mix_compile_project(app, apps_to_reload, compilers, purge) do
if app in apps_to_reload do
mix_compile_unless_stale_config(compilers, purge)
else
purge
end
end
defp mix_compile_unless_stale_config(compilers, purge) do
manifests = Mix.Tasks.Compile.Elixir.manifests()
configs = Mix.Project.config_files()
case Mix.Utils.extract_stale(configs, manifests) do
[] ->
purge = purge.()
mix_compile(compilers)
purge
files ->
raise """
could not compile application: #{Mix.Project.config()[:app]}.
You must restart your server after changing the following config or lib files:
* #{Enum.map_join(files, "\n * ", &Path.relative_to_cwd/1)}
"""
end
end
defp mix_compile(compilers) do
config = Mix.Project.config()
all = config[:compilers] || Mix.compilers()
compilers =
for compiler <- compilers, compiler in all do
Mix.Task.reenable("compile.#{compiler}")
compiler
end
# We call build_structure mostly for Windows so new
# assets in priv are copied to the build directory.
Mix.Project.build_structure(config)
results = Enum.map(compilers, &Mix.Task.run("compile.#{&1}", []))
# Results are either {:ok, _} | {:error, _}, {:noop, _} or
# :ok | :error | :noop. So we use proplists to do the unwraping.
cond do
:proplists.get_value(:error, results, false) ->
exit({:shutdown, 1})
:proplists.get_value(:ok, results, false) && config[:consolidate_protocols] ->
Mix.Task.reenable("compile.protocols")
Mix.Task.run("compile.protocols", [])
:ok
true ->
:ok
end
end
defp purge_consolidated(path) do
with {:ok, beams} <- File.ls(path) do
Enum.map(beams, & &1 |> Path.rootname(".beam") |> String.to_atom() |> purge_module())
end
Code.delete_path(path)
# Now return a stub for the next calls
recursive_fun()
end
defp purge_module(module) do
:code.purge(module)
:code.delete(module)
end
defp recursive_fun() do
fn -> recursive_fun() end
end
defp proxy_io(fun) do
original_gl = Process.group_leader()
{:ok, proxy_gl} = Proxy.start()
Process.group_leader(self(), proxy_gl)
try do
{fun.(), Proxy.stop(proxy_gl)}
after
Process.group_leader(self(), original_gl)
Process.exit(proxy_gl, :kill)
end
end
end