1 package FS::Cron::rt_tasks;
4 use vars qw( @ISA @EXPORT_OK $DEBUG $conf );
6 use FS::UID qw( dbh driver_name );
7 use FS::Record qw(qsearch qsearchs);
11 use Date::Parse qw(str2time);
13 @ISA = qw( Exporter );
14 @EXPORT_OK = qw ( rt_daily );
17 FS::UID->install_callback( sub {
20 $conf = FS::Conf->new;
28 my @custnums = @ARGV; # ick
30 # RT_External installations should have their own cron scripts for this
31 my $system = $FS::TicketSystem::system;
32 return if !defined($system) || $system ne 'RT_Internal';
34 system('/opt/rt3/sbin/rt-fulltext-indexer --quiet --limit 5400 &');
36 # if -d or -y is in use, bail out. There's no reliable way to tell RT
37 # to use an alternate system time.
38 if ( $opt{'d'} or $opt{'y'} ) {
39 warn "Forced date options in use - RT daily tasks skipped.\n";
43 FS::TicketSystem->init;
44 my $session = FS::TicketSystem->session();
45 my $CurrentUser = $session->{'CurrentUser'}
46 or die "Failed to create RT session";
48 $DEBUG = 1 if $opt{'v'};
49 RT::Config->Set( LogToScreen => 'debug' ) if $DEBUG;
51 # load some modules that aren't handled in FS::TicketSystem
53 Search::ActiveTicketsInQueue
54 Action::EscalatePriority
56 Action::ScheduledResolve
62 # adapted from rt-crontool
64 # to make some actions work without complaining
65 %void = map { $_ => "RT::$_"->new($CurrentUser) }
66 (qw(Scrip ScripAction));
68 # compile actions to be run
69 my (@actions, @active_tickets);
70 my $queues = RT::Queues->new($CurrentUser);
72 while (my $queue = $queues->Next) {
73 warn "Queue '".$queue->Name."'\n" if $DEBUG;
75 my $tickets = RT::Tickets->new($CurrentUser);
76 my $search = RT::Search::ActiveTicketsInQueue->new(
77 TicketsObj => $tickets,
78 Argument => $queue->Id,
79 CurrentUser => $CurrentUser,
82 foreach my $custnum ( @custnums ) {
83 die "invalid custnum passed to rt_daily: $custnum"
84 if !$custnum =~ /^\d+$/;
85 $tickets->LimitMemberOf(
86 "freeside://freeside/cust_main/$custnum",
87 ENTRYAGGREGATOR => 'OR',
88 SUBCLAUSE => 'custnum'
91 while (my $ticket = $tickets->Next) {
92 warn 'Ticket #'.$ticket->Id()."\n" if $DEBUG;
93 my @a = task_actions($ticket);
95 push @active_tickets, $ticket if @a; # avoid garbage collection
99 # and then commit them all
100 foreach (grep {$_} @actions) {
101 my ($val, $msg) = $_->Commit;
104 warn "Action committed: ".ref($_)." #".$_->TicketObj->Id."\n";
107 warn "Action returned $msg: #".$_->TicketObj->Id."\n";
118 $conf->exists('ticket_system-escalation') ? (
119 action($ticket, 'EscalatePriority', "CurrentTime: $^T"),
120 action($ticket, 'EscalateQueue')
123 ### scheduled resolve ###
124 action($ticket, 'ScheduledResolve'),
130 my $CurrentUser = $ticket->CurrentUser;
133 my $argument = shift;
135 $action = "RT::Action::$action";
136 my $action_obj = $action->new(
137 TicketObj => $ticket,
138 Argument => $argument,
139 Scrip => $void{'Scrip'},
140 ScripAction => $void{'ScripAction'},
141 CurrentUser => $CurrentUser,
143 if ( $action_obj->Prepare ) {
144 warn "Action prepared: $action\n" if $DEBUG;