work around missing id, RT#83146
[freeside.git] / FS / bin / freeside-history-requeue
1 #!/usr/bin/perl -w
2
3 use strict;
4 use vars qw($opt_j $opt_d);
5 use Getopt::Std;
6 use Date::Parse;
7 use FS::UID qw(adminsuidsetup);
8 use FS::Record qw(qsearch qsearchs);
9 use FS::queue;
10
11 getopts('j:d');
12
13 my $user = shift or die &usage;
14 adminsuidsetup $user;
15
16 my $start = shift or die &usage;
17 my $end   = shift or die &usage;
18
19 $start = str2time($start) unless $start =~ /^(\d+)$/;
20 $end   = str2time($end)   unless $end   =~ /^(\d+)$/;
21
22 my $extra_sql = "AND history_date >= $start AND history_date <= $end";
23
24 my $hashref = { 'history_action' => 'insert' };
25
26 $hashref->{'job'} = $opt_j if $opt_j;
27
28 my @h_queue = qsearch({
29   'table'     => 'h_queue',
30   'hashref'   => $hashref,
31   'extra_sql' => $extra_sql,
32 });
33
34 my $num = 0;
35
36 foreach my $h_queue (@h_queue) {
37
38   my @queue_args = qsearch({
39     'table'     => 'h_queue_arg',
40     'hashref'   => { 'history_action' => 'insert',
41                      'jobnum'         => $h_queue->jobnum,
42                    },
43     'order_by'  => 'argnum',
44   });
45
46   my @args = map {
47     my $arg = $_->arg;
48     $arg =~ s/^db\.suicidegirls\.com$/sg-account/;
49     $arg;
50   } @queue_args;
51
52   my $queue = new FS::queue {
53     map { $_ => $h_queue->$_() }
54         qw( job _date status statustext svcnum )
55   };
56
57   if ( $opt_d ) { #dry run
58     print "requeueing job: ". join(' ', @args). "\n";
59     my $error = $queue->check;
60     die "error requeueing job ". $h_queue->jobnum. ": $error" if $error;
61   } else {
62     print "requeueing job: ". join(' ', @args). "\n";
63     my $error = $queue->insert(@args);
64     #warn "error requeueing job ". $h_queue->jobnum. ": $error\n" if $error;
65     print "error requeueing job ". $h_queue->jobnum. ": $error\n" if $error;
66   }
67
68   $num++;
69
70 }
71
72 print "requeued $num jobs\n";
73
74 sub usage {
75   die "Usage:\n\n  freeside-history-requeue user start_timestamp end_timestamp\n";
76 }
77
78 =head1 NAME
79
80 freeside-history-requeue - Command line tool to re-trigger export jobs for existing services
81
82 =head1 SYNOPSIS
83
84   freeside-history-requeue [ -j job ] [ -d ] user start_timestamp end_timestamp
85
86 =head1 DESCRIPTION
87
88   Re-queues all queued jobs for the specified time period.
89
90   -j: specifies that only jobs with this job string are re-queued.
91
92   -d: dry run
93
94 =head1 SEE ALSO
95
96 L<freeside-reexport>, L<freeside-sqlradius-reset>, L<FS::part_export>
97
98 =cut
99
100 1;