This commit was generated by cvs2svn to compensate for changes in r11022,
[freeside.git] / FS / FS / Cron / rt_tasks.pm
1 package FS::Cron::rt_tasks;
2
3 use strict;
4 use vars qw( @ISA @EXPORT_OK $DEBUG );
5 use Exporter;
6 use FS::UID qw( dbh driver_name );
7 use FS::Record qw(qsearch qsearchs);
8 use FS::TicketSystem;
9 use FS::Conf;
10
11 use Date::Parse qw(str2time);
12
13 @ISA = qw( Exporter );
14 @EXPORT_OK = qw ( rt_escalate );
15 $DEBUG = 0;
16
17 my %void = ();
18
19 sub rt_escalate {
20   my %opt = @_;
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';
24
25   my $conf = new FS::Conf;
26   return if !$conf->exists('ticket_system-escalation');
27
28   FS::TicketSystem->init;
29   $DEBUG = 1 if $opt{'v'};
30   RT::Config->Set( LogToScreen => 'debug' ) if $DEBUG;
31   
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'};
35   my $error = '';
36
37   my $session = FS::TicketSystem->session();
38   my $CurrentUser = $session->{'CurrentUser'}
39     or die "Failed to create RT session";
40  
41   # load some modules that aren't handled in FS::TicketSystem 
42   foreach (qw(
43     Search::ActiveTicketsInQueue 
44     Action::EscalatePriority
45     Action::EscalateQueue
46     )) {
47     eval "use RT::$_";
48     die $@ if $@;
49   }
50
51   # adapted from rt-crontool
52   # Mechanics:
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.
56
57   # to make some actions work without complaining
58   %void = map { $_ => "RT::$_"->new($CurrentUser) }
59     (qw(Scrip ScripAction));
60
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);
64   $queues->UnLimit;
65   my @actions = ();
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;
70       next;
71     }
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,
78     );
79     $search->Prepare;
80     while (my $ticket = $tickets->Next) {
81       warn 'Ticket #'.$ticket->Id()."\n" if $DEBUG;
82       my @a = (
83         action($ticket, 'EscalatePriority', "CurrentTime:$time"),
84         action($ticket, 'EscalateQueue')
85       );
86       next if !@a;
87       push @actions, @a;
88       push @active_tickets, $ticket; # avoid RT's overzealous garbage collector
89     }
90   }
91   foreach (grep {$_} @actions) {
92     my ($val, $msg) = $_->Commit;
93     if ( $DEBUG ) {
94       if ($val) {
95         warn "Action committed: ".ref($_)." #".$_->TicketObj->Id."\n";
96       }
97       else {
98         warn "Action returned $msg: #".$_->TicketObj->Id."\n";
99       }
100     }
101   }
102   return;
103 }
104
105 sub action {
106   my $ticket = shift;
107   my $CurrentUser = $ticket->CurrentUser;
108
109   my $action = shift;
110   my $argument = shift;
111
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,
119   );
120   if ( $action_obj->Prepare ) {
121     warn "Action prepared: $action\n" if $DEBUG;
122     return $action_obj;
123   }
124   else {
125     return;
126   }
127 }
128
129 1;