totaljs / framework

Node.js framework
http://www.totaljs.com
Other
4.36k stars 450 forks source link

F.wait in Url injected module not work #657

Closed ckpiggy closed 6 years ago

ckpiggy commented 6 years ago

I inject mongodb module from URL and the server start before database connected.

But if I copy the file in to /modules directory, it works.

petersirka commented 6 years ago

Of course because downloading of the module costs some time.

You need to create a definition file for example wait.js:

F.wait('mongodb');
ckpiggy commented 6 years ago

@petersirka I still got the problem here is my /definitions/wait.js

const F = global.F
console.log('waiting for mognodb', new Date().getTime())
F.wait('mongodb')
console.log('continue ~~~', new Date().getTime())

and I got the logs, it shows the tests run before the database connected

waiting for mognodb 1533919374247
continue ~~~ 1533919374250
====================================================
PID         : 6498
Node.js     : v8.9.1
Total.js    : v2.9.2
OS          : darwin 17.7.0
====================================================
Name        : Total.js
Version     : 1.0.0
Author      :
Date        : 2018-08-11 00:42:54
Mode        : debug
====================================================

http://0.0.0.0:8000/

===================== TESTING ======================

[ TEST: long-run/demo.js ]

Passed ............. long run <long run 0> [0 ms]
Passed ............. next <the next test> [0 ms]

===================== RESULTS ======================

> Passed ......... 2/2
> Failed ......... 0/2

connected to database test
petersirka commented 6 years ago

Ohhh you use tests ... there can be a problem with F.wait()... I'll test it

ckpiggy commented 6 years ago

@petersirka
I also test the example

require('total.js');

F.route('/', function() {
    this.plain('Welcome');
});

F.wait('database');
F.wait('users');
F.wait('system');

setTimeout(function() {
    F.wait('database');
    console.log('database');
}, 3000);

setTimeout(function() {
    F.wait('users');
    console.log('users');
}, 5000);

setTimeout(function() {
    F.wait('system');
    console.log('systems');
}, 7000);

// Run the web server
F.http('debug');

I got the log

====================================================
PID           : 6609
Node.js       : v8.9.1
Total.js      : v3.0.0
OS            : darwin 17.7.0
Memory        : 11.18 MB / 17.96 MB
====================================================
Name          : Total.js
Version       : 1.0.0
Author        :
Date          : 2018-08-11 00:56:21
Mode          : debug
====================================================
Directory     : /Users/ckpiggy/Documents/JS/total_test
node_modules  : /Users/ckpiggy/Documents/JS/total_test/node_modules/total.js/
====================================================

http://0.0.0.0:8000/

database
users
systems

It seems the example is broken, too.

petersirka commented 6 years ago

It works correctly because F.wait() works as a toggle.

// Holds the web server
F.wait('database');

setTimeout(function() {
    // Allows processing in the web server
    F.wait('database');
}, 1000);
ckpiggy commented 6 years ago

@petersirka I thought the log of example should be like this:

database
users
systems
====================================================
PID           : 6609
Node.js       : v8.9.1
Total.js      : v3.0.0
OS            : darwin 17.7.0
Memory        : 11.18 MB / 17.96 MB
====================================================
Name          : Total.js
Version       : 1.0.0
Author        :
Date          : 2018-08-11 00:56:21
Mode          : debug
====================================================
Directory     : /Users/ckpiggy/Documents/JS/total_test
node_modules  : /Users/ckpiggy/Documents/JS/total_test/node_modules/total.js/
====================================================

http://0.0.0.0:8000/

Did I miss understand F.wait ? I thought the server will be hold until the F.wait('systems') unlock the process.

petersirka commented 6 years ago

Web server works with F.wait() but try to create a request in your browser and you will see something like this: image

I hope that you understand this feature :-D

ckpiggy commented 6 years ago

@petersirka Oh I see~~ So the problem is tests start before F.wait completed. Is there any workaround for this ?

petersirka commented 6 years ago

I have fixed it, but you need to install $ npm install total.js@beta. This was a bug :-/