File Coverage

blib/lib/Tapper/Schema/TestrunDB/Result/Host.pm
Criterion Covered Total %
statement 29 29 100.0
branch 8 8 100.0
condition 2 3 66.6
subroutine 6 6 100.0
pod 2 2 100.0
total 47 48 97.9


line stmt bran cond sub pod time code
1             package Tapper::Schema::TestrunDB::Result::Host;
2             our $AUTHORITY = 'cpan:TAPPER';
3             $Tapper::Schema::TestrunDB::Result::Host::VERSION = '5.0.9';
4             # ABSTRACT: Tapper - Containing hosts used by Tapper
5              
6 7     7   3210 use 5.010;
  7         19  
7 7     7   24 use strict;
  7         9  
  7         110  
8 7     7   22 use warnings;
  7         9  
  7         141  
9              
10 7     7   20 use parent 'DBIx::Class';
  7         9  
  7         30  
11              
12             __PACKAGE__->load_components(qw/InflateColumn::DateTime Core/);
13             __PACKAGE__->table("host");
14             __PACKAGE__->add_columns
15             (
16             "id", { data_type => "INT", default_value => undef, is_nullable => 0, size => 11, is_auto_increment => 1, },
17             "name", { data_type => "VARCHAR", default_value => "", is_nullable => 1, size => 255, },
18             "comment", { data_type => "VARCHAR", default_value => "", is_nullable => 1, size => 255, },
19             "free", { data_type => "TINYINT", default_value => "0", is_nullable => 1, },
20             "active", { data_type => "TINYINT", default_value => "0", is_nullable => 1, },
21             "is_deleted", { data_type => "TINYINT", default_value => "0", is_nullable => 1, }, # deleted hosts need to be kept in db to show old testruns correctly
22             "pool_free", { data_type => "INT", default_value => undef, is_nullable => 1, }, # number of free hosts in pool
23             "pool_id", { data_type => "INT", default_value => undef, is_nullable => 1, is_foreign_key => 1, }, # pool host that this host is element of
24             "created_at", { data_type => "TIMESTAMP", default_value => \'CURRENT_TIMESTAMP', is_nullable => 1, }, # '
25             "updated_at", { data_type => "DATETIME", default_value => undef, is_nullable => 1, },
26              
27             );
28              
29             __PACKAGE__->set_primary_key("id");
30              
31             (my $basepkg = __PACKAGE__) =~ s/::\w+$//;
32             __PACKAGE__->add_unique_constraint( constraint_name => [ qw/name/ ] );
33             __PACKAGE__->has_many ( testrunschedulings => "${basepkg}::TestrunScheduling", { 'foreign.host_id' => 'self.id' });
34             __PACKAGE__->has_many ( testrunrequestedhost => "${basepkg}::TestrunRequestedHost", { 'foreign.host_id' => 'self.id' });
35             __PACKAGE__->has_many ( queuehosts => "${basepkg}::QueueHost", { 'foreign.host_id' => 'self.id' });
36             __PACKAGE__->has_many ( denied_from_queue => "${basepkg}::DeniedHost", { 'foreign.host_id' => 'self.id' });
37             __PACKAGE__->has_many ( features => "${basepkg}::HostFeature", { 'foreign.host_id' => 'self.id' });
38              
39              
40             __PACKAGE__->belongs_to( pool_master => "${basepkg}::Host", { 'foreign.id' => 'self.pool_id'},{ join_type => 'left' });
41             __PACKAGE__->has_many ( pool_elements => "${basepkg}::Host", { 'foreign.pool_id' => 'self.id' });
42              
43              
44              
45             sub is_pool
46             {
47 14     14 1 56305 my($self) = @_;
48 14   66     366 return defined($self->pool_free) || $self->pool_elements->count != 0;
49             }
50              
51              
52             sub pool_count
53             {
54 9     9 1 143954 my ($self, $new_count) = @_;
55 9 100       32 if (defined $new_count) {
56             # if host is not a pool yet, we make it a pool but don't need to care for existing elements
57 3 100       10 if (not $self->is_pool) {
58 1         3128 $self->pool_free($new_count);
59 1         140 $self->free(1);
60 1         42 $self->update;
61             } else {
62             # need a transaction because its possible that the number
63             # of running tests changes between query and setting
64 2         38 my $guard = $self->result_source->schema->txn_scope_guard;
65              
66 2         881 my $new_free = $new_count - $self->testrunschedulings->search({status => 'running'})->count;
67 2         6211 $self->pool_free($new_free);
68 2 100       475 if ($self->pool_free > 0) {
69 1         27 $self->free(1);
70             } else {
71 1         24 $self->free(0);
72             }
73 2         144 $self->update;
74 2         1860 $guard->commit;
75             }
76             } else {
77 6 100       26 return undef unless $self->is_pool;
78 5         157 return($self->pool_free + $self->testrunschedulings->search({status => 'running'})->count);
79             }
80             }
81              
82              
83             1;
84              
85             __END__
86              
87             =pod
88              
89             =encoding UTF-8
90              
91             =head1 NAME
92              
93             Tapper::Schema::TestrunDB::Result::Host - Tapper - Containing hosts used by Tapper
94              
95             =head2 is_pool
96              
97             Tell me whether the given host is a pool host or not.
98              
99             =head2 pool_count
100              
101             Setter/getter for all elements in a pool
102              
103             =head1 AUTHORS
104              
105             =over 4
106              
107             =item *
108              
109             AMD OSRC Tapper Team <tapper@amd64.org>
110              
111             =item *
112              
113             Tapper Team <tapper-ops@amazon.com>
114              
115             =back
116              
117             =head1 COPYRIGHT AND LICENSE
118              
119             This software is Copyright (c) 2017 by Advanced Micro Devices, Inc..
120              
121             This is free software, licensed under:
122              
123             The (two-clause) FreeBSD License
124              
125             =cut