File Coverage

blib/lib/Tapper/Schema/TestrunDB/Result/TestrunScheduling.pm
Criterion Covered Total %
statement 34 44 77.2
branch 5 18 27.7
condition n/a
subroutine 6 7 85.7
pod 4 4 100.0
total 49 73 67.1


line stmt bran cond sub pod time code
1             # TODO: rename into "(Scheduler|Result)::Job"?
2              
3             package Tapper::Schema::TestrunDB::Result::TestrunScheduling;
4             our $AUTHORITY = 'cpan:TAPPER';
5             $Tapper::Schema::TestrunDB::Result::TestrunScheduling::VERSION = '5.0.9';
6             # ABSTRACT: Tapper - Containing informations for an executed testrun
7              
8 7     7   3165 use YAML::Syck;
  7         24  
  7         425  
9 7     7   2932 use common::sense;
  7         52  
  7         28  
10             ## no critic (RequireUseStrict)
11 7     7   298 use parent 'DBIx::Class';
  7         8  
  7         29  
12              
13             __PACKAGE__->load_components("InflateColumn::Object::Enum", "Core");
14             __PACKAGE__->table("testrun_scheduling");
15             __PACKAGE__->add_columns
16             (
17             "id", { data_type => "INT", default_value => undef, is_nullable => 0, size => 11, is_auto_increment => 1, },
18             "testrun_id", { data_type => "INT", default_value => undef, is_nullable => 0, size => 11, is_foreign_key => 1, },
19             "queue_id", { data_type => "INT", default_value => 0, is_nullable => 1, size => 11, is_foreign_key => 1, },
20             "host_id", { data_type => "INT", default_value => undef, is_nullable => 1, size => 11, is_foreign_key => 1, },
21             "prioqueue_seq", { data_type => "INT", default_value => undef, is_nullable => 1, size => 11, },
22             "status", { data_type => "VARCHAR", default_value => "prepare", is_nullable => 1, size => 255, is_enum => 1, extra => { list => [qw(prepare schedule running finished)] } },
23             "auto_rerun", { data_type => "TINYINT", default_value => "0", is_nullable => 1, },
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             __PACKAGE__->set_primary_key(qw/id/);
29              
30             (my $basepkg = __PACKAGE__) =~ s/::\w+$//;
31              
32             __PACKAGE__->belongs_to( testrun => "${basepkg}::Testrun", { 'foreign.id' => 'self.testrun_id' });
33             __PACKAGE__->belongs_to( queue => "${basepkg}::Queue", { 'foreign.id' => 'self.queue_id' });
34             __PACKAGE__->belongs_to( host => "${basepkg}::Host", { 'foreign.id' => 'self.host_id' });
35              
36             __PACKAGE__->has_many ( requested_features => "${basepkg}::TestrunRequestedFeature", { 'foreign.testrun_id' => 'self.testrun_id' });
37             __PACKAGE__->has_many ( requested_hosts => "${basepkg}::TestrunRequestedHost", { 'foreign.testrun_id' => 'self.testrun_id' });
38              
39              
40              
41              
42             sub update_content {
43 0     0 1 0 my ($self, $args) =@_;
44              
45 0 0       0 $self->queue_id ( $args->{queue_id} ) if $args->{queue_id};
46 0 0       0 $self->host_id ( $args->{host_id} ) if $args->{host_id};
47 0 0       0 $self->status ( $args->{status} ) if $args->{status};
48 0 0       0 $self->auto_rerun ( $args->{auto_rerun} ) if $args->{auto_rerun};
49 0         0 $self->update;
50 0         0 return $self->id;
51             }
52              
53              
54             sub mark_as_running
55             {
56 3     3 1 12178 my ($self) = @_;
57              
58             # need a transaction because someone might access this
59             # variable on the CLI
60 3         32 my $guard = $self->result_source->schema->txn_scope_guard;
61              
62 3 50       1204 if ($self->host->is_pool) {
63 3         95 $self->host->get_from_storage;
64 3         11539 $self->host->pool_free($self->host->pool_free-1);
65 3 100       895 $self->host->free(0) if $self->host->pool_free == 0;
66             } else {
67 0         0 $self->host->free(0);
68             }
69 3         315 $self->host->update;
70              
71 3         3403 $self->prioqueue_seq(undef);
72 3         198 $self->status("running");
73 3         359 $self->update;
74              
75 3         1704 $guard->commit;
76             }
77              
78              
79             sub mark_as_finished
80             {
81 1     1 1 5112 my ($self) = @_;
82              
83             # need a transaction because someone might access this
84             # variable on the CLI
85 1         7 my $guard = $self->result_source->schema->txn_scope_guard;
86              
87 1 50       596 if ($self->host->is_pool) {
88 1         35 $self->host($self->host->get_from_storage);
89 1         5917 $self->host->pool_free($self->host->pool_free+1);
90 1 50       220 if ($self->host->pool_free > 0) {
91 1         42 $self->host->free(1);
92             }
93             } else {
94 0 0       0 if ($self->host->testrunschedulings->search({status => "running"})->count == 1) { # mitigate scheduler bug where multiple jobs run on same host; this condition here hopefully recovers the situation.
95 0         0 $self->host->free(1);
96             }
97             }
98 1         129 $self->host->update;
99 1         1139 $self->status("finished");
100 1         140 $self->update;
101 1         955 $guard->commit;
102             }
103              
104              
105             sub sqlt_deploy_hook
106             {
107 7     7 1 51105 my ($self, $sqlt_table) = @_;
108 7         38 $sqlt_table->add_index(name => 'testrun_scheduling_idx_created_at', fields => ['created_at']);
109 7         4372 $sqlt_table->add_index(name => 'testrun_scheduling_idx_status', fields => ['status']);
110             }
111              
112             1;
113              
114             __END__
115              
116             =pod
117              
118             =encoding UTF-8
119              
120             =head1 NAME
121              
122             Tapper::Schema::TestrunDB::Result::TestrunScheduling - Tapper - Containing informations for an executed testrun
123              
124             =head2 update_content
125              
126             Update content from given params.
127              
128             =head2 mark_as_running
129              
130             Mark a testrun as currently I<running>.
131              
132             =head2 mark_as_finished
133              
134             Mark a testrun as I<finished>.
135              
136             =head2 sqlt_deploy_hook
137              
138             Add useful indexes at deploy time.
139              
140             =head1 AUTHORS
141              
142             =over 4
143              
144             =item *
145              
146             AMD OSRC Tapper Team <tapper@amd64.org>
147              
148             =item *
149              
150             Tapper Team <tapper-ops@amazon.com>
151              
152             =back
153              
154             =head1 COPYRIGHT AND LICENSE
155              
156             This software is Copyright (c) 2017 by Advanced Micro Devices, Inc..
157              
158             This is free software, licensed under:
159              
160             The (two-clause) FreeBSD License
161              
162             =cut