Skip to content

Commit

Permalink
feat: health check - can connect to firestore (#97)
Browse files Browse the repository at this point in the history
  • Loading branch information
subfuzion authored Jul 25, 2023
1 parent 90ae863 commit c9345be
Show file tree
Hide file tree
Showing 3 changed files with 60 additions and 1 deletion.
32 changes: 32 additions & 0 deletions src/lib/database.ts
Original file line number Diff line number Diff line change
Expand Up @@ -68,4 +68,36 @@ export class Database {
completedMissions: updatedMissions,
});
}

/**
* Returns true if able to connect to the Firestore instance.
* The Firestore API times out a request after 60 seconds. This method
* implements a configurable override that defaults to 5 seconds, but there's
* no point in setting it higher than 60 seconds.
* @param timeout seconds
*/
async isConnected(timeout: number = 5): Promise<boolean> {
try {
timeout = Math.min(timeout, 60) * 1000;
// eslint-disable-next-line no-undef
let timerId: NodeJS.Timeout;

const timer = new Promise<boolean>((resolve) => {
timerId = setTimeout(() => resolve(false), timeout);
});

// TODO: research if there's a lighter weight way to status a connection.
const connectionCheck = this.db.listCollections();

return Promise.race([connectionCheck, timer]).then(result => {
clearTimeout(timerId);
return !!result;
});

} catch (err) {
// GoogleError: Total timeout of API google.firestore.v1.Firestore
// exceeded 60000 milliseconds before any response was received.
return false;
}
}
}
28 changes: 28 additions & 0 deletions src/pages/api/fscheck.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
/**
* Copyright 2023 Google LLC
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
import type { NextApiRequest, NextApiResponse } from 'next'
import { Database } from "../../lib/database";


export default async function handler(
req: NextApiRequest,
res: NextApiResponse,
) {
const fs = new Database();
let isConnected = await fs.isConnected();
let statusCode = isConnected ? 200 : 503;
res.status(statusCode).end();
}
1 change: 0 additions & 1 deletion src/pages/api/user.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
// Next.js API route support: https://nextjs.org/docs/api-routes/introduction
import type { NextApiRequest, NextApiResponse } from 'next'
import { Database } from "../../lib/database";
import { User } from 'src/models/User';
Expand Down

0 comments on commit c9345be

Please sign in to comment.