Skip to content

Commit

Permalink
Merge pull request #8961 from weseek/fix/healthcheck
Browse files Browse the repository at this point in the history
fix: Healthcheck with checkServices=mongo
  • Loading branch information
yuki-takei authored Jul 13, 2024
2 parents c0c7faf + 50f2700 commit 60cb3d9
Show file tree
Hide file tree
Showing 27 changed files with 81 additions and 91 deletions.
4 changes: 2 additions & 2 deletions apps/app/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -148,7 +148,7 @@
"next-i18next": "^15.2.0",
"next-superjson": "^0.0.4",
"next-themes": "^0.2.1",
"nocache": "^3.0.1",
"nocache": "^4.0.0",
"node-cron": "^3.0.2",
"nodemailer": "^6.6.2",
"nodemailer-ses-transport": "~1.5.0",
Expand Down Expand Up @@ -230,7 +230,7 @@
"@swc/jest": "^0.2.36",
"@testing-library/react": "^14.1.2",
"@testing-library/user-event": "^14.5.2",
"@types/express": "^4.17.11",
"@types/express": "^4.17.21",
"@types/jest": "^29.5.2",
"@types/react-input-autosize": "^2.2.4",
"@types/react-scroll": "^1.8.4",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,19 +3,21 @@ import * as os from 'node:os';

import type { IUserHasId } from '@growi/core';

import { ObjectIdLike } from '~/server/interfaces/mongoose-utils';
import type { ObjectIdLike } from '~/server/interfaces/mongoose-utils';
// eslint-disable-next-line import/no-named-as-default
import Config from '~/server/models/config';
import { Config } from '~/server/models/config';
import { aclService } from '~/server/service/acl';
import loggerFactory from '~/utils/logger';

import type { IGrowiInfo } from '../../interfaces/growi-info';
import {
GrowiWikiType, GrowiExternalAuthProviderType, IGrowiInfo, GrowiServiceType, GrowiAttachmentType, GrowiDeploymentType,
GrowiWikiType, GrowiExternalAuthProviderType, GrowiServiceType, GrowiAttachmentType, GrowiDeploymentType,
} from '../../interfaces/growi-info';
import { StatusType } from '../../interfaces/questionnaire-answer-status';
import { type IUserInfo, UserType } from '../../interfaces/user-info';
import QuestionnaireAnswerStatus from '../models/questionnaire-answer-status';
import QuestionnaireOrder, { QuestionnaireOrderDocument } from '../models/questionnaire-order';
import type { QuestionnaireOrderDocument } from '../models/questionnaire-order';
import QuestionnaireOrder from '../models/questionnaire-order';
import { isShowableCondition } from '../util/condition';


Expand Down
2 changes: 1 addition & 1 deletion apps/app/src/migrations/20180927102719-init-serverurl.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import mongoose from 'mongoose';

// eslint-disable-next-line import/no-named-as-default
import Config from '~/server/models/config';
import { Config } from '~/server/models/config';
import { getMongoUri, mongoOptions } from '~/server/util/mongoose-utils';
import loggerFactory from '~/utils/logger';

Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import mongoose from 'mongoose';

// eslint-disable-next-line import/no-named-as-default
import Config from '~/server/models/config';
import { Config } from '~/server/models/config';
import { getMongoUri, mongoOptions } from '~/server/util/mongoose-utils';
import loggerFactory from '~/utils/logger';

Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import mongoose from 'mongoose';

// eslint-disable-next-line import/no-named-as-default
import Config from '~/server/models/config';
import { Config } from '~/server/models/config';
import { getModelSafely, getMongoUri, mongoOptions } from '~/server/util/mongoose-utils';
import loggerFactory from '~/utils/logger';

Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import mongoose from 'mongoose';

// eslint-disable-next-line import/no-named-as-default
import Config from '~/server/models/config';
import { Config } from '~/server/models/config';
import { getMongoUri, mongoOptions } from '~/server/util/mongoose-utils';
import loggerFactory from '~/utils/logger';

Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import mongoose from 'mongoose';

