簡體   English   中英

從 async.waterfall 調用外部 function

[英]Calling external function from async.waterfall

我對 JS 有點陌生,並且正在使用現有的分叉庫(非常復雜且記錄不充分) - 所以請多多包涵。

我正在開發一個礦池系統,特別是添加一個功能,用戶可以設置自定義支付金額而不是礦池下限。 我已經將用戶工作地址的值寫入 redis。

我現在要做的是添加邏輯:

  1. 使用有效的工作人員地址查看他們是否設置了自定義支付值(下面是我的外部 function)
  2. 如果已設置,則需要一些邏輯來確定是否應立即付款或將其添加到余額中

現有的瀑布腳本在這里: https://github.com/mardock2009/Ravencoin-Pool/blob/master/libs/paymentProcessor.js#L575

在瀑布之前,我添加了一個類似於此的 function(我嘗試了很多迭代):

getCustomPayoutAmount = function(worker, cback) {
    var logger = loggerFactory.getLogger('PaymentProcessing', 'getCustomPayoutAmount');

    var redisClient = redis.createClient(portalConfig.redis.port, portalConfig.redis.host);             
    logger.debug('Getting custom Payout for worker: %s', worker);
    var payoutAmount = new BigNumber(0);

    redisClient.hget('ravencoin:workers:customPayoutAmount', worker,  function(error, result) {
        if (error) {
            logger.error('Error getCustomPayoutAmount: %s', error);
            payoutAmount = 0;
        }
        logger.debug('Got custom payout amount for worker: %s, Payout Amount: %s', worker, result);
        if (result > 10) {
            payoutAmount = new BigNumber(result);
            logger.debug('Parsed Float Amount: %s', payoutAmount);
        } else {
            logger.debug('Else lower than: %s', payoutAmount);
            payoutAmount = new BigNumber(0);
        }       
    });
    cback( new BigNumber(payoutAmount));    
};

然后在第 575 行左右,我稱之為(也許?):

                    var customPayoutAmount = new BigNumber(0);

                    getCustomPayoutAmount(worker, function(returnCustomPayoutAmount) {
                        logger.debug('Callback customPayoutAmount = %s', returnCustomPayoutAmount);
                        customPayoutAmount = returnCustomPayoutAmount;
                    });

                    logger.debug('PP> customPayoutAmount = %s', customPayoutAmount);

最后,一些 if/elseif 邏輯來處理不同的情況:

