Skip to content

Commit a1bf209

Browse files
Clean up WorkerSetup module (#258)
1 parent b461ff6 commit a1bf209

File tree

5 files changed

+69
-116
lines changed

5 files changed

+69
-116
lines changed

Project.toml

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@ REPL = "3fa0cd96-eef1-5676-8a61-b3b8758bbffb"
1919
Random = "9a3f8284-a2c9-5f02-9a11-845980a1fd5c"
2020
RelocatableFolders = "05181044-ff0b-4ac5-8273-598c1e38db00"
2121
SHA = "ea8e919c-243c-51af-8825-aaa63cd721ce"
22-
Scratch = "6c6a2e73-6563-6170-7368-637461726353"
2322
Sockets = "6462fe0b-24de-5631-8697-dd941f90decc"
2423
TOML = "fa267f1f-6049-4f14-aa54-33bafae1ed76"
2524
YAML = "ddb6d928-2868-570f-bddf-ab3f9cf99eb6"
@@ -40,7 +39,6 @@ REPL = "1.6"
4039
Random = "1.6"
4140
RelocatableFolders = "1"
4241
SHA = "0.7, 1.6"
43-
Scratch = "1"
4442
Sockets = "1.6"
4543
TOML = "1"
4644
YAML = "0.4"

src/WorkerSetup.jl

Lines changed: 11 additions & 92 deletions
Original file line numberDiff line numberDiff line change
@@ -16,39 +16,10 @@ module WorkerSetup
1616

1717
# Imports.
1818

19-
import Pkg
2019
import RelocatableFolders
21-
import Scratch
22-
import TOML
2320

24-
# Package init-time.
25-
26-
# To allow it to be added to a system image we make sure it is relocatable.
21+
# Support adding to a system image.
2722
const QNW = RelocatableFolders.@path joinpath(@__DIR__, "QuartoNotebookWorker")
28-
let
29-
# Any content from the worker package should trigger recompilation in the
30-
# runner package, for ease of development.
31-
for (root, dirs, files) in walkdir(QNW)
32-
for file in files
33-
include_dependency(joinpath(root, file))
34-
end
35-
end
36-
end
37-
38-
# The loader environment is used to load the worker package into whatever
39-
# environment that the user has started the process with.
40-
const LOADER_ENV = Ref("")
41-
42-
# Since we start a task to perform the loader env setup at package init-time we
43-
# don't want that to block `using QuartoNotebookRunner` so we lock the setup
44-
# task to allow prevention of starting the loader env until
45-
const WORKER_SETUP_LOCK = ReentrantLock()
46-
47-
function __init__()
48-
if ccall(:jl_generating_output, Cint, ()) == 0
49-
LOADER_ENV[] = String(QNW)
50-
end
51-
end
5223

5324
# Debugging utilities.
5425

@@ -69,53 +40,14 @@ julia> include("runtests.jl")
6940
to run the test suite without having to reload the worker package.
7041
"""
7142
function debug(; exeflags = String[])
72-
if islocked(WORKER_SETUP_LOCK)
73-
error("Worker setup is in progress. Please try again later.")
74-
else
75-
mktempdir() do temp_dir
76-
file = joinpath(temp_dir, "setup.jl")
77-
project = LOADER_ENV[]
78-
write(
79-
file,
80-
"""
81-
# Try load `Revise` first, since we want to be able to track
82-
# changes in the worker package.
83-
try
84-
import Revise
85-
catch error
86-
@info "Revise not available."
87-
end
88-
89-
cd($(repr(QNW)))
90-
91-
pushfirst!(LOAD_PATH, $(repr(project)))
92-
93-
# Always do a `precompile` so that it's simpler to kill and
94-
# restart the worker without it potentially being stale.
95-
import Pkg
96-
Pkg.precompile()
97-
98-
import QuartoNotebookWorker
99-
100-
# Attempt to import some other useful packages.
101-
try
102-
import Debugger
103-
catch error
104-
@info "Debugger not available."
105-
end
106-
try
107-
import TestEnv
108-
catch error
109-
@info "TestEnv not available."
110-
end
111-
""",
112-
)
113-
julia = Base.julia_cmd()[1]
114-
cmd = `$julia $exeflags --startup-file=no -i $file`
115-
run(cmd)
116-
end
43+
mktempdir() do temp_dir
44+
julia = Base.julia_cmd()[1]
45+
debug_env = joinpath(temp_dir, "QuartoNotebookWorker.DEBUG")
46+
cmd = `$julia $exeflags --project=$debug_env --startup-file=no -i $DEBUG_STARTUP`
47+
run(addenv(cmd, "QUARTONOTEBOOKWORKER_PACKAGE" => String(QNW)))
11748
end
11849
end
50+
const DEBUG_STARTUP = RelocatableFolders.@path joinpath(@__DIR__, "debug_startup.jl")
11951

12052
"""
12153
test()
@@ -126,25 +58,12 @@ the `TestEnv` package to do so.
12658
"""
12759
function test(; exeflags = String[])
12860
mktempdir() do temp_dir
129-
file = joinpath(temp_dir, "runtests.jl")
130-
project = LOADER_ENV[]
131-
write(
132-
file,
133-
"""
134-
pushfirst!(LOAD_PATH, "@stdlib")
135-
import Pkg
136-
popfirst!(LOAD_PATH)
137-
138-
cd($(repr(QNW)))
139-
140-
pushfirst!(LOAD_PATH, $(repr(project)))
141-
Pkg.test("QuartoNotebookWorker")
142-
""",
143-
)
14461
julia = Base.julia_cmd()[1]
145-
cmd = `$julia $exeflags --startup-file=no $file`
146-
run(cmd)
62+
test_env = joinpath(temp_dir, "QuartoNotebookWorker.TEST")
63+
cmd = `$julia $exeflags --project=$test_env --startup-file=no $TEST_STARTUP`
64+
run(addenv(cmd, "QUARTONOTEBOOKWORKER_PACKAGE" => String(QNW)))
14765
end
14866
end
67+
const TEST_STARTUP = RelocatableFolders.@path joinpath(@__DIR__, "test_startup.jl")
14968

15069
end

src/debug_startup.jl

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
# Try load `Revise` first, since we want to be able to track changes in the
2+
# worker package and it needs to be loaded prior to any packages to track.
3+
try
4+
import Revise
5+
catch error
6+
@info "Revise not available."
7+
end
8+
9+
import Pkg
10+
let qnw = ENV["QUARTONOTEBOOKWORKER_PACKAGE"]
11+
cd(qnw)
12+
Pkg.develop(; path = qnw)
13+
Pkg.precompile()
14+
end
15+
16+
import QuartoNotebookWorker
17+
QuartoNotebookWorker.NotebookState.define_notebook_module!()
18+
19+
# Attempt to import some other useful packages.
20+
try
21+
import Debugger
22+
catch error
23+
@info "Debugger not available."
24+
end
25+
try
26+
import TestEnv
27+
catch error
28+
@info "TestEnv not available."
29+
end

src/test_startup.jl

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
import Pkg
2+
let qnw = ENV["QUARTONOTEBOOKWORKER_PACKAGE"]
3+
cd(qnw)
4+
Pkg.develop(; path = qnw)
5+
Pkg.precompile()
6+
Pkg.test("QuartoNotebookWorker")
7+
end

src/worker.jl

Lines changed: 22 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,28 @@
11
function worker_init(f::File, options::Dict)
2-
return lock(WorkerSetup.WORKER_SETUP_LOCK) do
3-
return quote
4-
# issue #192
5-
# Malt itself uses a new task for each `remote_eval` and because of this, random number streams
6-
# are not consistent across runs even if seeded, as each task introduces a new state for its
7-
# task-local RNG. As a workaround, we feed all `remote_eval` requests through these channels, such
8-
# that the task executing code is always the same.
9-
const stable_execution_task_channel_out = Channel()
10-
const stable_execution_task_channel_in = Channel() do chan
11-
for expr in chan
12-
result = Core.eval(Main, expr)
13-
put!(stable_execution_task_channel_out, result)
14-
end
15-
end
16-
17-
let QNW = Main.QuartoNotebookWorker
18-
QNW.NotebookState.PROJECT[] = Base.active_project()
19-
QNW.NotebookState.OPTIONS[] = $(options)
20-
QNW.NotebookState.define_notebook_module!(Main)
21-
global refresh!(args...) = QNW.refresh!($(f.path), $(options), args...)
22-
global render(args...; kwargs...) = QNW.render(args...; kwargs...)
2+
return quote
3+
# Issue #192
4+
#
5+
# Malt itself uses a new task for each `remote_eval` and because of
6+
# this, random number streams are not consistent across runs even if
7+
# seeded, as each task introduces a new state for its task-local RNG.
8+
# As a workaround, we feed all `remote_eval` requests through these
9+
# channels, such that the task executing code is always the same.
10+
const stable_execution_task_channel_out = Channel()
11+
const stable_execution_task_channel_in = Channel() do chan
12+
for expr in chan
13+
result = Core.eval(Main, expr)
14+
put!(stable_execution_task_channel_out, result)
2315
end
16+
end
2417

25-
nothing
18+
let QNW = Main.QuartoNotebookWorker
19+
QNW.NotebookState.PROJECT[] = Base.active_project()
20+
QNW.NotebookState.OPTIONS[] = $(options)
21+
QNW.NotebookState.define_notebook_module!(Main)
22+
global refresh!(args...) = QNW.refresh!($(f.path), $(options), args...)
23+
global render(args...; kwargs...) = QNW.render(args...; kwargs...)
2624
end
25+
26+
nothing
2727
end
2828
end

0 commit comments

Comments
 (0)