Skip to content

Commit 5f2f94b

Browse files
authored
Merge pull request #3203 from metacpan/haarg/healthcheck
Add docker healthcheck
2 parents 5a4ec8c + b7b7d10 commit 5f2f94b

File tree

4 files changed

+34
-0
lines changed

4 files changed

+34
-0
lines changed

Dockerfile

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,8 @@ RUN <<EOT
2222
npm run build:min
2323
EOT
2424

25+
HEALTHCHECK CMD [ "test", "-e", "root/assets/assets.json" ]
26+
2527
################### Web Server
2628
# hadolint ignore=DL3007
2729
FROM metacpan/metacpan-base:latest AS server
@@ -62,6 +64,8 @@ CMD [ \
6264

6365
EXPOSE 80
6466

67+
HEALTHCHECK --start-period=3s CMD [ "curl", "--fail", "http://localhost/healthcheck" ]
68+
6569
################### Development Server
6670
FROM server AS develop
6771

lib/MetaCPAN/Web/Controller/Root.pm

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,16 @@ sub robots : Path("robots.txt") : Args(0) {
9393
} );
9494
}
9595

96+
sub healthcheck : Local : Args(0) {
97+
my ( $self, $c ) = @_;
98+
99+
$c->res->content_type('application/json');
100+
$c->stash( {
101+
template => 'healthcheck.tx',
102+
status => 'healthy',
103+
} );
104+
}
105+
96106
=head2 end
97107
98108
Attempt to render a view, if needed.

root/healthcheck.tx

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
[% { status => $status }.json() | raw %]

t/controller/healthcheck.t

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
use strict;
2+
use warnings;
3+
use lib 't/lib';
4+
5+
use Cpanel::JSON::XS qw( decode_json );
6+
use MetaCPAN::Web::Test qw( app GET test_psgi );
7+
use Test::More;
8+
9+
test_psgi app, sub {
10+
my $cb = shift;
11+
ok( my $res = $cb->( GET '/healthcheck' ), 'GET /healthcheck' );
12+
is( $res->code, 200, 'code 200' );
13+
is $res->header('Content-Type'), 'application/json',
14+
'correct Content-Type';
15+
my $data = decode_json( $res->content );
16+
is $data->{status}, 'healthy', 'has correct status';
17+
};
18+
19+
done_testing;

0 commit comments

Comments
 (0)