ssoma: add --cron option to sync
authorEric Wong <e@80x24.org>
Wed, 16 Apr 2014 19:45:51 +0000 (19:45 +0000)
committerEric Wong <e@80x24.org>
Wed, 16 Apr 2014 20:05:54 +0000 (20:05 +0000)
Encourages users to add "ssoma sync --cron" to their crontabs
and reduce load spikes.

ssoma

diff --git a/ssoma b/ssoma
index 1f68f6bef33adf66b5d03418792fd39d49581e6c..b6332fd0ff3000b91f47b8fbf995cfd13706a823 100755 (executable)
--- a/ssoma
+++ b/ssoma
@@ -12,6 +12,7 @@ use File::Path qw/make_path/;
 use File::Temp qw/tempfile/;
 use File::Spec qw//;
 use Email::LocalDelivery;
 use File::Temp qw/tempfile/;
 use File::Spec qw//;
 use Email::LocalDelivery;
+use constant CRON_RAND_DELAY => 60; # adjust as necessary
 Getopt::Long::Configure("require_order", "pass_through");
 our %opts;
 GetOptions(
 Getopt::Long::Configure("require_order", "pass_through");
 our %opts;
 GetOptions(
@@ -34,6 +35,7 @@ our %cmd = (
        "sync" => {
                doc => "sync target(s) for existing LISTNAME",
                arg => "[LISTNAME] [TARGET]",
        "sync" => {
                doc => "sync target(s) for existing LISTNAME",
                arg => "[LISTNAME] [TARGET]",
+               opt => { "cron" => \$opts{cron} }
        },
        "cat" => {
                doc => "show a message by Message-ID",
        },
        "cat" => {
                doc => "show a message by Message-ID",
@@ -50,6 +52,9 @@ my $cmd_sub = eval {
        no strict 'refs';
        *{"cmd_$cmd"};
 } or die "BUG: $cmd not implemented\n";
        no strict 'refs';
        *{"cmd_$cmd"};
 } or die "BUG: $cmd not implemented\n";
+if (my $opt = $cmd{$cmd}->{opt}) {
+       GetOptions(%$opt) or usage(1);
+}
 
 $cmd_sub->(@ARGV);
 exit 0;
 
 $cmd_sub->(@ARGV);
 exit 0;
@@ -57,7 +62,7 @@ exit 0;
 sub usage {
        my ($cmd, $exit) = @_;
        my $fd = $exit ? \*STDERR : \*STDOUT;
 sub usage {
        my ($cmd, $exit) = @_;
        my $fd = $exit ? \*STDERR : \*STDOUT;
-       print $fd "Usage: ssoma <command> [options] [arguments]\n";
+       print $fd "Usage: ssoma [opts] <command> [command-opts] [args]\n";
 
        print $fd "Available commands:\n" unless $cmd;
 
 
        print $fd "Available commands:\n" unless $cmd;
 
@@ -77,7 +82,7 @@ sub usage {
                        # prints out arguments as they should be passed:
                        my $x = s#[:=]s$## ? '<arg>' :
                                (s#[:=]i$## ? '<num>' : '');
                        # prints out arguments as they should be passed:
                        my $x = s#[:=]s$## ? '<arg>' :
                                (s#[:=]i$## ? '<num>' : '');
-                       print $fd ' ' x 21, join(', ', map { length $_ > 1 ?
+                       print $fd ' ' x 14, join(', ', map { length $_ > 1 ?
                                                        "--$_" : "-$_" }
                                                split /\|/, $_)," $x\n";
                }
                                                        "--$_" : "-$_" }
                                                split /\|/, $_)," $x\n";
                }
@@ -184,6 +189,9 @@ sub foreach_list {
 
 sub cmd_sync {
        my ($listname, @targets) = @_;
 
 sub cmd_sync {
        my ($listname, @targets) = @_;
+       if ($opts{cron}) {
+               sleep(rand(CRON_RAND_DELAY));
+       }
        if (defined $listname) {
                check_listname($listname);
                do_sync("$ENV{SSOMA_HOME}/$listname.git", \@targets);
        if (defined $listname) {
                check_listname($listname);
                do_sync("$ENV{SSOMA_HOME}/$listname.git", \@targets);