4 use vars qw( @ISA @EXPORT_OK $conf $jobnums);
8 use FS::Record qw( qsearch qsearchs dbh );
14 @ISA = qw(FS::Record);
15 @EXPORT_OK = qw( joblisting );
17 $FS::UID::callback{'FS::queue'} = sub {
25 FS::queue - Object methods for queue records
31 $record = new FS::queue \%hash;
32 $record = new FS::queue { 'column' => 'value' };
34 $error = $record->insert;
36 $error = $new_record->replace($old_record);
38 $error = $record->delete;
40 $error = $record->check;
44 An FS::queue object represents an queued job. FS::queue inherits from
45 FS::Record. The following fields are currently supported:
49 =item jobnum - primary key
51 =item job - fully-qualified subroutine name
53 =item status - job status
55 =item statustext - freeform text status message
57 =item _date - UNIX timestamp
59 =item svcnum - optional link to service (see L<FS::cust_svc>)
69 Creates a new job. To add the example to the database, see L<"insert">.
71 Note that this stores the hash reference, not a distinct copy of the hash it
72 points to. You can ask the object for a copy with the I<hash> method.
76 # the new method can be inherited from FS::Record, if a table method is defined
78 sub table { 'queue'; }
80 =item insert [ ARGUMENT, ARGUMENT... ]
82 Adds this record to the database. If there is an error, returns the error,
83 otherwise returns false.
85 If any arguments are supplied, a queue_arg record for each argument is also
86 created (see L<FS::queue_arg>).
90 #false laziness w/part_export.pm
94 local $SIG{HUP} = 'IGNORE';
95 local $SIG{INT} = 'IGNORE';
96 local $SIG{QUIT} = 'IGNORE';
97 local $SIG{TERM} = 'IGNORE';
98 local $SIG{TSTP} = 'IGNORE';
99 local $SIG{PIPE} = 'IGNORE';
101 my $oldAutoCommit = $FS::UID::AutoCommit;
102 local $FS::UID::AutoCommit = 0;
105 my $error = $self->SUPER::insert;
107 $dbh->rollback if $oldAutoCommit;
111 foreach my $arg ( @_ ) {
112 my $queue_arg = new FS::queue_arg ( {
113 'jobnum' => $self->jobnum,
116 $error = $queue_arg->insert;
118 $dbh->rollback if $oldAutoCommit;
123 push @$jobnums, $self->jobnum if $jobnums;
125 $dbh->commit or die $dbh->errstr if $oldAutoCommit;
133 Delete this record from the database. Any corresponding queue_arg records are
141 local $SIG{HUP} = 'IGNORE';
142 local $SIG{INT} = 'IGNORE';
143 local $SIG{QUIT} = 'IGNORE';
144 local $SIG{TERM} = 'IGNORE';
145 local $SIG{TSTP} = 'IGNORE';
146 local $SIG{PIPE} = 'IGNORE';
148 my $oldAutoCommit = $FS::UID::AutoCommit;
149 local $FS::UID::AutoCommit = 0;
152 my @del = qsearch( 'queue_arg', { 'jobnum' => $self->jobnum } );
153 push @del, qsearch( 'queue_depend', { 'depend_jobnum' => $self->jobnum } );
155 my $error = $self->SUPER::delete;
157 $dbh->rollback if $oldAutoCommit;
161 foreach my $del ( @del ) {
162 $error = $del->delete;
164 $dbh->rollback if $oldAutoCommit;
169 $dbh->commit or die $dbh->errstr if $oldAutoCommit;
175 =item replace OLD_RECORD
177 Replaces the OLD_RECORD with this one in the database. If there is an error,
178 returns the error, otherwise returns false.
182 # the replace method can be inherited from FS::Record
186 Checks all fields to make sure this is a valid job. If there is
187 an error, returns the error, otherwise returns false. Called by the insert
195 $self->ut_numbern('jobnum')
196 || $self->ut_anything('job')
197 || $self->ut_numbern('_date')
198 || $self->ut_enum('status',['', qw( new locked failed )])
199 || $self->ut_anything('statustext')
200 || $self->ut_numbern('svcnum')
202 return $error if $error;
204 $error = $self->ut_foreign_keyn('svcnum', 'cust_svc', 'svcnum');
205 $self->svcnum('') if $error;
207 $self->status('new') unless $self->status;
208 $self->_date(time) unless $self->_date;
215 Returns a list of the arguments associated with this job.
221 map $_->arg, qsearch( 'queue_arg',
222 { 'jobnum' => $self->jobnum },
230 Returns the FS::cust_svc object associated with this job, if any.
236 qsearchs('cust_svc', { 'svcnum' => $self->svcnum } );
241 Returns the FS::queue_depend objects associated with this job, if any.
247 qsearch('queue_depend', { 'jobnum' => $self->jobnum } );
251 =item depend_insert OTHER_JOBNUM
253 Inserts a dependancy for this job - it will not be run until the other job
254 specified completes. If there is an error, returns the error, otherwise
257 When using job dependancies, you should wrap the insertion of all relevant jobs
258 in a database transaction.
263 my($self, $other_jobnum) = @_;
264 my $queue_depend = new FS::queue_depend ( {
265 'jobnum' => $self->jobnum,
266 'depend_jobnum' => $other_jobnum,
268 $queue_depend->insert;
277 =item joblisting HASHREF NOACTIONS
282 my($hashref, $noactions) = @_;
288 my @queue = qsearch( 'queue', $hashref );
289 return '' unless scalar(@queue);
291 my $p = FS::CGI::popurl(2);
293 my $html = qq!<FORM ACTION="$p/misc/queue.cgi" METHOD="POST">!.
294 FS::CGI::table(). <<END;
296 <TH COLSPAN=2>Job</TH>
301 $html .= '<TH>Account</TH>' unless $hashref->{svcnum};
304 my $dangerous = $conf->exists('queue_dangerous_controls');
308 foreach my $queue ( sort {
309 $a->getfield('jobnum') <=> $b->getfield('jobnum')
311 my $queue_hashref = $queue->hashref;
312 my $jobnum = $queue->jobnum;
315 if ( $dangerous || $queue->job !~ /^FS::part_export::/ || !$noactions ) {
316 $args = encode_entities( join(' ',
317 map { length($_)<54 ? $_ : substr($_,0,32)."..." } $queue->args #1&g
323 my $date = time2str( "%a %b %e %T %Y", $queue->_date );
324 my $status = $queue->status;
325 $status .= ': '. $queue->statustext if $queue->statustext;
326 my @queue_depend = $queue->queue_depend;
327 $status .= ' (waiting for '.
328 join(', ', map { $_->depend_jobnum } @queue_depend ).
331 my $changable = $dangerous
332 || ( ! $noactions && $status =~ /^failed/ || $status =~ /^locked/ );
335 qq! ( <A HREF="$p/misc/queue.cgi?jobnum=$jobnum&action=new">retry</A> |!.
336 qq! <A HREF="$p/misc/queue.cgi?jobnum=$jobnum&action=del">remove</A> )!;
338 my $cust_svc = $queue->cust_svc;
343 <TD>$queue_hashref->{job}</TD>
349 unless ( $hashref->{svcnum} ) {
352 my $table = $cust_svc->part_svc->svcdb;
353 my $label = ( $cust_svc->label )[1];
354 $account = qq!<A HREF="../view/$table.cgi?!. $queue->svcnum.
359 $html .= "<TD>$account</TD>";
365 qq!<TD><INPUT NAME="jobnum$jobnum" TYPE="checkbox" VALUE="1"></TD>!;
376 $html .= '<BR><INPUT TYPE="submit" NAME="action" VALUE="retry selected">'.
377 '<INPUT TYPE="submit" NAME="action" VALUE="remove selected"><BR>';
388 $Id: queue.pm,v 1.15 2002-07-02 06:48:59 ivan Exp $
396 L<FS::Record>, schema.html from the base documentation.