4 use vars qw( @ISA @EXPORT_OK $conf );
8 use FS::Record qw( qsearch qsearchs dbh );
13 @ISA = qw(FS::Record);
14 @EXPORT_OK = qw( joblisting );
16 $FS::UID::callback{'FS::queue'} = sub {
22 FS::queue - Object methods for queue records
28 $record = new FS::queue \%hash;
29 $record = new FS::queue { 'column' => 'value' };
31 $error = $record->insert;
33 $error = $new_record->replace($old_record);
35 $error = $record->delete;
37 $error = $record->check;
41 An FS::queue object represents an queued job. FS::queue inherits from
42 FS::Record. The following fields are currently supported:
46 =item jobnum - primary key
48 =item job - fully-qualified subroutine name
50 =item status - job status
52 =item statustext - freeform text status message
54 =item _date - UNIX timestamp
56 =item svcnum - optional link to service (see L<FS::cust_svc>)
66 Creates a new job. To add the example to the database, see L<"insert">.
68 Note that this stores the hash reference, not a distinct copy of the hash it
69 points to. You can ask the object for a copy with the I<hash> method.
73 # the new method can be inherited from FS::Record, if a table method is defined
75 sub table { 'queue'; }
77 =item insert [ ARGUMENT, ARGUMENT... ]
79 Adds this record to the database. If there is an error, returns the error,
80 otherwise returns false.
82 If any arguments are supplied, a queue_arg record for each argument is also
83 created (see L<FS::queue_arg>).
87 #false laziness w/part_export.pm
91 local $SIG{HUP} = 'IGNORE';
92 local $SIG{INT} = 'IGNORE';
93 local $SIG{QUIT} = 'IGNORE';
94 local $SIG{TERM} = 'IGNORE';
95 local $SIG{TSTP} = 'IGNORE';
96 local $SIG{PIPE} = 'IGNORE';
98 my $oldAutoCommit = $FS::UID::AutoCommit;
99 local $FS::UID::AutoCommit = 0;
102 my $error = $self->SUPER::insert;
104 $dbh->rollback if $oldAutoCommit;
108 foreach my $arg ( @_ ) {
109 my $queue_arg = new FS::queue_arg ( {
110 'jobnum' => $self->jobnum,
113 $error = $queue_arg->insert;
115 $dbh->rollback if $oldAutoCommit;
120 $dbh->commit or die $dbh->errstr if $oldAutoCommit;
128 Delete this record from the database. Any corresponding queue_arg records are
136 local $SIG{HUP} = 'IGNORE';
137 local $SIG{INT} = 'IGNORE';
138 local $SIG{QUIT} = 'IGNORE';
139 local $SIG{TERM} = 'IGNORE';
140 local $SIG{TSTP} = 'IGNORE';
141 local $SIG{PIPE} = 'IGNORE';
143 my $oldAutoCommit = $FS::UID::AutoCommit;
144 local $FS::UID::AutoCommit = 0;
147 my @args = qsearch( 'queue_arg', { 'jobnum' => $self->jobnum } );
149 my $error = $self->SUPER::delete;
151 $dbh->rollback if $oldAutoCommit;
155 foreach my $arg ( @args ) {
156 $error = $arg->delete;
158 $dbh->rollback if $oldAutoCommit;
163 $dbh->commit or die $dbh->errstr if $oldAutoCommit;
169 =item replace OLD_RECORD
171 Replaces the OLD_RECORD with this one in the database. If there is an error,
172 returns the error, otherwise returns false.
176 # the replace method can be inherited from FS::Record
180 Checks all fields to make sure this is a valid job. If there is
181 an error, returns the error, otherwise returns false. Called by the insert
189 $self->ut_numbern('jobnum')
190 || $self->ut_anything('job')
191 || $self->ut_numbern('_date')
192 || $self->ut_enum('status',['', qw( new locked failed )])
193 || $self->ut_textn('statustext')
194 || $self->ut_numbern('svcnum')
196 return $error if $error;
198 $error = $self->ut_foreign_keyn('svcnum', 'cust_svc', 'svcnum');
199 $self->svcnum('') if $error;
201 $self->status('new') unless $self->status;
202 $self->_date(time) unless $self->_date;
213 map $_->arg, qsearch( 'queue_arg',
214 { 'jobnum' => $self->jobnum },
222 Returns the FS::cust_svc object associated with this job, if any.
228 qsearchs('cust_svc', { 'svcnum' => $self->svcnum } );
231 =item joblisting HASHREF NOACTIONS
236 my($hashref, $noactions) = @_;
241 my @queue = qsearch( 'queue', $hashref );
242 return '' unless scalar(@queue);
244 my $html = FS::CGI::table(). <<END;
246 <TH COLSPAN=2>Job</TH>
251 $html .= '<TH>Account</TH>' unless $hashref->{svcnum};
254 my $p = FS::CGI::popurl(2);
255 foreach my $queue ( sort {
256 $a->getfield('jobnum') <=> $b->getfield('jobnum')
258 my $queue_hashref = $queue->hashref;
259 my $jobnum = $queue->jobnum;
260 my $args = join(' ', $queue->args);
261 my $date = time2str( "%a %b %e %T %Y", $queue->_date );
262 my $status = $queue->status;
263 $status .= ': '. $queue->statustext if $queue->statustext;
264 if ( $conf->exists('queue_dangerous_controls')
265 || ( ! $noactions && $status =~ /^failed/ || $status =~ /^locked/ ) ) {
267 qq! ( <A HREF="$p/misc/queue.cgi?jobnum=$jobnum&action=new">retry</A> |!.
268 qq! <A HREF="$p/misc/queue.cgi?jobnum=$jobnum&action=del">remove</A> )!;
270 my $cust_svc = $queue->cust_svc;
275 <TD>$queue_hashref->{job}</TD>
281 unless ( $hashref->{svcnum} ) {
284 my $table = $cust_svc->part_svc->svcdb;
285 my $label = ( $cust_svc->label )[1];
286 $account = qq!<A HREF="../view/$table.cgi?!. $queue->svcnum.
291 $html .= "<TD>$account</TD>";
308 $Id: queue.pm,v 1.9 2002-03-24 14:29:00 ivan Exp $
314 L<FS::Record>, schema.html from the base documentation.