todo.org: Added.
[Fumo.git] / lib / Fumo / Queue.pm
blobfc77d8aa6f020398eacb101456174495026ae37b
1 package Fumo::Queue;
3 use Carp;
5 =head1 NAME
7 Fumo::Queue - Manipulate a queue of smoke server runs.
9 =head1 DESCRIPTION
11 Queue holds a queue of test runs that are to be run. It allows
12 manipulation of a weight field so that users can change which runs
13 happen when.
15 =head1 METHODS
17 =head2 new
19 =over 4
21 =item Arguments: $dbh
23 =item Return Value: $self
25 =back
27 Initialises a new Fumo::Queue object.
29 =cut
31 sub new {
32 my ($class, $dbh) = @_;
33 my $self = {
34 dbh => $dbh,
35 all_sth => undef
38 bless($self, $class);
40 return $self;
43 =head2 empty
45 =over 4
47 =item Arguments: none
49 =item Return Value: none
51 =back
53 Empties the queue completely. Will croak on failure.
55 =cut
57 sub all_runs {
58 my $self = shift;
60 if (not defined $self->{all_sth}) {
61 $self->{all_sth} = $self->{dbh}->prepare(qq(
62 SELECT
64 FROM
65 queue
66 ORDER BY
67 weight DESC
68 ));
70 $self->{all_sth}->execute()
71 or croak $self->{dbh}->errstr();
74 my $details = $self->{all_sth}->fetchrow_hashref();
76 if (defined $details) {
77 return Fumo::Queue::Run->new($details)
79 else {
80 return $self->{all_sth} = undef;
84 =head2 empty
86 =over 4
88 =item Arguments: none
90 =item Return Value: none
92 =back
94 Empties the queue completely. Will croak on failure.
96 =cut
98 sub empty {
99 my $self = shift;
101 $self->{dbh}->do(q(
102 DELETE
103 FROM
104 queue
105 )) or croak $self->{dbh}->errstr();
108 =head2 count
110 =over 4
112 =item Arguments: none
114 =item Return Value: Queue count
116 =back
118 Returns the number of runs in the queue. Croaks should there be a problem.
120 =cut
122 sub count {
123 my $self = shift;
125 my $sth = $self->{dbh}->prepare(q(
126 SELECT
127 count(project) as count
128 FROM
129 queue
130 )) or croak $self->{dbh}->errstr();
132 $sth->execute()
133 or croak $self->{dbh}->errstr();
135 return $sth->fetchrow_hashref->{count};
138 =head2 put_run
140 =over 4
142 =item Arguments: Queue::Run object
144 =item Return Value: none
146 =back
148 Add a Queue::Run object to the queue.
150 =cut
152 sub put_run {
153 my ($self, $run) = @_;
155 # does it this run already exist?
156 if (not $self->get_run($run->project, $run->branch, $run->revision)) {
157 my $sth = $self->{dbh}->prepare(q(
158 INSERT INTO
159 queue (project, branch, revision, created)
160 VALUES
161 (?, ?, ?, now())
162 )) or croak $self->{dbh}->errstr;
164 $sth->execute($run->project, $run->branch, $run->revision)
165 or croak $self->{dbh}->errstr;
167 else {
168 my $sth = $self->{dbh}->prepare(q(
169 UPDATE
170 queue
172 created = ?, run_start = ?, weight = ?, status = ?, comment = ?
173 WHERE
174 project = ? AND branch = ? AND revision = ?
177 # grab the argument from the Fumo::Queue::Run
178 my @args;
180 no strict 'refs';
182 @args = map {
183 $run->$_
184 } qw(created run_start weight status comment project branch
185 revision);
188 $sth->execute(@args)
189 or croak $self->{dbh}->errstr;
193 =head2 delete
195 =over 4
197 =item Arguments: Queue::Run
199 =item Return Value: none
201 =back
203 Delete the Queue::Run object given (it must have at least project,
204 branch and revision attributes).
206 =cut
208 sub delete {
209 my ($self, $run) = @_;
211 my $sth = $self->{dbh}->prepare(q(
212 DELETE
213 FROM
214 queue
215 WHERE
216 project = ? AND branch = ? AND revision = ?
217 )) or croak $self->{dbh}->errstr();
219 $sth->execute($run->project, $run->branch, $run->revision)
220 or croak $self->{dbh}->errstr;
223 =head2 get_run
225 =over 4
227 =item Arguments: $project, $branch, $revision
229 =item Return Value: Queue::Run
231 =back
233 Grab the Queue::Run object with the project, branch and revision.
235 =cut
237 sub get_run {
238 my ($self, $project, $branch, $revision) = @_;
240 my $sth = $self->{dbh}->prepare(q(
241 SELECT
243 FROM
244 queue
245 WHERE
246 project = ? AND branch = ? AND revision = ?
247 )) or croak $self->{dbh}->errstr;
249 $sth->execute($project, $branch, $revision)
250 or croak $self->{dbh}->errstr;
252 my $details = $sth->fetchrow_hashref();
254 return (defined $details)
255 ? Fumo::Queue::Run->new($details)
256 : undef;
259 return 1;