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 } );
235 =item depend_insert OTHER_JOBNUM
237 Inserts a dependancy for this job. If there is an error, returns the error,
238 otherwise returns false.
240 When using job dependancies, you should wrap the insertion of jobs in a
241 database transaction.
246 my($self, $other_jobnum) = @_;
247 my $queue_depend = new FS::queue_depend (
248 'jobnum' => $self->jobnum,
249 'depend_jobnum' => $other_jobnum,
251 $queue_depend->insert;
260 =item joblisting HASHREF NOACTIONS
265 my($hashref, $noactions) = @_;
270 my @queue = qsearch( 'queue', $hashref );
271 return '' unless scalar(@queue);
273 my $p = FS::CGI::popurl(2);
275 my $html = qq!<FORM ACTION="$p/misc/queue.cgi" METHOD="POST">!.
276 FS::CGI::table(). <<END;
278 <TH COLSPAN=2>Job</TH>
283 $html .= '<TH>Account</TH>' unless $hashref->{svcnum};
286 my $dangerous = $conf->exists('queue_dangerous_controls');
290 foreach my $queue ( sort {
291 $a->getfield('jobnum') <=> $b->getfield('jobnum')
293 my $queue_hashref = $queue->hashref;
294 my $jobnum = $queue->jobnum;
297 if ( $dangerous || $queue->job !~ /^FS::part_export::/ || !$noactions ) {
298 $args = join(' ', $queue->args);
303 my $date = time2str( "%a %b %e %T %Y", $queue->_date );
304 my $status = $queue->status;
305 $status .= ': '. $queue->statustext if $queue->statustext;
306 my $changable = $dangerous
307 || ( ! $noactions && $status =~ /^failed/ || $status =~ /^locked/ );
310 qq! ( <A HREF="$p/misc/queue.cgi?jobnum=$jobnum&action=new">retry</A> |!.
311 qq! <A HREF="$p/misc/queue.cgi?jobnum=$jobnum&action=del">remove</A> )!;
313 my $cust_svc = $queue->cust_svc;
318 <TD>$queue_hashref->{job}</TD>
324 unless ( $hashref->{svcnum} ) {
327 my $table = $cust_svc->part_svc->svcdb;
328 my $label = ( $cust_svc->label )[1];
329 $account = qq!<A HREF="../view/$table.cgi?!. $queue->svcnum.
334 $html .= "<TD>$account</TD>";
340 qq!<TD><INPUT NAME="jobnum$jobnum" TYPE="checkbox" VALUE="1"></TD>!;
351 $html .= '<BR><INPUT TYPE="submit" NAME="action" VALUE="retry selected">'.
352 '<INPUT TYPE="submit" NAME="action" VALUE="remove selected"><BR>';
363 $Id: queue.pm,v 1.12 2002-05-15 13:24:24 ivan Exp $
369 L<FS::Record>, schema.html from the base documentation.