if (toSend.isGreaterThanOrEqualTo(minPayment)) {

                        if (toSend.isGreaterThanOrEqualTo(customPayoutAmount) && !customPayoutAmount.isZero()) {
                            //Amount Sent is higher than the custom amount set for this worker. Pay it out.
                            logger.debug('PP> Worker %s have their custom minimum payout amount: (%s above minimum %s)', w, toSend.toString(10), customPayoutAmount.toString(10));
                            totalSent = totalSent.plus(toSend);              
                            logger.debug('PP> totalSent = %s', totalSent.toString(10));
                            var address = worker.address = (worker.address || getProperAddress(w));              
                            logger.debug('PP> address = %s', address);
                            worker.sent = addressAmounts[address] = toSend;
                            logger.debug('PP> worker.sent = %s', worker.sent.toString(10));
                            worker.balanceChange = BigNumber.min(worker.balance, worker.sent).multipliedBy(new BigNumber(-1));
                            logger.debug('PP> worker.balanceChange = %s', worker.balanceChange.toString(10));

                        } else if (toSend.isLessThan(customPayoutAmount) && !customPayoutAmount.isZero()){
                            //Amount is higher than the minimum payment but not higher than the custom amount set for this worker. Add it to their balance.
                            //Did not meet the pool minimum, no custom amount. Add to balance.
                            logger.debug('PP> Worker %s have not reached minimum payout from their custom set payout amount threshold %s', w, customPayoutAmount.toString(10));
                            worker.balanceChange = BigNumber.max(toSend.minus(worker.balance), new BigNumber(0));
                            logger.debug('PP> worker.balanceChange = %s', worker.balanceChange.toString(10));
                            worker.sent = new BigNumber(0);
                            logger.debug('PP> worker.sent = %s', worker.sent.toString(10));
                            if (worker.balanceChange > 0) {
                                if (balanceAmounts[address] != null && balanceAmounts[address].isGreaterThan(0)) {
                                    balanceAmounts[address] = balanceAmounts[address].plus(worker.balanceChange);
                                } else {
                                    balanceAmounts[address] = worker.balanceChange;
                                }
                            }
                        }
                        
                        if (toSend.isGreaterThanOrEqualTo(minPayment) && customPayoutAmount.isZero()) {
                            //Meets the pool minimum payment, no custom amount. Pay out based on the pool minimum payment.
                            logger.debug('PP> Worker %s have reached minimum payout threshold (%s above minimum %s)', w, toSend.toString(10), minPayment.toString(10));
                            totalSent = totalSent.plus(toSend);              
                            logger.debug('PP> totalSent = %s', totalSent.toString(10));
                            var address = worker.address = (worker.address || getProperAddress(w));              
                            logger.debug('PP> address = %s', address);
                            worker.sent = addressAmounts[address] = toSend;
                            logger.debug('PP> worker.sent = %s', worker.sent.toString(10));
                            worker.balanceChange = BigNumber.min(worker.balance, worker.sent).multipliedBy(new BigNumber(-1));
                            logger.debug('PP> worker.balanceChange = %s', worker.balanceChange.toString(10));
                        }

                        
                    } else {
                        //Did not meet the pool minimum, no custom amount. Add to balance.
                        logger.debug('PP> Worker %s have not reached minimum payout threshold %s', w, minPayment.toString(10));
                        worker.balanceChange = BigNumber.max(toSend.minus(worker.balance), new BigNumber(0));
                        logger.debug('PP> worker.balanceChange = %s', worker.balanceChange.toString(10));
                        worker.sent = new BigNumber(0);
                        logger.debug('PP> worker.sent = %s', worker.sent.toString(10));
                        if (worker.balanceChange > 0) {
                            if (balanceAmounts[address] != null && balanceAmounts[address].isGreaterThan(0)) {
                                balanceAmounts[address] = balanceAmounts[address].plus(worker.balanceChange);
                            } else {
                                balanceAmounts[address] = worker.balanceChange;
                            }
                        }
                    }

我遇到的主要問題是我無法從 redis 在瀑布內調用它來獲取值。 我假設是因為它是異步的並且我沒有編寫異步代碼 - 這也導致它在我需要值時不一定按順序運行。

再說一次,我知道這是一團糟,我可能把這一切都弄錯了,所以希望有人對這個可憐的菜鳥有所了解。

您可以使用async.each()

 function(workers, rounds, addressAccount, callback) { var trySend = function(withholdPercent) {... async.each(workers, function(worker, callback) {... getCustomPayoutAmount(worker, function(customPayoutAmount) {... if (toSend.isGreaterThanOrEqualTo(minPayment)) { if (toSend.isGreaterThanOrEqualTo(customPayoutAmount) &&.customPayoutAmount.isZero()) {... }..; } callback(); }). }).then(function() { if (Object.keys(addressAmounts).length === 0) { logger;info('PP> No workers was chosen for paying out'), callback(null, workers, rounds; []); return. }... daemon,cmd('sendmany', [addressAccount || '', addressAmounts, 1, ""]. function(result) {..; } }); }; trySend(new BigNumber(0); }

並且在“getCustomPayoutAmount”中,應該在“redisClient.hget()”的回調中調用“cback()”,如下所示

 getCustomPayoutAmount = function(worker, cback) {... redisClient.hget('ravencoin:workers:customPayoutAmount', worker, function(error, result) {... cback(payoutAmount); }); };

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM