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 # if -d or -y is in use, bail out. There's no reliable way to tell RT
35 # to use an alternate system time.
36 if ( $opt{'d'} or $opt{'y'} ) {
37 warn "Forced date options in use - RT daily tasks skipped.\n";
41 FS::TicketSystem->init;
42 my $session = FS::TicketSystem->session();
43 my $CurrentUser = $session->{'CurrentUser'}
44 or die "Failed to create RT session";
46 $DEBUG = 1 if $opt{'v'};
47 RT::Config->Set( LogToScreen => 'debug' ) if $DEBUG;
49 # load some modules that aren't handled in FS::TicketSystem
51 Search::ActiveTicketsInQueue
52 Action::EscalatePriority
54 Action::ScheduledResolve
60 # adapted from rt-crontool
62 # to make some actions work without complaining
63 %void = map { $_ => "RT::$_"->new($CurrentUser) }
64 (qw(Scrip ScripAction));
66 # compile actions to be run
67 my (@actions, @active_tickets);
68 my $queues = RT::Queues->new($CurrentUser);
70 while (my $queue = $queues->Next) {
71 warn "Queue '".$queue->Name."'\n" if $DEBUG;
73 my $tickets = RT::Tickets->new($CurrentUser);
74 my $search = RT::Search::ActiveTicketsInQueue->new(
75 TicketsObj => $tickets,
76 Argument => $queue->Id,
77 CurrentUser => $CurrentUser,
80 foreach my $custnum ( @custnums ) {
81 die "invalid custnum passed to rt_daily: $custnum"
82 if !$custnum =~ /^\d+$/;
83 $tickets->LimitMemberOf(
84 "freeside://freeside/cust_main/$custnum",
85 ENTRYAGGREGATOR => 'OR',
86 SUBCLAUSE => 'custnum'
89 while (my $ticket = $tickets->Next) {
90 warn 'Ticket #'.$ticket->Id()."\n" if $DEBUG;
91 my @a = task_actions($ticket);
93 push @active_tickets, $ticket if @a; # avoid garbage collection
97 # and then commit them all
98 foreach (grep {$_} @actions) {
99 my ($val, $msg) = $_->Commit;
102 warn "Action committed: ".ref($_)." #".$_->TicketObj->Id."\n";
105 warn "Action returned $msg: #".$_->TicketObj->Id."\n";
116 $conf->exists('ticket_system-escalation') ? (
117 action($ticket, 'EscalatePriority', "CurrentTime: $^T"),
118 action($ticket, 'EscalateQueue')
121 ### scheduled resolve ###
122 action($ticket, 'ScheduledResolve'),
128 my $CurrentUser = $ticket->CurrentUser;
131 my $argument = shift;
133 $action = "RT::Action::$action";
134 my $action_obj = $action->new(
135 TicketObj => $ticket,
136 Argument => $argument,
137 Scrip => $void{'Scrip'},
138 ScripAction => $void{'ScripAction'},
139 CurrentUser => $CurrentUser,
141 if ( $action_obj->Prepare ) {
142 warn "Action prepared: $action\n" if $DEBUG;