Skip to content

Commit

Permalink
Add healthcheck page at /status/health
Browse files Browse the repository at this point in the history
  • Loading branch information
davea committed Sep 25, 2024
1 parent 0291878 commit 347f28a
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 1 deletion.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@
- Add perl 5.38 support.
- Add plain text template previews to /_dev/email. #5105
- Add --exclude option to bin/open311-populate-service-list
- Add /status/health page to indicate service health.
- Performance improvements:
- Reduce database queries on shortlist page.
- Provide ResultSet fallback translation in lookup.
Expand Down
27 changes: 26 additions & 1 deletion perllib/FixMyStreet/App/Controller/Status.pm
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ use namespace::autoclean;

use HTTP::Negotiate;
use JSON::MaybeXS;
use Try::Tiny;

BEGIN { extends 'Catalyst::Controller'; }

Expand Down Expand Up @@ -46,7 +47,6 @@ sub index : Path : Args(0) {
$chosen = 'html' unless $chosen;
}

# TODO Perform health checks here

if ($chosen eq 'json') {
$c->res->content_type('application/json; charset=utf-8');
Expand All @@ -68,6 +68,31 @@ sub index : Path : Args(0) {
return 1;
}

sub health : Path('health') : Args(0) {
my ($self, $c) = @_;

Check warning on line 72 in perllib/FixMyStreet/App/Controller/Status.pm

View check run for this annotation

Codecov / codecov/patch

perllib/FixMyStreet/App/Controller/Status.pm#L72

Added line #L72 was not covered by tests

# Just do a very simple and inexpensive query to confirm DB connectivity
my $id = try {
$c->model('DB::Problem')->search(undef, {

Check warning on line 76 in perllib/FixMyStreet/App/Controller/Status.pm

View check run for this annotation

Codecov / codecov/patch

perllib/FixMyStreet/App/Controller/Status.pm#L76

Added line #L76 was not covered by tests
columns => [ "id" ],
rows => 1,
order_by => { -desc => 'id' },
})->first->id;
} catch {
undef;
};

Check warning on line 83 in perllib/FixMyStreet/App/Controller/Status.pm

View check run for this annotation

Codecov / codecov/patch

perllib/FixMyStreet/App/Controller/Status.pm#L82-L83

Added lines #L82 - L83 were not covered by tests

$c->res->content_type('text/plain; charset=utf-8');
$c->res->headers->header('Cache-Control' => 'max-age=0');

Check warning on line 86 in perllib/FixMyStreet/App/Controller/Status.pm

View check run for this annotation

Codecov / codecov/patch

perllib/FixMyStreet/App/Controller/Status.pm#L85-L86

Added lines #L85 - L86 were not covered by tests
if ($id) {
$c->res->body("OK: $id");

Check warning on line 88 in perllib/FixMyStreet/App/Controller/Status.pm

View check run for this annotation

Codecov / codecov/patch

perllib/FixMyStreet/App/Controller/Status.pm#L88

Added line #L88 was not covered by tests
} else {
$c->response->status("500");
$c->res->body("ERROR: Couldn't get last problem ID from DB");

Check warning on line 91 in perllib/FixMyStreet/App/Controller/Status.pm

View check run for this annotation

Codecov / codecov/patch

perllib/FixMyStreet/App/Controller/Status.pm#L90-L91

Added lines #L90 - L91 were not covered by tests
}
}


__PACKAGE__->meta->make_immutable;

1;
28 changes: 28 additions & 0 deletions t/app/controller/status.t
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
use FixMyStreet::TestMech;
use Test::MockModule;


ok( my $mech = FixMyStreet::TestMech->new, 'Created mech object' );

my $surrey = $mech->create_body_ok(2242, 'Surrey County Council');
(my $report) = $mech->create_problems_for_body(1, $surrey->id, 'Pothole', {
category => 'Potholes', cobrand => 'surrey',
latitude => 51.293415, longitude => -0.441269, areas => '2242',
});


subtest 'Health page returns 200 when things are OK' => sub {
$mech->get_ok('/status/health');
is $mech->content, 'OK: ' . $report->id, 'Got correct content for status page';
is $mech->res->code, 200, 'Got 200 for status page';
};

subtest 'Health page returns 500 when things are not OK' => sub {
my $rs = Test::MockModule->new('FixMyStreet::DB::ResultSet::Problem');
$rs->mock('first', sub { die 'DB error' });
$mech->get('/status/health');
is $mech->res->code, 500, 'Got 500 for status page';
};


done_testing();

0 comments on commit 347f28a

Please sign in to comment.