// eslint-disable-next-line import/no-named-as-default
import Config from '~/server/models/config';
import { Config } from '~/server/models/config';
import { getMongoUri, mongoOptions } from '~/server/util/mongoose-utils';
import loggerFactory from '~/utils/logger';

Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import mongoose from 'mongoose';

// eslint-disable-next-line import/no-named-as-default
import Config from '~/server/models/config';
import { Config } from '~/server/models/config';
import { getMongoUri, mongoOptions } from '~/server/util/mongoose-utils';
import loggerFactory from '~/utils/logger';

Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import mongoose from 'mongoose';

// eslint-disable-next-line import/no-named-as-default
import Config from '~/server/models/config';
import { Config } from '~/server/models/config';
import { getModelSafely, getMongoUri, mongoOptions } from '~/server/util/mongoose-utils';
import loggerFactory from '~/utils/logger';

Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import mongoose from 'mongoose';

// eslint-disable-next-line import/no-named-as-default
import Config from '~/server/models/config';
import { Config } from '~/server/models/config';
import { getMongoUri, mongoOptions } from '~/server/util/mongoose-utils';
import loggerFactory from '~/utils/logger';

Expand Down
2 changes: 1 addition & 1 deletion apps/app/src/migrations/20200828024025-copy-aws-setting.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import mongoose from 'mongoose';

// eslint-disable-next-line import/no-named-as-default
import Config from '~/server/models/config';
import { Config } from '~/server/models/config';
import { getMongoUri, mongoOptions } from '~/server/util/mongoose-utils';
import loggerFactory from '~/utils/logger';

Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import mongoose from 'mongoose';

// eslint-disable-next-line import/no-named-as-default
import Config from '~/server/models/config';
import { Config } from '~/server/models/config';
import { getMongoUri, mongoOptions } from '~/server/util/mongoose-utils';
import loggerFactory from '~/utils/logger';

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
// eslint-disable-next-line import/no-named-as-default
import Config from '~/server/models/config';
import { Config } from '~/server/models/config';
import { getMongoUri, mongoOptions } from '~/server/util/mongoose-utils';
import loggerFactory from '~/utils/logger';

Expand Down
2 changes: 1 addition & 1 deletion apps/app/src/migrations/20200915035234-rename-s3-config.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
// eslint-disable-next-line import/no-named-as-default
import Config from '~/server/models/config';
import { Config } from '~/server/models/config';
import { getMongoUri, mongoOptions } from '~/server/util/mongoose-utils';
import loggerFactory from '~/utils/logger';

Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import mongoose from 'mongoose';

// eslint-disable-next-line import/no-named-as-default
import Config from '~/server/models/config';
import { Config } from '~/server/models/config';
import { getMongoUri, mongoOptions } from '~/server/util/mongoose-utils';
import loggerFactory from '~/utils/logger';

Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import mongoose from 'mongoose';

// eslint-disable-next-line import/no-named-as-default
import Config from '~/server/models/config';
import { Config } from '~/server/models/config';
import { getMongoUri, mongoOptions } from '~/server/util/mongoose-utils';
import loggerFactory from '~/utils/logger';

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@ import mongoose from 'mongoose';
import {
PageRecursiveDeleteConfigValue, PageRecursiveDeleteCompConfigValue,
} from '~/interfaces/page-delete-config';
import ConfigModel from '~/server/models/config';
import { getModelSafely, getMongoUri, mongoOptions } from '~/server/util/mongoose-utils';
import { Config } from '~/server/models/config';
import { getMongoUri, mongoOptions } from '~/server/util/mongoose-utils';
import loggerFactory from '~/utils/logger';

