Skip to content

Instantly share code, notes, and snippets.

@jasonhancock
Last active October 5, 2017 23:38
Show Gist options
  • Save jasonhancock/4503759 to your computer and use it in GitHub Desktop.
Save jasonhancock/4503759 to your computer and use it in GitHub Desktop.
A script to generate a progress bar tracking Redis' progress loading the data files on startup
#!/usr/bin/perl
# Copyright (c) 2012 Jason Hancock <[email protected]>
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is furnished
# to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in all
# copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
# THE SOFTWARE.
use strict;
use warnings;
use Getopt::Std;
use Redis;
use Term::ProgressBar;
my %options=();
getopts("h:p:",\%options);
my $host = defined($options{h}) ? $options{h} : 'localhost';
my $port = defined($options{p}) ? $options{p} : 6379;
my $redis = Redis->new(
server => "$host:$port",
reconnect => 2, # try to connect for 2 seconds total
every => 100, # attempt to connect every 100ms
);
my $progress = Term::ProgressBar->new ({count => 100});
my $loading;
my $so_far = 0;
do {
my $info = $redis->info();
$loading=$info->{'loading'};
if($loading) {
$so_far = int($info->{'loading_loaded_perc'});
} else {
$so_far = 100;
}
$progress->update($so_far);
if($loading) {
sleep(1);
}
} while($loading);
print "\n\n";
=head1 NAME
B<redis_loading> - A script to display a progress bar showing redis' progress
loading the rdb or aof files from disk
=head1 SYNOPSIS
redis_loading [-h host] [-p port]
=head1 DESCRIPTION
This script connects to the redis server, runs the INFO command and displays a
progress bar showing Redis' progress in loading the rdb or aof files from disk
on startup.
=over 4
=item B<-h>
Optional. Specify the host or IP to connect to. Defaults to localhost
=item B<-p>
Optional. What port to connect to. Defaults to 6379
=back
=head1 EXAMPLES
C<redis_loading -h redis.example.com -p 6379>
Attempts to connect to the redis instance at redis.example.com:6379
C<redis_loading>
Attempts to connect to the redis instance at localhost:6379
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment