faster (cached) fuzzy searches
[freeside.git] / FS / FS / queue.pm
1 package FS::queue;
2
3 use strict;
4 use vars qw( @ISA );
5 use FS::Record qw( qsearch qsearchs dbh );
6 use FS::queue_arg;
7
8 @ISA = qw(FS::Record);
9
10 =head1 NAME
11
12 FS::queue - Object methods for queue records
13
14 =head1 SYNOPSIS
15
16   use FS::queue;
17
18   $record = new FS::queue \%hash;
19   $record = new FS::queue { 'column' => 'value' };
20
21   $error = $record->insert;
22
23   $error = $new_record->replace($old_record);
24
25   $error = $record->delete;
26
27   $error = $record->check;
28
29 =head1 DESCRIPTION
30
31 An FS::queue object represents an queued job.  FS::queue inherits from
32 FS::Record.  The following fields are currently supported:
33
34 =over 4
35
36 =item jobnum - primary key
37
38 =item job - fully-qualified subroutine name
39
40 =item status - job status
41
42 =back
43
44 =head1 METHODS
45
46 =over 4
47
48 =item new HASHREF
49
50 Creates a new job.  To add the example to the database, see L<"insert">.
51
52 Note that this stores the hash reference, not a distinct copy of the hash it
53 points to.  You can ask the object for a copy with the I<hash> method.
54
55 =cut
56
57 # the new method can be inherited from FS::Record, if a table method is defined
58
59 sub table { 'queue'; }
60
61 =item insert [ ARGUMENT, ARGUMENT... ]
62
63 Adds this record to the database.  If there is an error, returns the error,
64 otherwise returns false.
65
66 If any arguments are supplied, a queue_arg record for each argument is also
67 created (see L<FS::queue_arg>).
68
69 =cut
70
71 sub insert {
72   my $self = shift;
73
74   local $SIG{HUP} = 'IGNORE';
75   local $SIG{INT} = 'IGNORE';
76   local $SIG{QUIT} = 'IGNORE';
77   local $SIG{TERM} = 'IGNORE';
78   local $SIG{TSTP} = 'IGNORE';
79   local $SIG{PIPE} = 'IGNORE';
80
81   my $oldAutoCommit = $FS::UID::AutoCommit;
82   local $FS::UID::AutoCommit = 0;
83   my $dbh = dbh;
84
85   my $error = $self->SUPER::insert;
86   if ( $error ) {
87     $dbh->rollback if $oldAutoCommit;
88     return $error;
89   }
90
91   foreach my $arg ( @_ ) {
92     my $queue_arg = new FS::queue_arg ( {
93       'jobnum' => $self->jobnum,
94       'arg'    => $arg,
95     } );
96     $error = $queue_arg->insert;
97     if ( $error ) {
98       $dbh->rollback if $oldAutoCommit;
99       return $error;
100     }
101   }
102
103   $dbh->commit or die $dbh->errstr if $oldAutoCommit;
104
105   '';
106
107 }
108
109 =item delete
110
111 Delete this record from the database.
112
113 =cut
114
115 # the delete method can be inherited from FS::Record
116
117 =item replace OLD_RECORD
118
119 Replaces the OLD_RECORD with this one in the database.  If there is an error,
120 returns the error, otherwise returns false.
121
122 =cut
123
124 # the replace method can be inherited from FS::Record
125
126 =item check
127
128 Checks all fields to make sure this is a valid job.  If there is
129 an error, returns the error, otherwise returns false.  Called by the insert
130 and replace methods.
131
132 =cut
133
134 sub check {
135   my $self = shift;
136   my $error =
137     $self->ut_numbern('jobnum')
138     || $self->ut_anything('job')
139     || $self->ut_numbern('_date')
140     || $self->ut_enum('status',['', qw( new locked failed )])
141   ;
142   return $error if $error;
143
144   $self->status('new') unless $self->status;
145   $self->_date(time) unless $self->_date;
146
147   ''; #no error
148 }
149
150 =item args
151
152 =cut
153
154 sub args {
155   my $self = shift;
156   map $_->arg, qsearch( 'queue_arg', { 'jobnum' => $self->jobnum } );
157 }
158
159 =back
160
161 =head1 VERSION
162
163 $Id: queue.pm,v 1.1 2001-09-11 00:08:18 ivan Exp $
164
165 =head1 BUGS
166
167 =head1 SEE ALSO
168
169 L<FS::Record>, schema.html from the base documentation.
170
171 =cut
172
173 1;
174