09672480b7f021ddb2fb30536e9407390c4b5571
[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.  Any corresponding queue_arg records are
112 deleted as well
113
114 =cut
115
116 sub delete {
117   my $self = shift;
118
119   local $SIG{HUP} = 'IGNORE';
120   local $SIG{INT} = 'IGNORE';
121   local $SIG{QUIT} = 'IGNORE';
122   local $SIG{TERM} = 'IGNORE';
123   local $SIG{TSTP} = 'IGNORE';
124   local $SIG{PIPE} = 'IGNORE';
125
126   my $oldAutoCommit = $FS::UID::AutoCommit;
127   local $FS::UID::AutoCommit = 0;
128   my $dbh = dbh;
129
130   my @args = qsearch( 'queue_arg', { 'jobnum' => $self->jobnum } );
131
132   my $error = $self->SUPER::delete;
133   if ( $error ) {
134     $dbh->rollback if $oldAutoCommit;
135     return $error;
136   }
137
138   foreach my $arg ( @args ) {
139     $error = $arg->delete;
140     if ( $error ) {
141       $dbh->rollback if $oldAutoCommit;
142       return $error;
143     }
144   }
145
146   $dbh->commit or die $dbh->errstr if $oldAutoCommit;
147
148   '';
149
150 }
151
152 =item replace OLD_RECORD
153
154 Replaces the OLD_RECORD with this one in the database.  If there is an error,
155 returns the error, otherwise returns false.
156
157 =cut
158
159 # the replace method can be inherited from FS::Record
160
161 =item check
162
163 Checks all fields to make sure this is a valid job.  If there is
164 an error, returns the error, otherwise returns false.  Called by the insert
165 and replace methods.
166
167 =cut
168
169 sub check {
170   my $self = shift;
171   my $error =
172     $self->ut_numbern('jobnum')
173     || $self->ut_anything('job')
174     || $self->ut_numbern('_date')
175     || $self->ut_enum('status',['', qw( new locked failed )])
176   ;
177   return $error if $error;
178
179   $self->status('new') unless $self->status;
180   $self->_date(time) unless $self->_date;
181
182   ''; #no error
183 }
184
185 =item args
186
187 =cut
188
189 sub args {
190   my $self = shift;
191   map $_->arg, qsearch( 'queue_arg',
192                         { 'jobnum' => $self->jobnum },
193                         '',
194                         'ORDER BY argnum'
195                       );
196 }
197
198 =back
199
200 =head1 VERSION
201
202 $Id: queue.pm,v 1.3 2001-09-11 12:25:55 ivan Exp $
203
204 =head1 BUGS
205
206 =head1 SEE ALSO
207
208 L<FS::Record>, schema.html from the base documentation.
209
210 =cut
211
212 1;
213