All files / qlobber-fsq/lib process_all_getdents.js

92.36% Statements 133/144
86.95% Branches 20/23
100% Functions 5/5
92.36% Lines 133/144

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 1461x 1x 1x 1x 1x 1x 1x 750x 750x 750x 750x 750x 750x 750x 750x 750x 750x 750x 750x 191637x 191637x 191637x 191637x 750x 750x 2319380x 2319380x 2319380x 2319380x 2319380x 2319380x 2319380x 2319380x 2319380x 2319380x 2319380x 2319380x 1568724x 1568724x 2319380x 2319380x 2319380x 2319380x 2319380x 2319380x 2319380x 2319380x 2319380x 2319380x 2319380x 2319380x 2319380x 2319380x 2319380x 2319380x 2319380x 2451800x 2451800x 2451800x 2451800x 2451800x 2451800x 2319344x 2319344x 2319344x 2451800x 2319380x 2319380x 132420x 132420x 132420x 132420x 2319380x 2319380x 2319380x 2319380x 2319380x 2319380x 2319380x 2319380x 2319380x 2319380x 2319380x 2319380x 2319380x 2319380x 2319380x 2319380x 2319380x 4771203x 4771203x 132443x 132443x 132443x 4771203x 2319380x 2319380x 2319380x 2319380x 2319380x 2319380x 2319380x 2319380x 2319380x 2319380x 2319380x                 2319380x 2319380x 2319380x 2319380x       2319380x 2319380x 2319380x 2319380x 2319380x 750x 750x 750x 742x 742x 742x 189594x 189594x 189594x 5x 5x 5x 189594x 742x 750x 750x 750x 1x    
const util = require('util'),
      path = require('path'),
      async = require('async'),
      Getdents = require('getdents').Getdents,
      sleep = util.promisify(setTimeout);
 
module.exports = function (size,
                           num_buckets,
                           delivered,
                           pending,
                           make_fname_handler)
{
    const ths = this,
          bucket_getdents = [],
          popen = util.promisify(this._fs.open),
          pclose = util.promisify(this._fs.close);
 
    for (let count = 0; count < num_buckets; count += 1)
    {
        let getdents = new Getdents(size);
        getdents.active = false;
        bucket_getdents[count] = getdents;
    }
 
    let r = async function (bucket,
                            bucket_fmt,
                            extra_matcher,
                            cache,
                            only_check_expired,
                            cb)
    {
        let d = cache ? delivered.get(bucket) : null,
            d2 = cache ? new Set() : null,
            pending2 = [];
 
        if (cache)
        {
            delivered.set(bucket, d2);
        }
 
        pending.set(bucket, pending2);
 
        let queue = async.queue(make_fname_handler(d,
                                                   d2,
                                                   pending2,
                                                   bucket_fmt,
                                                   extra_matcher,
                                                   only_check_expired),
                                ths._message_concurrency);
 
        let called = false;
        let done = false;
        let count = 0;
        let err = null;
 
        function check()
        {
            if ((ths._error(err) ||
                 (done && (count === 0))) &&
                !ths._chkstop() &&
                !called)
            {
                called = true;
                cb();
            }
        }
 
        function processed()
        {
            count--;
            check();
        }
 
        if (ths._chkstop()) { return; }
 
        let getdents = bucket_getdents[bucket];
 
        try
        {
            getdents.active = true;
 
            let fd = await popen(ths._msg_dir + path.sep + bucket_fmt, 'r');
 
            try
            {
                getdents.reset(fd);
 
                for await (let unused_ of getdents)
                {
                    if (getdents.type !== Getdents.DT_DIR)
                    {
                        count++;
                        queue.push(getdents.name, processed);
                    }
                }
            }
            finally
            {
                while (true) // eslint-disable-line no-constant-condition
                {
                    try
                    {
                        await pclose(fd);
                        break;
                    }
                    catch (ex)
                    {
                        if (!ths._try_again(ex))
                        {
                            throw ex; // eslint-disable-line no-unsafe-finally
                        }
                    }
                    await sleep(ths._retry_interval);
                }
                done = true;
            }
        }
        catch (ex)
        {
            err = ex;
        }
        finally
        {
            getdents.active = false;
            check();
        }
    };
 
    r.stop = async function (cb)
    {
        for (let count = 0; count < num_buckets; count += 1)
        {
            let getdents = bucket_getdents[count];
            if (getdents.active)    
            {
                await sleep(ths._retry_interval);
                count -= 1;
            }
        }
        cb();
    };
 
    return r;
};