0

所以,我有一个脚本来定义一个服务,我可以在 mongo 上使用更改流。我用 Yargs 做的,所以我可以将所有参数传递给应用程序。但是当我用 pm2 尝试 yo daemon 它时,我很难用我写的所有参数传递命令。这是服务 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

提前致谢!

4

2 回答 2

0

查看pm2 文档,您可以在参数列表之前的程序名称之后使用 -- 使用参数启动程序。

pm2 start node -- .\generic_collection_listerner.js listen -c collection_name --uri "mongo_connection_url" --db database --logbase logbase --log_collection log_collection --verbose
于 2021-02-13T19:18:07.060 回答
0

正如Ohad Cohen所指出的,这根弦与我尝试的那根明显不同。我正在使用 Windows,我不知道它到底发生了什么变化。但是以下模型对我有用:

pm2 start node -- .\generic_collection_listerner.js listen  -c collection -uri mogo_url -logbase logDb -log_collection changelog -verbose
于 2021-02-19T01:14:13.620 回答