1 package FS::Cron::rt_tasks;
4 use vars qw( @ISA @EXPORT_OK $DEBUG );
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_escalate );
21 # RT_External installations should have their own cron scripts for this
22 my $system = $FS::TicketSystem::system;
23 return if $system ne 'RT_Internal';
25 my $conf = new FS::Conf;
26 return if !$conf->exists('ticket_system-escalation');
28 FS::TicketSystem->init;
29 $DEBUG = 1 if $opt{'v'};
30 RT::Config->Set( LogToScreen => 'debug' ) if $DEBUG;
32 #we're at now now (and later).
33 my $time = $opt{'d'} ? str2time($opt{'d'}) : $^T;
34 $time += $opt{'y'} * 86400 if $opt{'y'};
37 my $session = FS::TicketSystem->session();
38 my $CurrentUser = $session->{'CurrentUser'}
39 or die "Failed to create RT session";
41 # load some modules that aren't handled in FS::TicketSystem
43 Search::ActiveTicketsInQueue
44 Action::EscalatePriority
51 # adapted from rt-crontool
53 # We're using EscalatePriority, so search in all queues that have a
54 # priority range defined. Select all active tickets in those queues and
55 # EscalatePriority, then EscalateQueue them.
57 # to make some actions work without complaining
58 %void = map { $_ => "RT::$_"->new($CurrentUser) }
59 (qw(Scrip ScripAction));
61 # Most of this stuff is common to any condition -> action processing
62 # we might want to do, but escalation is the only one we do now.
63 my $queues = RT::Queues->new($CurrentUser);
66 my @active_tickets = ();
67 while (my $queue = $queues->Next) {
68 if ( $queue->InitialPriority == $queue->FinalPriority ) {
69 warn "Queue '".$queue->Name."' (skipped)\n" if $DEBUG;
72 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->Name,
77 CurrentUser => $CurrentUser,
80 while (my $ticket = $tickets->Next) {
81 warn 'Ticket #'.$ticket->Id()."\n" if $DEBUG;
83 action($ticket, 'EscalatePriority', "CurrentTime:$time"),
84 action($ticket, 'EscalateQueue')
88 push @active_tickets, $ticket; # avoid RT's overzealous garbage collector
91 foreach (grep {$_} @actions) {
92 my ($val, $msg) = $_->Commit;
95 warn "Action committed: ".ref($_)." #".$_->TicketObj->Id."\n";
98 warn "Action returned $msg: #".$_->TicketObj->Id."\n";
107 my $CurrentUser = $ticket->CurrentUser;
110 my $argument = shift;
112 $action = "RT::Action::$action";
113 my $action_obj = $action->new(
114 TicketObj => $ticket,
115 Argument => $argument,
116 Scrip => $void{'Scrip'},
117 ScripAction => $void{'ScripAction'},
118 CurrentUser => $CurrentUser,
120 if ( $action_obj->Prepare ) {
121 warn "Action prepared: $action\n" if $DEBUG;