2
0
Эх сурвалжийг харах

impl new query of healthcheck 'checkMiddlewaresStrictly'

Yuki Takei 6 жил өмнө
parent
commit
3e9ef2cd88

+ 19 - 0
src/server/models/vo/error-apiv3.js

@@ -1,3 +1,22 @@
+/**
+ * @swagger
+ *
+ *  components:
+ *    schemas:
+ *      ErrorV3:
+ *        description: Error for APIv3
+ *        type: object
+ *        properties:
+ *          message:
+ *            type: string
+ *            example: 'error message'
+ *          code:
+ *            type: string
+ *            example: 'someapi-error-with-something'
+ *          stack:
+ *            type: object
+ */
+
 class ErrorV3 extends Error {
 class ErrorV3 extends Error {
 
 
   constructor(message = '', code = '', stack = undefined) {
   constructor(message = '', code = '', stack = undefined) {

+ 81 - 14
src/server/routes/apiv3/healthcheck.js

@@ -7,6 +7,7 @@ const express = require('express');
 const router = express.Router();
 const router = express.Router();
 
 
 const helmet = require('helmet');
 const helmet = require('helmet');
+const ErrorV3 = require('../../models/vo/error-apiv3');
 
 
 /**
 /**
  * @swagger
  * @swagger
@@ -14,6 +15,38 @@ const helmet = require('helmet');
  *    name: Healthcheck
  *    name: Healthcheck
  */
  */
 
 
+/**
+ * @swagger
+ *
+ *  components:
+ *    schemas:
+ *      HealthcheckInfo:
+ *        description: Information of middlewares
+ *        type: object
+ *        properties:
+ *          mongo:
+ *            type: string
+ *            description: 'OK'
+ *            example: 'OK'
+ *          searchInfo:
+ *            type: object
+ *            example: {
+ *              "esVersion":"6.6.1",
+ *              "esNodeInfos":{
+ *                "6pnILIqFT_Cjbs4mwQfcmA": {
+ *                  "name":"6pnILIq",
+ *                  "version":"6.6.1",
+ *                  "plugins":[
+ *                    {"name":"analysis-icu","version":"6.6.1"},
+ *                    {"name":"analysis-kuromoji","version":"6.6.1"},
+ *                    {"name":"ingest-geoip","version":"6.6.1"},
+ *                    {"name":"ingest-user-agent","version":"6.6.1"}
+ *                  ]
+ *                }
+ *              }
+ *            }
+ */
+
 module.exports = (crowi) => {
 module.exports = (crowi) => {
   /**
   /**
    * @swagger
    * @swagger
@@ -27,7 +60,12 @@ module.exports = (crowi) => {
    *      parameters:
    *      parameters:
    *        - name: connectToMiddlewares
    *        - name: connectToMiddlewares
    *          in: query
    *          in: query
-   *          description: Check also MongoDB and SearchService
+   *          description: Check MongoDB and SearchService
+   *          schema:
+   *            type: boolean
+   *        - name: checkMiddlewaresStrictly
+   *          in: query
+   *          description: Check MongoDB and SearchService and responds 503 if either of these is unhealthy
    *          schema:
    *          schema:
    *            type: boolean
    *            type: boolean
    *      responses:
    *      responses:
@@ -37,34 +75,63 @@ module.exports = (crowi) => {
    *            application/json:
    *            application/json:
    *              schema:
    *              schema:
    *                properties:
    *                properties:
-   *                  mongo:
-   *                    type: string
-   *                    description: 'OK'
-   *                  searchInfo:
-   *                    type: object
+   *                  info:
+   *                    $ref: '#/components/schemas/HealthcheckInfo'
+   *        503:
+   *          content:
+   *            application/json:
+   *              schema:
+   *                properties:
+   *                  errors:
+   *                    type: array
+   *                    items:
+   *                      $ref: '#/components/schemas/ErrorV3'
+   *                  info:
+   *                    $ref: '#/components/schemas/HealthcheckInfo'
    */
    */
   router.get('/', helmet.noCache(), async(req, res) => {
   router.get('/', helmet.noCache(), async(req, res) => {
-    const connectToMiddlewares = req.query.connectToMiddlewares;
+    const connectToMiddlewares = req.query.connectToMiddlewares != null;
+    const checkMiddlewaresStrictly = req.query.checkMiddlewaresStrictly != null;
 
 
     // return 200 w/o connecting to MongoDB and SearchService
     // return 200 w/o connecting to MongoDB and SearchService
-    if (connectToMiddlewares == null) {
+    if (!connectToMiddlewares && !checkMiddlewaresStrictly) {
       res.status(200).send({ status: 'OK' });
       res.status(200).send({ status: 'OK' });
       return;
       return;
     }
     }
 
 
+    const errors = [];
+    const info = {};
+
+    // connect to MongoDB
     try {
     try {
-      // connect to MongoDB
       const Config = crowi.models.Config;
       const Config = crowi.models.Config;
       await Config.findOne({});
       await Config.findOne({});
-      // connect to Elasticsearch
-      const search = crowi.getSearcher();
-      const searchInfo = await search.getInfo();
 
 
-      res.status(200).send({ mongo: 'OK', searchInfo });
+      info.mongo = 'OK';
     }
     }
     catch (err) {
     catch (err) {
-      res.status(503).send({ err });
+      errors.push(new ErrorV3(`MongoDB is not connectable - ${err.message}`, 'healthcheck-mongodb-unhealthy', err.stack));
+    }
+
+    // connect to search service
+    try {
+      const search = crowi.getSearcher();
+      info.searchInfo = await search.getInfo();
     }
     }
+    catch (err) {
+      errors.push(new ErrorV3(`The Search Service is not connectable - ${err.message}`, 'healthcheck-search-unhealthy', err.stack));
+    }
+
+    if (errors.length > 0) {
+      let httpStatus = 200;
+      if (checkMiddlewaresStrictly) {
+        httpStatus = 503;
+      }
+
+      return res.apiv3Err(errors, httpStatus, info);
+    }
+
+    res.status(200).send({ info });
   });
   });
 
 
   return router;
   return router;