尝试使用 PM2 设置使用 YARGS 构建的服务

Trying to setup a service built with YARGS using PM2

所以,我有一个脚本来定义我可以在 mongo 上使用更改流的服务。我用 Yargs 做到了,所以我可以将所有参数传递给应用程序。但是当我用 pm2 尝试守护进程时,我很难用我写的所有参数传递命令。 这是服务 yargs 设置:

const yargs = require('yargs');
const mongo = require("mongodb").MongoClient;
const fs = require('fs');
const BSON = require('bson');

const argv = yargs
.command(
    'listen', 'Listen to a collection for changes and save it to another',{
        collection :{
            description : 'the collection you want to listen to',
            alias : 'c',
            type: 'string',
        },
        database:{
            description: 'the database you want to listen to',
            alias : 'db',
            type: 'string',
        },
        uri:{
            description: 'the connection string for your mongodb instance',
            alias : 'uri',
            type: 'string',
        },
        logbase:{
            description: 'the database where the changes will be kept',
            alias: 'logdb',
            type: 'string',
        },
        log_collection:{
            description: 'the collection where the changes will be kept',
            alias: 'logc',
            type: 'string',
        }
    })
    .option('verbose', {
        alias : 'v',
        description: 'Writes on the console the information',
        type: 'boolean',
    })
    .help()
    .alias('help', 'h')
    .argv;

if(argv._.includes('listen')){
    if(!argv.uri || !argv.collection || !argv.db || !argv.log_collection || !argv.logbase){
        console.log("Missing parameters!")
        if(!argv.uri) console.log("--uri is missing!")
        if(!argv.collection) console.log("--collection is missing!")
        if(!argv.db) console.log("--db is missing!")
        if(!argv.log_collection) console.log("--log_collection is missing!")
        if(!argv.logbase) console.log("--log_base is missing!")
    }
    else{
        const uri = argv.uri;
        const coll = argv.collection;
        const database = argv.db;
        const logbase = argv.logbase;
        const log_collection = argv.log_collection;
        const verbose = argv.verbose;
        const token = token_reader(logbase, log_collection, verbose)
        if(token){
            //if there is a resume token
            resume_with_token(token ,uri, database, coll, argv.verbose, logbase, log_collection)
        }else { 
            listen (uri, database, coll, argv.verbose, logbase, log_collection)
        }
    }
} else {
    console.log('Commnad not listed')
}

所以基本上我的控制台中有 运行 的以下行:

node .\generic_collection_listerner.js listen -c collection_name --uri "mongo_connection_url" --db database --logbase logbase --log_collection log_collection --verbose

提前致谢!

看看pm2 docs,你可以在参数列表前的程序名之后使用 -- 启动带参数的程序。

pm2 start node -- .\generic_collection_listerner.js listen -c collection_name --uri "mongo_connection_url" --db database --logbase logbase --log_collection log_collection --verbose

正如Ohad Cohen指出的那样,字符串肯定与我试过的不同。 我正在使用 windows,我不知道它究竟改变了什么。但以下模型对我有用:

pm2 start node -- .\generic_collection_listerner.js listen  -c collection -uri mogo_url -logbase logDb -log_collection changelog -verbose