Skip to content

Commit 2e9b866

Browse files
committed
staging 0.43
1 parent c1e3df4 commit 2e9b866

30 files changed

+193
-124
lines changed

App/index.js

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,13 @@
1-
process.on('SIGINT', shutdownApp)
2-
require('../package')
3-
.done(function(rootConfig){
1+
process.on('SIGINT', shutdownApp);
2+
require('../root-app')
3+
.done(function(){
4+
45
global.app = atma
56
.server
67
.Application({
78
base: __dirname,
89
configs: [
9-
rootConfig.toJSON(),
10+
'../config/**.yml',
1011
'server/config/**.yml'
1112
]
1213
})
@@ -15,7 +16,7 @@ require('../package')
1516

1617
function startApp(app){
1718
var connect = require('connect'),
18-
port = process.env.PORT || app.config.port,
19+
port = app.config.port,
1920
// Application library Modules: @see ./server/config/env/server.yml
2021
Lib = app.lib,
2122

@@ -34,7 +35,10 @@ function startApp(app){
3435
if (app.config.embedWorker)
3536
Lib.Worker.connect(app.config);
3637

37-
logger.log('Listen', port);
38+
logger.log('Queue server started on', port);
39+
40+
if (process.send)
41+
process.send('ok');
3842
}
3943
function shutdownApp(){
4044
process.exit(0);

Cli/bin/server.js

Lines changed: 37 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
/* global config, threads */
12
require('atma-libs/globals');
23
process.on('exit', shutdownSelf);
34
process.on('SIGINT', shutdownSelf);
@@ -55,15 +56,14 @@ function server_Start(){
5556
startWorker(workerStarted);
5657
}
5758
function workerStarted(error) {
58-
if (error)
59+
if (error)
5960
send(error);
60-
if (--count > -1)
61+
62+
if (--count > 0)
6163
return;
6264

6365
config
64-
.$write({
65-
pid: config.pid
66-
})
66+
.$write({ pid: config.pid }, false)
6767
.fail(disconnect)
6868
.done(function(){
6969
var msg = logger.formatMessage(
@@ -72,7 +72,7 @@ function server_Start(){
7272
);
7373
config.pid.workers.forEach(function(pid, index){
7474
msg += logger.formatMessage(
75-
'Worker green<%s> process: bold<%s>'.color
75+
'\nWorker green<%s> process: bold<%s>'.color
7676
, index
7777
, pid
7878
);
@@ -87,17 +87,19 @@ function server_Start(){
8787
}
8888
function startServer(cb) {
8989
fork('App/index.js', function(error, thread){
90-
if (error) return cb(error);
90+
if (thread)
91+
config.$set('pid.server', thread.child.pid);
9192

92-
config.$set('pid.server', thread.child.pid);
93+
if (error) return cb(error);
9394
cb();
9495
});
9596
}
9697
function startWorker(cb) {
9798
fork('Worker/index.js', function(error, thread){
99+
if (thread)
100+
config.$get('pid.workers').push(thread.child.pid);
101+
98102
if (error) return cb(error);
99-
100-
config.$get('pid.workers').push(thread.child.pid);
101103
cb();
102104
});
103105
}
@@ -133,15 +135,21 @@ function fork(path, cb){
133135
silent: true,
134136
killSignal: 'SIGINT',
135137
killTree: false,
138+
detached: true,
136139
spawnWith: {
137-
detached: true
138-
}
140+
detached: true,
141+
stdio: ['ipc'],
142+
},
143+
options: [
144+
'--release'
145+
]
139146
});
140147
threads.push(thread);
141148
thread
142149
.on('start', function(){
143-
144-
cb(null, thread);
150+
//thread.child.on('message', function(msg){
151+
// ...
152+
//});
145153
})
146154
.on('stop', function(error){
147155
send('stopped'.bold);
@@ -154,15 +162,19 @@ function fork(path, cb){
154162
.on('restart', function(a, b) {
155163
send('Failed to start: ' + path);
156164
})
157-
.on('exit:code', function(code, x) {
158-
//send('Forever detected script exited with code ' + code);
165+
.on('message', function(message){
166+
if (message === 'ok') {
167+
cb(null, thread);
168+
return;
169+
}
159170

171+
cb(message, thread);
160172
})
161173
.on('stdout', function(data){
162-
send(path + ' [stdout]: ' + String(data));
174+
send('[stdout]: ' + path + String(data));
163175
})
164176
.on('stderr', function(data){
165-
send(path + ' [stderr]: ' + String(data));
177+
send('[stderr]: ' + path + String(data));
166178
})
167179
;
168180

@@ -179,6 +191,13 @@ function send() {
179191
console.log(msg);
180192
}
181193
function disconnect(error) {
194+
195+
threads.forEach(function(thread){
196+
thread
197+
.removeAllListeners('stdout')
198+
.removeAllListeners('stderr');
199+
});
200+
182201
if (error == null) {
183202
send('ok');
184203
return;

Cli/index.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
require('../package')
1+
require('../root-app')
22
.done(function(){
33

44
include

Cli/src/utils/Api.es6

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ include
22
.js('./socket.es6')
33
.use('RestApi')
44
.done(function(resp, Api){
5-
65
include.exports = {
76
get: function(url){
87
return httpRun(url, 'get');
@@ -33,6 +32,14 @@ include
3332
if (error)
3433
return dfr.reject(error);
3534

35+
if (typeof body === 'string') {
36+
try {
37+
body = JSON.parse(body);
38+
} catch(e){
39+
dfr.reject('Invalid json: ' + body);
40+
return;
41+
}
42+
}
3643
dfr.resolve(body);
3744
});
3845
return dfr;

Cli/src/utils/forever.es6

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,12 @@ function fork(appdir, script, args) {
3636
dfr.reject('');
3737
return;
3838
}
39+
40+
if (/^\[std/.test(msg)) {
41+
logger.log(msg);
42+
return;
43+
}
44+
3945
logger.log(msg);
4046
});
4147

Cli/test/config.js

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,14 @@
11
module.exports = {
22
exec: 'node',
3-
env: 'Cli.js::Cli',
3+
env: [
4+
'../root-app.js',
5+
'Cli.js::Cli',
6+
],
47
tests: 'test/*.test',
58

69
$config: {
710
$before: function(done){
11+
812
var Mongo = Class.MongoStore;
913
Mongo
1014
.settings(app.config.mongo);

Cli/test/task.test

Lines changed: 21 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,19 @@
11
/* global Cli */
2+
var file_PATH = '../test-cli-write.txt';
23
UTest({
4+
$config: {
5+
timeout: 4000
6+
},
7+
$before: function(){
8+
io.File.remove(file_PATH);
9+
},
10+
$after: function(){
11+
io.File.remove(file_PATH);
12+
},
313
'start server': function(){
414
return Cli.process('server start');
515
},
16+
617
'create': function(){
718
return Cli.process([
819
'task',
@@ -12,9 +23,9 @@ UTest({
1223
'-name',
1324
'First',
1425
'-trigger',
15-
'in 400 ms',
26+
'in 200 ms',
1627
'-exec.script',
17-
'io.File.write(\'my-test.txt\', String(new Date))'
28+
'io.File.write(\'test-cli-write.txt\', \'Baz \' + new Date(2014,0,1).getFullYear())'
1829
]);
1930
},
2031
'list': function(){
@@ -30,7 +41,14 @@ UTest({
3041
}])
3142
});
3243
},
44+
'ensure write': function(done){
45+
setTimeout(() => {
46+
eq_(io.File.exists(file_PATH), true);
47+
eq_(io.File.read(file_PATH), 'Baz 2014');
48+
done();
49+
}, 1000);
50+
},
3351
'stop server': function(){
3452
return Cli.process('server stop');
3553
}
36-
})
54+
});

Queue/Queue.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ include
22
.js(
33
'../Utils/Utils.js',
44
'../Model/Model.js',
5+
'src/logger.js::Logger',
56
'src/*.es6.package::Queue'
67
)
78
.done(function(resp){

Queue/src/Server.es6

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
include
22
.js('WorkerSocket.es6', 'TaskQueue.es6')
3-
.done(function(resp){
3+
.use('Logger')
4+
.done(function(resp, log){
45

56
include.exports = {
67
listen: function(httpServer){
@@ -27,15 +28,15 @@ include
2728

2829
function emit(){
2930
if (server_io == null) {
30-
logger.error('Queue Socket | Socket server is not yet started');
31+
log.error('Queue Socket | Socket server is not yet started');
3132
return;
3233
}
3334

3435
var socket = server_io.of(socket_NAMESPACE),
3536
clients = socket.clients();
3637

3738
if (clients.length === 0) {
38-
logger.error('Queue Socket | No workers');
39+
log.error('Queue Socket | No workers');
3940
return;
4041
}
4142

Queue/src/TaskQueue.es6

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
include
2-
.use('Model')
2+
.use('Model', 'Logger')
33
.js('TaskFactory.es6', 'TaskHistory.es6')
4-
.done(function(resp, Model){
4+
.done(function(resp, Model, log){
55

66
var TaskFactory = resp.TaskFactory,
77
TaskHistory = resp.TaskHistory
@@ -24,11 +24,11 @@ include
2424

2525
Self: {
2626
add: function(task){
27-
logger.log('TaskQueue| Push `bold<%s>`'.color, task.name);
27+
log('TaskQueue | Push `bold<%s>`'.color, task.name);
2828

2929
var taskAdded = () => {
30-
logger.log(
31-
'TaskQueue: Task, ready for execution `bold<%s>`'.color
30+
log(
31+
'TaskQueue | Task, ready for execution `bold<%s>`'.color
3232
, task.name
3333
);
3434
this.trigger('hasNewTasks', this);
@@ -52,7 +52,7 @@ include
5252
.shift()
5353
.del()
5454
.fail(error =>
55-
logger.error('Queuedtask | Del error', error)
55+
log.error('QueuedTask | Del error', error)
5656
);
5757
TaskHistory
5858
.add(scheduled._task, worker)
@@ -75,7 +75,7 @@ include
7575
return;
7676
}
7777

78-
logger.warn(
78+
log.error(
7979
'There are still tasks in the queue.',
8080
'Last application-run terminated unexpectedly.'
8181
);
@@ -100,7 +100,7 @@ include
100100
dfr.resolve(this);
101101
},
102102
onError = (message, error) => {
103-
logger.error(message, error);
103+
log.error(message, error);
104104
dfr.reject(error);
105105
};
106106
this

0 commit comments

Comments
 (0)