#!/usr/bin/env node // -*- mode: js -*- /* * This Source Code Form is subject to the terms of the Mozilla Public * License, v. 2.0. If a copy of the MPL was not distributed with this * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ /* * Copyright (c) 2018, Joyent, Inc. */ var cmdutil = require('cmdutil'); var moray = require('../lib'); var moraycli = require('../lib/cmd'); var clientOptions, parser, client; var bucket, count; cmdutil.configure({ 'usageMessage': 'reindex objects in a Moray bucket', 'synopses': [ moraycli.commonUsage + ' BUCKET [COUNT]' ] }); clientOptions = {}; count = 100; parser = moraycli.parseCliOptions({ 'argv': process.argv, 'env': process.env, 'errstream': process.stderr, 'extraOptStr': '', 'clientOptions': clientOptions, 'onUsage': cmdutil.usage }); if (parser.optind() >= process.argv.length) cmdutil.usage('missing required arguments: "bucket"'); bucket = process.argv[parser.optind()]; if (parser.optind() + 1 < process.argv.length) { count = parseInt(process.argv[parser.optind() + 1], 10); if (isNaN(count) || count <= 0) { cmdutil.usage('unsupported value for COUNT: "%s"', process.argv[parser.optind() + 1]); } } client = moray.createClient(clientOptions); client.on('error', cmdutil.fail); client.on('connect', function onConnect() { client.getBucket(bucket, function (err, bucketinfo) { if (err) { cmdutil.fail(err); return; } if (!bucketinfo.reindex_active || Object.keys(bucketinfo.reindex_active).length === 0) { console.error('bucket "%s": no reindexing required', bucket); moraycli.cliFinish(cmdutil, client); client.close(); } else { reindexLoop(function (err2) { if (err2) { cmdutil.fail(err2); } moraycli.cliFinish(cmdutil, client); client.close(); }); } }); }); function reindexLoop(cb) { client.reindexObjects(bucket, count, function (err, res) { if (err) { cb(err); return; } if (res.processed === 0) { console.error('bucket "%s": all objects processed', bucket); cb(); } else { console.error('bucket "%s": %d object%s processed (continuing)', bucket, res.processed, res.processed == 1 ? '' : 's'); reindexLoop(cb); } }); }