const logger = loggerFactory('growi:migrate:convert-page-delete-config');
Expand All @@ -13,7 +13,6 @@ const logger = loggerFactory('growi:migrate:convert-page-delete-config');
module.exports = {
async up(db, client) {
mongoose.connect(getMongoUri(), mongoOptions);
const Config = getModelSafely('Config') || ConfigModel;

const isNewConfigExists = await Config.count({
ns: 'crowi',
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
// eslint-disable-next-line import/no-named-as-default
import Config from '~/server/models/config';
import { Config } from '~/server/models/config';
import { getMongoUri, mongoOptions } from '~/server/util/mongoose-utils';
import loggerFactory from '~/utils/logger';

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
// eslint-disable-next-line import/no-named-as-default
import Config from '~/server/models/config';
import { Config } from '~/server/models/config';
import { getMongoUri, mongoOptions } from '~/server/util/mongoose-utils';
import loggerFactory from '~/utils/logger';

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
// eslint-disable-next-line import/no-named-as-default
import Config from '~/server/models/config';
import { Config } from '~/server/models/config';
import { getMongoUri, mongoOptions } from '~/server/util/mongoose-utils';
import loggerFactory from '~/utils/logger';

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
// eslint-disable-next-line import/no-named-as-default
import ConfigModel from '~/server/models/config';
import { Config } from '~/server/models/config';
import { getModelSafely, getMongoUri, mongoOptions } from '~/server/util/mongoose-utils';
import loggerFactory from '~/utils/logger';

Expand All @@ -12,7 +12,6 @@ module.exports = {
async up() {
logger.info('Apply migration');
mongoose.connect(getMongoUri(), mongoOptions);
const Config = getModelSafely('Config') || ConfigModel;
const User = getModelSafely('User') || require('~/server/models/user')();

const appInstalled = await Config.findOne({ key: 'app:installed' });
Expand All @@ -39,7 +38,6 @@ module.exports = {
async down() {
logger.info('Rollback migration');
mongoose.connect(getMongoUri(), mongoOptions);
const Config = getModelSafely('Config') || ConfigModel;

const appInstalled = await Config.findOne({ key: 'app:installed' });
if (appInstalled != null) {
Expand Down
6 changes: 3 additions & 3 deletions apps/app/src/server/models/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import { RehypeSanitizeType } from '~/interfaces/services/rehype-sanitize';
import { getOrCreateModel } from '../util/mongoose-utils';


export interface Config {
export interface IConfig {
_id: Types.ObjectId;
ns: string;
key: string;
Expand All @@ -21,7 +21,7 @@ export interface Config {
interface ModelMethods { any }


const schema = new Schema<Config>({
const schema = new Schema<IConfig>({
ns: { type: String, required: true },
key: { type: String, required: true },
value: { type: String, required: true },
Expand Down Expand Up @@ -176,4 +176,4 @@ export const defaultNotificationConfigs: { [key: string]: any } = {
'slack:token': undefined,
};

export default getOrCreateModel<Config, ModelMethods>('Config', schema);
export const Config = getOrCreateModel<IConfig, ModelMethods>('Config', schema);
Original file line number Diff line number Diff line change
@@ -1,14 +1,17 @@
import { ErrorV3 } from '@growi/core/dist/models';
import express from 'express';
import nocache from 'nocache';

import loggerFactory from '~/utils/logger';

const logger = loggerFactory('growi:routes:apiv3:healthcheck'); // eslint-disable-line no-unused-vars
import { Config } from '../../models/config';

const express = require('express');
import type { ApiV3Response } from './interfaces/apiv3-response';

const router = express.Router();

const noCache = require('nocache');
const logger = loggerFactory('growi:routes:apiv3:healthcheck');

const router = express.Router();

/**
* @swagger
Expand Down Expand Up @@ -52,7 +55,6 @@ module.exports = (crowi) => {

async function checkMongo(errors, info) {
try {
const Config = crowi.models.Config;
await Config.findOne({});

info.mongo = 'OK';
Expand Down Expand Up @@ -123,8 +125,11 @@ module.exports = (crowi) => {
* info:
* $ref: '#/components/schemas/HealthcheckInfo'
*/
router.get('/', noCache(), async(req, res) => {
let checkServices = req.query.checkServices || [];
router.get('/', nocache(), async(req, res: ApiV3Response) => {
let checkServices = (() => {
if (req.query.checkServices == null) return [];
return Array.isArray(req.query.checkServices) ? req.query.checkServices : [req.query.checkServices];
})();
let isStrictly = req.query.strictly != null;

// for backward compatibility
Expand Down
7 changes: 4 additions & 3 deletions apps/app/src/server/service/config-loader.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,9 @@ import { parseISO } from 'date-fns/parseISO';
import { GrowiServiceType } from '~/features/questionnaire/interfaces/growi-info';
import loggerFactory from '~/utils/logger';

import type { Config } from '../models/config';
import ConfigModel, { defaultCrowiConfigs, defaultMarkdownConfigs, defaultNotificationConfigs } from '../models/config';
import {
Config, defaultCrowiConfigs, defaultMarkdownConfigs, defaultNotificationConfigs,
} from '../models/config';


const logger = loggerFactory('growi:service:ConfigLoader');
Expand Down Expand Up @@ -790,7 +791,7 @@ export default class ConfigLoader {

async loadFromDB(): Promise<any> {
const config = {};
const docs: Config[] = await ConfigModel.find().exec();
const docs = await Config.find().exec();

for (const doc of docs) {
if (!config[doc.ns]) {
Expand Down
10 changes: 5 additions & 5 deletions apps/app/src/server/service/config-manager.spec.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { mock } from 'vitest-mock-extended';

import ConfigModel from '../models/config';
import { Config } from '../models/config';

import { configManager } from './config-manager';
import type { S2sMessagingService } from './s2s-messaging/base';
Expand All @@ -19,7 +19,7 @@ describe('ConfigManager test', () => {

test('invoke publishUpdateMessage()', async() => {
// setup
ConfigModel.bulkWrite = vi.fn();
Config.bulkWrite = vi.fn();
configManager.loadConfigs = vi.fn();
configManager.publishUpdateMessage = vi.fn();

Expand All @@ -28,14 +28,14 @@ describe('ConfigManager test', () => {
await configManager.updateConfigsInTheSameNamespace('dummyNs', dummyConfig);

// then
expect(ConfigModel.bulkWrite).toHaveBeenCalledTimes(1);
expect(Config.bulkWrite).toHaveBeenCalledTimes(1);
expect(configManager.loadConfigs).toHaveBeenCalledTimes(1);
expect(configManager.publishUpdateMessage).toHaveBeenCalledTimes(1);
});

test('does not invoke publishUpdateMessage()', async() => {
// setup
ConfigModel.bulkWrite = vi.fn();
Config.bulkWrite = vi.fn();
configManager.loadConfigs = vi.fn();
configManager.publishUpdateMessage = vi.fn();

Expand All @@ -44,7 +44,7 @@ describe('ConfigManager test', () => {
await configManager.updateConfigsInTheSameNamespace('dummyNs', dummyConfig, true);

// then
expect(ConfigModel.bulkWrite).toHaveBeenCalledTimes(1);
expect(Config.bulkWrite).toHaveBeenCalledTimes(1);
expect(configManager.loadConfigs).toHaveBeenCalledTimes(1);
expect(configManager.publishUpdateMessage).not.toHaveBeenCalled();
});
Expand Down
6 changes: 3 additions & 3 deletions apps/app/src/server/service/config-manager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { parseISO } from 'date-fns/parseISO';

import loggerFactory from '~/utils/logger';

import ConfigModel from '../models/config';
import { Config } from '../models/config';
import S2sMessage from '../models/vo/s2s-message';

import type { ConfigObject } from './config-loader';
Expand Down Expand Up @@ -204,7 +204,7 @@ class ConfigManagerImpl implements ConfigManager, S2sMessageHandlable {
},
});
}
await ConfigModel.bulkWrite(queries);
await Config.bulkWrite(queries);

await this.loadConfigs();

Expand All @@ -223,7 +223,7 @@ class ConfigManagerImpl implements ConfigManager, S2sMessageHandlable {
},
});
}
await ConfigModel.bulkWrite(queries);
await Config.bulkWrite(queries);

await this.loadConfigs();

Expand Down
Loading

0 comments on commit 60cb3d9

Please sign in to comment.