Skip to content
This repository was archived by the owner on Dec 15, 2022. It is now read-only.

Commit ad83c38

Browse files
authored
Merge pull request #200 from atom/aw/deferred-worker-start
Defer worker start
2 parents b7428db + e6de1d9 commit ad83c38

File tree

7 files changed

+58
-16
lines changed

7 files changed

+58
-16
lines changed

src/binding.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -123,7 +123,7 @@ void configure(const Nan::FunctionCallbackInfo<Value> &info)
123123
}
124124

125125
all->set_result(move(r));
126-
all->fire_if_empty(true);
126+
all->mark_ready();
127127
}
128128

129129
void watch(const Nan::FunctionCallbackInfo<Value> &info)

src/hub.cpp

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,6 @@ Hub::Hub() :
6060
report_uv_error(err);
6161
}
6262

63-
report_if_error(worker_thread.run());
6463
freeze();
6564
}
6665

@@ -92,13 +91,13 @@ Result<> Hub::unwatch(ChannelID channel_id, unique_ptr<AsyncCallback> &&ack_call
9291

9392
string root;
9493
shared_ptr<AllCallback> all = AllCallback::create(move(ack_callback));
94+
unique_ptr<AsyncCallback> worker_cb = all->create_callback("@atom/watcher:hub.unwatch.worker");
95+
unique_ptr<AsyncCallback> polling_cb = all->create_callback("@atom/worker:hub.unwatch.polling");
9596

9697
Result<> r = ok_result();
97-
r &= send_command(
98-
worker_thread, CommandPayloadBuilder::remove(channel_id), all->create_callback("@atom/watcher:hub.unwatch.worker"));
99-
r &= send_command(polling_thread,
100-
CommandPayloadBuilder::remove(channel_id),
101-
all->create_callback("@atom/worker:hub.unwatch.polling"));
98+
r &= send_command(worker_thread, CommandPayloadBuilder::remove(channel_id), move(worker_cb));
99+
r &= send_command(polling_thread, CommandPayloadBuilder::remove(channel_id), move(polling_cb));
100+
all->mark_ready();
102101

103102
auto maybe_event_callback = channel_callbacks.find(channel_id);
104103
if (maybe_event_callback == channel_callbacks.end()) {

src/nan/all_callback.cpp

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ shared_ptr<AllCallback> AllCallback::create(unique_ptr<AsyncCallback> &&done)
3434

3535
AllCallback::AllCallback(unique_ptr<AsyncCallback> &&done) :
3636
done(move(done)),
37+
ready{false},
3738
fired{false},
3839
total{0},
3940
remaining{0},
@@ -55,6 +56,12 @@ unique_ptr<AsyncCallback> AllCallback::create_callback(const char *async_name)
5556
return fn_callback(async_name, functions.front());
5657
}
5758

59+
void AllCallback::mark_ready()
60+
{
61+
ready = true;
62+
fire_if_empty(true);
63+
}
64+
5865
void AllCallback::set_result(Result<> &&r)
5966
{
6067
if (r.is_ok()) return;
@@ -70,6 +77,7 @@ void AllCallback::set_result(Result<> &&r)
7077
void AllCallback::fire_if_empty(bool sync)
7178
{
7279
if (remaining > 0) return;
80+
if (!ready) return;
7381
if (fired) return;
7482
fired = true;
7583

src/nan/all_callback.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,8 @@ class AllCallback
2121

2222
std::unique_ptr<AsyncCallback> create_callback(const char *async_name);
2323

24+
void mark_ready();
25+
2426
void set_result(Result<> &&r);
2527

2628
void fire_if_empty(bool sync);
@@ -36,6 +38,7 @@ class AllCallback
3638
void callback_complete(size_t callback_index, const Nan::FunctionCallbackInfo<v8::Value> &info);
3739

3840
std::unique_ptr<AsyncCallback> done;
41+
bool ready;
3942
bool fired;
4043
size_t total;
4144
size_t remaining;

src/worker/worker_thread.cpp

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,26 @@ Result<> WorkerThread::body()
4242
return platform->listen();
4343
}
4444

45+
Result<Thread::OfflineCommandOutcome> WorkerThread::handle_offline_command(const CommandPayload *payload)
46+
{
47+
Result<OfflineCommandOutcome> r = Thread::handle_offline_command(payload);
48+
if (r.is_error()) return r;
49+
50+
if (payload->get_action() == COMMAND_ADD) {
51+
return ok_result(TRIGGER_RUN);
52+
}
53+
54+
if (payload->get_action() == COMMAND_CACHE_SIZE) {
55+
handle_cache_size_command(payload);
56+
}
57+
58+
if (payload->get_action() == COMMAND_STATUS) {
59+
handle_status_command(payload);
60+
}
61+
62+
return ok_result(OFFLINE_ACK);
63+
}
64+
4565
Result<Thread::CommandOutcome> WorkerThread::handle_add_command(const CommandPayload *payload)
4666
{
4767
Result<bool> r = platform->handle_add_command(

src/worker/worker_thread.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,8 @@ class WorkerThread : public Thread
3030

3131
Result<> body() override;
3232

33+
Result<OfflineCommandOutcome> handle_offline_command(const CommandPayload *payload) override;
34+
3335
Result<CommandOutcome> handle_add_command(const CommandPayload *payload) override;
3436

3537
Result<CommandOutcome> handle_remove_command(const CommandPayload *payload) override;

test/configuration.test.js

Lines changed: 19 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -29,19 +29,29 @@ describe('configuration', function () {
2929
assert.match(contents, /FileLogger opened/)
3030
})
3131

32-
it('configures the worker thread logger', async function () {
33-
await configure({ workerLog: fixture.workerLogFile })
34-
35-
const contents = await fs.readFile(fixture.workerLogFile)
36-
assert.match(contents, /FileLogger opened/)
37-
})
38-
3932
it('fails if the main log file cannot be written', async function () {
4033
await assert.isRejected(configure({ mainLog: badPath }), /No such file or directory/)
4134
})
4235

43-
it('fails if the worker log file cannot be written', async function () {
44-
await assert.isRejected(configure({ workerLog: badPath }), /No such file or directory/)
36+
describe('for the worker thread', function () {
37+
// There's currently no way to *stop* the worker thread, so we can't reliably test it in the stopped state.
38+
39+
describe("after it's started", function () {
40+
it('configures the logger', async function () {
41+
await fixture.watch([], { poll: false }, () => {})
42+
43+
await configure({ workerLog: fixture.workerLogFile })
44+
45+
const contents = await fs.readFile(fixture.workerLogFile)
46+
assert.match(contents, /FileLogger opened/)
47+
})
48+
49+
it('fails if the worker log file cannot be written', async function () {
50+
await fixture.watch([], { poll: false }, () => {})
51+
52+
await assert.isRejected(configure({ workerLog: badPath }), /No such file or directory/)
53+
})
54+
})
4555
})
4656

4757
describe('for the polling thread', function () {

0 commit comments

Comments
 (0)