Skip to content

Commit

Permalink
feat: export health service and support mongoose health check (#3569)
Browse files Browse the repository at this point in the history
* chore: export midway health service

* feat: add mongoose health check

* feat: add mongoose health check

* fix: test

* fix: test
  • Loading branch information
czy88840616 authored Jan 16, 2024
1 parent 30caff5 commit c3dd094
Show file tree
Hide file tree
Showing 23 changed files with 179 additions and 156 deletions.
12 changes: 0 additions & 12 deletions packages/cache-manager-redis/README.md

This file was deleted.

8 changes: 0 additions & 8 deletions packages/cache-manager-redis/jest.config.js

This file was deleted.

2 changes: 0 additions & 2 deletions packages/cache-manager-redis/jest.setup.js

This file was deleted.

40 changes: 0 additions & 40 deletions packages/cache-manager-redis/package.json

This file was deleted.

12 changes: 0 additions & 12 deletions packages/cache-manager-redis/src/index.ts

This file was deleted.

11 changes: 0 additions & 11 deletions packages/cache-manager-redis/tsconfig.json

This file was deleted.

4 changes: 0 additions & 4 deletions packages/cache-manager-redis/typedoc.json

This file was deleted.

1 change: 1 addition & 0 deletions packages/cache-manager/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,4 @@ export * from './interface';
export * from './factory';
export * from './decorator/cacheKey';
export * as CacheManager from 'cache-manager';
export * from './store';
Original file line number Diff line number Diff line change
@@ -1,13 +1,28 @@
import type { Config, Store } from 'cache-manager';
import { Redis } from '@midwayjs/redis';
import { MidwayCommonError } from '@midwayjs/core';
import type { Redis, RedisServiceFactory } from '@midwayjs/redis';
import {
IMidwayContainer,
MidwayCommonError,
safeRequire,
} from '@midwayjs/core';

const getVal = (value: unknown) => JSON.stringify(value) || '"undefined"';
export interface RedisStore extends Store {
readonly isCacheable: (value: unknown) => boolean;
}

export function createRedisStore(redisCache: Redis, options?: Config) {
export function createRedisStore(instanceName: string) {
return async (options: Config, container: IMidwayContainer) => {
const { RedisServiceFactory } = safeRequire('@midwayjs/redis');
const redisServiceFactory: RedisServiceFactory = await container.getAsync(
RedisServiceFactory
);
const redisInstance = redisServiceFactory.get(instanceName);
return createStore(redisInstance, options);
};
}

function createStore(redisCache: Redis, options?: Config) {
const isCacheable =
options?.isCacheable || (value => value !== undefined && value !== null);

Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
import * as cacheManager from '@midwayjs/cache-manager';
import * as redis from '@midwayjs/redis';
import { createLightApp, close } from '@midwayjs/mock';
import { createStore } from '../src';
import { CachingFactory } from '@midwayjs/cache-manager';
import { CachingFactory, createRedisStore } from '../src';

describe('cache-manager-redis', () => {
describe('cache-manager-redis store test', () => {
it('should test single caching', async () => {
const app = await createLightApp('', {
imports: [
Expand All @@ -14,7 +13,7 @@ describe('cache-manager-redis', () => {
globalConfig: {
cacheManager: {
client: {
store: createStore('default'),
store: createRedisStore('default'),
options: {
ttl: 10,
}
Expand Down Expand Up @@ -56,7 +55,7 @@ describe('cache-manager-redis', () => {
client: {
store: [
{
store: createStore('default'),
store: createRedisStore('default'),
options: {
ttl: 10,
}
Expand Down
30 changes: 28 additions & 2 deletions packages/core/src/common/dataSourceManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ import { loadModule } from '../util';
import { ModuleLoadType, DataSourceManagerConfigOption } from '../interface';
import { Inject } from '../decorator';
import { MidwayEnvironmentService } from '../service/environmentService';
import { MidwayPriorityManager } from './priorityManager';

const debug = debuglog('midway:debug');

export abstract class DataSourceManager<
Expand All @@ -22,12 +24,16 @@ export abstract class DataSourceManager<
protected options: DataSourceManagerConfigOption<ConnectionOpts> = {};
protected modelMapping = new WeakMap();
private innerDefaultDataSourceName: string;
protected dataSourcePriority: Record<string, string>;

@Inject()
protected appDir: string;

@Inject()
appDir: string;
protected environmentService: MidwayEnvironmentService;

@Inject()
environmentService: MidwayEnvironmentService;
protected priorityManager: MidwayPriorityManager;

protected async initDataSource(
dataSourceConfig: DataSourceManagerConfigOption<ConnectionOpts>,
Expand Down Expand Up @@ -115,6 +121,10 @@ export abstract class DataSourceManager<
return Array.from(this.dataSource.keys());
}

public getAllDataSources() {
return this.dataSource;
}

/**
* check the data source is connected
* @param dataSourceName
Expand Down Expand Up @@ -203,6 +213,22 @@ export abstract class DataSourceManager<
}
return this.innerDefaultDataSourceName;
}

public getDataSourcePriority(name: string) {
return this.priorityManager.getPriority(this.dataSourcePriority[name]);
}

public isHighPriority(name: string) {
return this.priorityManager.isHighPriority(this.dataSourcePriority[name]);
}

public isMediumPriority(name: string) {
return this.priorityManager.isMediumPriority(this.dataSourcePriority[name]);
}

public isLowPriority(name: string) {
return this.priorityManager.isLowPriority(this.dataSourcePriority[name]);
}
}

export function formatGlobString(globString: string): string[] {
Expand Down
37 changes: 37 additions & 0 deletions packages/core/src/common/priorityManager.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
import { Provide, Scope } from '../decorator';
import { ScopeEnum } from '../interface';

export const DEFAULT_PRIORITY = {
L1: 'High',
L2: 'Medium',
L3: 'Low',
};

@Provide()
@Scope(ScopeEnum.Singleton)
export class MidwayPriorityManager {
private priorityList: Record<string, string> = DEFAULT_PRIORITY;
private defaultPriority = DEFAULT_PRIORITY.L2;

public getCurrentPriorityList() {
return this.priorityList;
}

public getDefaultPriority(): string {
return this.defaultPriority;
}

public isHighPriority(priority = DEFAULT_PRIORITY.L2) {
return priority === DEFAULT_PRIORITY.L1;
}
public isMediumPriority(priority = DEFAULT_PRIORITY.L2) {
return priority === DEFAULT_PRIORITY.L2;
}
public isLowPriority(priority = DEFAULT_PRIORITY.L2) {
return priority === DEFAULT_PRIORITY.L3;
}

public getPriority(priority: string) {
return priority || this.getDefaultPriority();
}
}
35 changes: 15 additions & 20 deletions packages/core/src/common/serviceFactory.ts
Original file line number Diff line number Diff line change
@@ -1,21 +1,20 @@
import { extend } from '../util/extend';
import { IServiceFactory } from '../interface';

export const DEFAULT_PRIORITY = {
L1: 'High',
L2: 'Medium',
L3: 'Low',
};
import { MidwayPriorityManager } from './priorityManager';
import { Inject } from '../decorator';

/**
* 多客户端工厂实现
*/
export abstract class ServiceFactory<T> implements IServiceFactory<T> {
protected clients: Map<string, T> = new Map();
protected priority;
protected clientPriority: Record<string, string>;

protected options = {};

@Inject()
protected priorityManager: MidwayPriorityManager;

protected async initClients(options: any = {}): Promise<void> {
this.options = options;

Expand All @@ -34,11 +33,7 @@ export abstract class ServiceFactory<T> implements IServiceFactory<T> {
}

// set priority
this.priority = options.priority || {
...Array.from(this.clients.keys()).map(name => ({
[name]: DEFAULT_PRIORITY.L2,
})),
};
this.clientPriority = options.priority || {};
}

public get<U = T>(id = 'default'): U {
Expand Down Expand Up @@ -86,19 +81,19 @@ export abstract class ServiceFactory<T> implements IServiceFactory<T> {
return Array.from(this.clients.keys());
}

public getClientPriority(clientName: string) {
return this.priority[clientName] || DEFAULT_PRIORITY.L2;
public getClientPriority(name: string) {
return this.priorityManager.getPriority(this.clientPriority[name]);
}

public isHighPriority(clientName: string) {
return this.getClientPriority(clientName) === DEFAULT_PRIORITY.L1;
public isHighPriority(name: string) {
return this.priorityManager.isHighPriority(this.clientPriority[name]);
}

public isMediumPriority(clientName: string) {
return this.getClientPriority(clientName) === DEFAULT_PRIORITY.L2;
public isMediumPriority(name: string) {
return this.priorityManager.isMediumPriority(this.clientPriority[name]);
}

public isLowPriority(clientName: string) {
return this.getClientPriority(clientName) === DEFAULT_PRIORITY.L3;
public isLowPriority(name: string) {
return this.priorityManager.isLowPriority(this.clientPriority[name]);
}
}
5 changes: 5 additions & 0 deletions packages/core/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ export { MidwayLifeCycleService } from './service/lifeCycleService';
export { MidwayMiddlewareService } from './service/middlewareService';
export { MidwayDecoratorService } from './service/decoratorService';
export { MidwayMockService } from './service/mockService';
export { MidwayHealthService } from './service/healthService';
export {
RouterInfo,
DynamicRouterInfo,
Expand All @@ -30,6 +31,10 @@ export {
WebRouterCollector,
} from './service/slsFunctionService';
export { DataSourceManager } from './common/dataSourceManager';
export {
DEFAULT_PRIORITY,
MidwayPriorityManager,
} from './common/priorityManager';
export * from './service/pipelineService';

export * from './common/loggerFactory';
Expand Down
7 changes: 2 additions & 5 deletions packages/core/src/interface.ts
Original file line number Diff line number Diff line change
Expand Up @@ -459,9 +459,6 @@ export interface MidwayCoreDefaultConfig {
}
}


export type ClientPriorityValue = 'High' | 'Medium' | 'Low';

export type ServiceFactoryConfigOption<OPTIONS> = {
default?: PowerPartial<OPTIONS>;
client?: PowerPartial<OPTIONS>;
Expand All @@ -470,7 +467,7 @@ export type ServiceFactoryConfigOption<OPTIONS> = {
};
defaultClientName?: string;
clientPriority?: {
[key: string]: ClientPriorityValue;
[key: string]: number;
}
};

Expand Down Expand Up @@ -1174,7 +1171,7 @@ export interface IServiceFactory<Client> {
getDefaultClientName(): string;
getClients(): Map<string, Client>;
getClientKeys(): string[];
getClientPriority(clientName: string): ClientPriorityValue;
getClientPriority(clientName: string): string;
isHighPriority(clientName: string): boolean;
isMediumPriority(clientName: string): boolean;
isLowPriority(clientName: string) : boolean;
Expand Down
Loading

0 comments on commit c3dd094

Please sign in to comment.