0

我正在尝试在码头服务器上运行 grunt-yslow 测试。我有server_start一个server_stop任务需要运行 yslow 任务。

child_process.spawn()在内部使用server_start然后运行整个事物的第一种方法使用

grunt.task.run('server_start', 'yslow_test', 'server_stop');

导致终端挂在

Running "server_start" task
Starting java server....
Running "yslow_test:your_target" (yslow_test) task
Command: phantomjs /.../node_modules/grunt-yslow-test/tasks/lib/yslow.js -i grade -f junit http://localhost:8080/cms/s/0/1251659c-efb2-11e2-ad61-002128161462.html

编辑原来这也部分与通过 npm 安装 phantomjs 而不是作为独立二进制文件有关,但是在正确安装二进制文件后,htlm 页面仍然无法加载

使用更复杂的方法来侦听服务器数据事件并且仅在发出服务器的最后一次启动输出后运行 ySlow 会导致一些奇怪的行为(请参阅下面的评论)。(为简洁起见,我删除了所有的 try/catch 块,但从未抛出任何错误)

var server;

grunt.registerTask('runYslow', 'Initialise a test server to run tests against', function() {
    //If I don't include yslow_test here then no normal server startup output is generated. 
    //If I do include yslow_test then the startup output is emitted and then yslow behaviour is bizarrely influenced by which events I listen to on the spawned server process!
    grunt.task.run(['server_start', 'yslow_test']);

});

grunt.registerTask('server_start', 'Start the web server', function () {

    var javaParams = ['-jar', 'target/the.jar', 'server', 'config-local.yml'],
        count = 0;

        server = grunt.util.spawn({
            cmd: 'java',
            args: javaParams
        });

        server.stdout.setEncoding('utf8');

        // If I include this listener somewhere between 13 and 17 events are fired
        // but the contents of read() are always null
        server.stdout.on('readable', function() {  
            console.log(count++, server.stdout.read());
        });

        // If I include this listener 7 events are fired ONLY IF I run yslow_test in the 
        // runYslow task. otherwise absolutely nothing is output. When the events are
        // fired the content of data is the normal startup messages of the jetty server
        server.stdout.on('data', function(chunk) {
            console.log(chunk);
            if (chunk.indexOf('Started SocketConnector@0.0.0.0:8085') > -1) {
                // never run unless yslow_test is already called within runYslow
                grunt.task.run(['yslow_test']);
            }

        });
});

我想我的问题是如何让 grunt 启动和停止非 nodejs 服务器的更广泛的一个特殊情况。

4

1 回答 1

1

这是我的最终解决方案

module.exports = function(grunt) {
    "use strict";

    grunt.registerTask('runYslow', 'Initialise a test server to run tests against', function() {
        try {
            //queue these tasks to happen one after the other
            grunt.task.run('server_start', 'yslow_test', 'server_stop');
        } catch (err) {
            console.log('runYslow: err is', err);
        }
    });

    grunt.registerTask('server_start', 'Start the web server', function () {
        //this needs to not be hard coded - i.e. the version number needs to be got dynamically from somewhere
        var javaParams = ['-jar', 'target/my.jar', 'server', 'config-local.yml'],
            done = this.async(),
            server;
        try {
            grunt.log.write('Starting java server....');

            server = process.env.JETTY_SERVER || grunt.util.spawn({
                cmd: 'java',
                args: javaParams
            });

            server.stdout.setEncoding('utf8');

            server.stdout.on('data', function(chunk) {
                if (chunk.indexOf('the last thing your server logs to confirm startup was successful') > -1) {
                    done();
                }
            });

            process.env.JETTY_SERVER = server;

        } catch (err) {
            console.log('server_start: err is', err);
        }
    });

    grunt.registerTask('server_stop', 'Stop the web server', function () {
        try {
            grunt.log.write('Killing java server....');
            process.kill(process.env.JETTY_SERVER.pid, 'SIGINT');
            delete process.env.JETTY_SERVER;
            grunt.log.write('Killed java server.');
        } catch (err) {
            grunt.log.write('Error whilst killing java server');
        }
    });
};
于 2013-11-05T16:45:26.310 回答