From: Eric Wong Date: Wed, 16 Apr 2014 19:45:51 +0000 (+0000) Subject: ssoma: add --cron option to sync X-Git-Tag: v0.0.0~10 X-Git-Url: http://git.tremily.us/?p=ssoma-mda.git;a=commitdiff_plain;h=ed2adfe7d6ec00a356ef1ba8a0e799c433b4ad68 ssoma: add --cron option to sync Encourages users to add "ssoma sync --cron" to their crontabs and reduce load spikes. --- diff --git a/ssoma b/ssoma index 1f68f6b..b6332fd 100755 --- 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 constant CRON_RAND_DELAY => 60; # adjust as necessary 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]", + opt => { "cron" => \$opts{cron} } }, "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"; +if (my $opt = $cmd{$cmd}->{opt}) { + GetOptions(%$opt) or usage(1); +} $cmd_sub->(@ARGV); exit 0; @@ -57,7 +62,7 @@ exit 0; sub usage { my ($cmd, $exit) = @_; my $fd = $exit ? \*STDERR : \*STDOUT; - print $fd "Usage: ssoma [options] [arguments]\n"; + print $fd "Usage: ssoma [opts] [command-opts] [args]\n"; 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$## ? '' : (s#[:=]i$## ? '' : ''); - print $fd ' ' x 21, join(', ', map { length $_ > 1 ? + print $fd ' ' x 14, join(', ', map { length $_ > 1 ? "--$_" : "-$_" } split /\|/, $_)," $x\n"; } @@ -184,6 +189,9 @@ sub foreach_list { 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);