Skip to content

Commit

Permalink
Merge pull request #3203 from metacpan/haarg/healthcheck
Browse files Browse the repository at this point in the history
Add docker healthcheck
  • Loading branch information
haarg authored Oct 12, 2024
2 parents 5a4ec8c + b7b7d10 commit 5f2f94b
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 0 deletions.
4 changes: 4 additions & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@ RUN <<EOT
npm run build:min
EOT

HEALTHCHECK CMD [ "test", "-e", "root/assets/assets.json" ]

################### Web Server
# hadolint ignore=DL3007
FROM metacpan/metacpan-base:latest AS server
Expand Down Expand Up @@ -62,6 +64,8 @@ CMD [ \

EXPOSE 80

HEALTHCHECK --start-period=3s CMD [ "curl", "--fail", "http://localhost/healthcheck" ]

################### Development Server
FROM server AS develop

Expand Down
10 changes: 10 additions & 0 deletions lib/MetaCPAN/Web/Controller/Root.pm
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,16 @@ sub robots : Path("robots.txt") : Args(0) {
} );
}

sub healthcheck : Local : Args(0) {
my ( $self, $c ) = @_;

$c->res->content_type('application/json');
$c->stash( {
template => 'healthcheck.tx',
status => 'healthy',
} );
}

=head2 end
Attempt to render a view, if needed.
Expand Down
1 change: 1 addition & 0 deletions root/healthcheck.tx
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
[% { status => $status }.json() | raw %]
19 changes: 19 additions & 0 deletions t/controller/healthcheck.t
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
use strict;
use warnings;
use lib 't/lib';

use Cpanel::JSON::XS qw( decode_json );
use MetaCPAN::Web::Test qw( app GET test_psgi );
use Test::More;

test_psgi app, sub {
my $cb = shift;
ok( my $res = $cb->( GET '/healthcheck' ), 'GET /healthcheck' );
is( $res->code, 200, 'code 200' );
is $res->header('Content-Type'), 'application/json',
'correct Content-Type';
my $data = decode_json( $res->content );
is $data->{status}, 'healthy', 'has correct status';
};

done_testing;

0 comments on commit 5f2f94b

Please sign in to comment.