4 use vars qw( @ISA @EXPORT_OK $conf );
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 {
23 FS::queue - Object methods for queue records
29 $record = new FS::queue \%hash;
30 $record = new FS::queue { 'column' => 'value' };
32 $error = $record->insert;
34 $error = $new_record->replace($old_record);
36 $error = $record->delete;
38 $error = $record->check;
42 An FS::queue object represents an queued job. FS::queue inherits from
43 FS::Record. The following fields are currently supported:
47 =item jobnum - primary key
49 =item job - fully-qualified subroutine name
51 =item status - job status
53 =item statustext - freeform text status message
55 =item _date - UNIX timestamp
57 =item svcnum - optional link to service (see L<FS::cust_svc>)
67 Creates a new job. To add the example to the database, see L<"insert">.
69 Note that this stores the hash reference, not a distinct copy of the hash it
70 points to. You can ask the object for a copy with the I<hash> method.
74 # the new method can be inherited from FS::Record, if a table method is defined
76 sub table { 'queue'; }
78 =item insert [ ARGUMENT, ARGUMENT... ]
80 Adds this record to the database. If there is an error, returns the error,
81 otherwise returns false.
83 If any arguments are supplied, a queue_arg record for each argument is also
84 created (see L<FS::queue_arg>).
88 #false laziness w/part_export.pm
92 local $SIG{HUP} = 'IGNORE';
93 local $SIG{INT} = 'IGNORE';
94 local $SIG{QUIT} = 'IGNORE';
95 local $SIG{TERM} = 'IGNORE';
96 local $SIG{TSTP} = 'IGNORE';
97 local $SIG{PIPE} = 'IGNORE';
99 my $oldAutoCommit = $FS::UID::AutoCommit;
100 local $FS::UID::AutoCommit = 0;
103 my $error = $self->SUPER::insert;
105 $dbh->rollback if $oldAutoCommit;
109 foreach my $arg ( @_ ) {
110 my $queue_arg = new FS::queue_arg ( {
111 'jobnum' => $self->jobnum,
114 $error = $queue_arg->insert;
116 $dbh->rollback if $oldAutoCommit;
121 $dbh->commit or die $dbh->errstr if $oldAutoCommit;
129 Delete this record from the database. Any corresponding queue_arg records are
137 local $SIG{HUP} = 'IGNORE';
138 local $SIG{INT} = 'IGNORE';
139 local $SIG{QUIT} = 'IGNORE';
140 local $SIG{TERM} = 'IGNORE';
141 local $SIG{TSTP} = 'IGNORE';
142 local $SIG{PIPE} = 'IGNORE';
144 my $oldAutoCommit = $FS::UID::AutoCommit;
145 local $FS::UID::AutoCommit = 0;
148 my @del = qsearch( 'queue_arg', { 'jobnum' => $self->jobnum } );
149 push @del, qsearch( 'queue_depend', { 'depend_jobnum' => $self->jobnum } );
151 my $error = $self->SUPER::delete;
153 $dbh->rollback if $oldAutoCommit;
157 foreach my $del ( @del ) {
158 $error = $del->delete;
160 $dbh->rollback if $oldAutoCommit;
165 $dbh->commit or die $dbh->errstr if $oldAutoCommit;
171 =item replace OLD_RECORD
173 Replaces the OLD_RECORD with this one in the database. If there is an error,
174 returns the error, otherwise returns false.
178 # the replace method can be inherited from FS::Record
182 Checks all fields to make sure this is a valid job. If there is
183 an error, returns the error, otherwise returns false. Called by the insert
191 $self->ut_numbern('jobnum')
192 || $self->ut_anything('job')
193 || $self->ut_numbern('_date')
194 || $self->ut_enum('status',['', qw( new locked failed )])
195 || $self->ut_textn('statustext')
196 || $self->ut_numbern('svcnum')
198 return $error if $error;
200 $error = $self->ut_foreign_keyn('svcnum', 'cust_svc', 'svcnum');
201 $self->svcnum('') if $error;
203 $self->status('new') unless $self->status;
204 $self->_date(time) unless $self->_date;
211 Returns a list of the arguments associated with this job.
217 map $_->arg, qsearch( 'queue_arg',
218 { 'jobnum' => $self->jobnum },
226 Returns the FS::cust_svc object associated with this job, if any.
232 qsearchs('cust_svc', { 'svcnum' => $self->svcnum } );
237 Returns the FS::queue_depend objects associated with this job, if any.
243 qsearch('queue_depend', { 'jobnum' => $self->jobnum } );
247 =item depend_insert OTHER_JOBNUM
249 Inserts a dependancy for this job - it will not be run until the other job
250 specified completes. If there is an error, returns the error, otherwise
253 When using job dependancies, you should wrap the insertion of all relevant jobs
254 in a database transaction.
259 my($self, $other_jobnum) = @_;
260 my $queue_depend = new FS::queue_depend (
261 'jobnum' => $self->jobnum,
262 'depend_jobnum' => $other_jobnum,
264 $queue_depend->insert;
273 =item joblisting HASHREF NOACTIONS
278 my($hashref, $noactions) = @_;
283 my @queue = qsearch( 'queue', $hashref );
284 return '' unless scalar(@queue);
286 my $p = FS::CGI::popurl(2);
288 my $html = qq!<FORM ACTION="$p/misc/queue.cgi" METHOD="POST">!.
289 FS::CGI::table(). <<END;
291 <TH COLSPAN=2>Job</TH>
296 $html .= '<TH>Account</TH>' unless $hashref->{svcnum};
299 my $dangerous = $conf->exists('queue_dangerous_controls');
303 foreach my $queue ( sort {
304 $a->getfield('jobnum') <=> $b->getfield('jobnum')
306 my $queue_hashref = $queue->hashref;
307 my $jobnum = $queue->jobnum;
310 if ( $dangerous || $queue->job !~ /^FS::part_export::/ || !$noactions ) {
311 $args = join(' ', $queue->args);
316 my $date = time2str( "%a %b %e %T %Y", $queue->_date );
317 my $status = $queue->status;
318 $status .= ': '. $queue->statustext if $queue->statustext;
319 my @queue_depend = $queue->queue_depend;
320 $status .= ' (waiting for '.
321 join(', ', map { $_->other_jobnum } @queue_depend ).
324 my $changable = $dangerous
325 || ( ! $noactions && $status =~ /^failed/ || $status =~ /^locked/ );
328 qq! ( <A HREF="$p/misc/queue.cgi?jobnum=$jobnum&action=new">retry</A> |!.
329 qq! <A HREF="$p/misc/queue.cgi?jobnum=$jobnum&action=del">remove</A> )!;
331 my $cust_svc = $queue->cust_svc;
336 <TD>$queue_hashref->{job}</TD>
342 unless ( $hashref->{svcnum} ) {
345 my $table = $cust_svc->part_svc->svcdb;
346 my $label = ( $cust_svc->label )[1];
347 $account = qq!<A HREF="../view/$table.cgi?!. $queue->svcnum.
352 $html .= "<TD>$account</TD>";
358 qq!<TD><INPUT NAME="jobnum$jobnum" TYPE="checkbox" VALUE="1"></TD>!;
369 $html .= '<BR><INPUT TYPE="submit" NAME="action" VALUE="retry selected">'.
370 '<INPUT TYPE="submit" NAME="action" VALUE="remove selected"><BR>';
381 $Id: queue.pm,v 1.13 2002-05-15 14:00:32 ivan Exp $
387 L<FS::Record>, schema.html from the base documentation.