Skip to content

Commit 521f0b2

Browse files
committed
fix the worker process
1 parent 5f13f70 commit 521f0b2

1 file changed

Lines changed: 69 additions & 33 deletions

File tree

bin/worker

Lines changed: 69 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,23 @@
11
#!/usr/bin/env node
2+
/*!
3+
* Copyright (C) 2017 Glayzzle (BSD3 License)
4+
* @authors https://github.com/glayzzle/php-reflection/graphs/contributors
5+
* @url http://glayzzle.com
6+
*/
27
'use strict';
38

9+
var fs = require('fs');
10+
var path = require('path');
411
var Repository = require('../src/repository');
512
var stack = [];
613
var working = false;
714

15+
// sends a response to master
16+
function reply(type, data) {
17+
data.type = type;
18+
process.send(JSON.stringify(data));
19+
}
20+
821
// received a work request
922
process.on('message', function(data) {
1023
stack.push(JSON.parse(data));
@@ -15,45 +28,68 @@ process.on('message', function(data) {
1528

1629
// end of work
1730
process.on('disconnect', function() {
18-
process.exit(1);
31+
process.kill();
1932
});
2033

2134
// do some work
2235
function work() {
23-
// got work ?
24-
if (stack.length === 0) {
25-
working = false;
26-
return;
27-
}
36+
// got work ?
37+
if (stack.length === 0) {
38+
working = false;
39+
return;
40+
}
2841

29-
// lets go !
30-
working = true;
31-
var data = stack.pop();
42+
// lets go !
43+
working = true;
44+
var data = stack.pop();
3245

33-
console.log('Start >> ', data);
34-
// is parameters OK ?
35-
if (!data.filename || !data.options) {
36-
return process.nextTick(work);
37-
}
46+
// is parameters OK ?
47+
if (!data.filename || !data.options) {
48+
return process.nextTick(work);
49+
}
3850

39-
// init a new repository
40-
process.send(JSON.stringify({
41-
type: 'start',
42-
filename: data.filename
43-
}));
44-
45-
// init a new repository
46-
try {
47-
data.options.forkWorker = false;
48-
var repo = new Repository(data.options);
49-
} catch(e) {
50-
process.send(JSON.stringify({
51-
type: 'reject',
52-
filename: data.filename,
53-
error: e.message
54-
}));
55-
}
51+
// init a new repository
52+
reply('start', {
53+
filename: data.filename,
54+
directory: data.directory
55+
});
5656

57-
// release loop for checking message
58-
process.nextTick(work);
57+
// init a new repository
58+
try {
59+
data.options.forkWorker = false;
60+
var repo = new Repository(data.directory, data.options);
61+
fs.stat(path.resolve(data.directory, data.filename), function(err, stat) {
62+
if (err) {
63+
reply('reject', {
64+
filename: data.filename,
65+
directory: data.directory,
66+
error: err.message
67+
});
68+
process.nextTick(work);
69+
} else {
70+
repo.parse(data.filename, null, stat).then(function(file) {
71+
reply('done', {
72+
filename: data.filename,
73+
directory: data.directory,
74+
result: file.export()
75+
});
76+
process.nextTick(work);
77+
}, function(err) {
78+
reply('reject', {
79+
filename: data.filename,
80+
directory: data.directory,
81+
error: err.stack
82+
});
83+
process.nextTick(work);
84+
});
85+
}
86+
});
87+
} catch(e) {
88+
reply('reject', {
89+
filename: data.filename,
90+
directory: data.directory,
91+
error: e.stack
92+
});
93+
process.nextTick(work);
94+
}
5995
}

0 commit comments

Comments
 (0)