Skip to content

Commit

Permalink
Agent
Browse files Browse the repository at this point in the history
  • Loading branch information
fengmk2 committed Jul 7, 2024
1 parent 235b77a commit 37abca9
Show file tree
Hide file tree
Showing 17 changed files with 176 additions and 188 deletions.
11 changes: 0 additions & 11 deletions src/agent.js

This file was deleted.

7 changes: 7 additions & 0 deletions src/agent.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import { BaseHookClass } from './lib/core/base_hook_class.js';

export class EggAgentHook extends BaseHookClass {
configDidLoad() {
this.agent._wrapMessenger();
}
}
1 change: 0 additions & 1 deletion src/index.test-d.ts

This file was deleted.

24 changes: 13 additions & 11 deletions src/index.js → src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,67 +2,69 @@
* @namespace Egg
*/

import { BaseContextClass } from './lib/core/base_context_class.js';

/**
* Start egg application with cluster mode
* @since 1.0.0
*/
exports.startCluster = require('egg-cluster').startCluster;
export { startCluster } from 'egg-cluster';

/**
* Start egg application with single process mode
* @since 1.0.0
*/
exports.start = require('./lib/start');
export { startEgg as start } from './lib/start.js';

/**
* @member {Application} Egg#Application
* @since 1.0.0
*/
exports.Application = require('./lib/application');
export { Application } from './lib/application.js';

/**
* @member {Agent} Egg#Agent
* @since 1.0.0
*/
exports.Agent = require('./lib/agent');
export { Agent } from './lib/agent.js';

/**
* @member {AppWorkerLoader} Egg#AppWorkerLoader
* @since 1.0.0
*/
exports.AppWorkerLoader = require('./lib/loader').AppWorkerLoader;

/**
* @member {AgentWorkerLoader} Egg#AgentWorkerLoader
* @since 1.0.0
*/
exports.AgentWorkerLoader = require('./lib/loader').AgentWorkerLoader;

export { AppWorkerLoader, AgentWorkerLoader } from './lib/loader/index.js';

/**
* @member {Controller} Egg#Controller
* @since 1.1.0
*/
exports.Controller = require('./lib/core/base_context_class');
export const Controller = BaseContextClass;

/**
* @member {Service} Egg#Service
* @since 1.1.0
*/
exports.Service = require('./lib/core/base_context_class');
export const Service = BaseContextClass;

/**
* @member {Subscription} Egg#Subscription
* @since 1.10.0
*/
exports.Subscription = require('./lib/core/base_context_class');
export const Subscription = BaseContextClass;

/**
* @member {BaseContextClass} Egg#BaseContextClass
* @since 1.2.0
*/
exports.BaseContextClass = require('./lib/core/base_context_class');
export { BaseContextClass } from './lib/core/base_context_class.js';

/**
* @member {Boot} Egg#Boot
*/
exports.Boot = require('./lib/core/base_hook_class');
export { BaseHookClass as Boot } from './lib/core/base_hook_class.js';
61 changes: 21 additions & 40 deletions src/lib/agent.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import ms from 'ms';
import { EggApplication } from './egg.js';
import { AgentWorkerLoader } from './loader';
import { EggLogger } from 'egg-logger';
import { EggApplication, EggApplicationOptions } from './egg.js';
import { AgentWorkerLoader } from './loader/index.js';

const EGG_LOADER = Symbol.for('egg#loader');

Expand All @@ -9,40 +9,22 @@ const EGG_LOADER = Symbol.for('egg#loader');
* @augments EggApplication
*/
export class Agent extends EggApplication {
readonly #agentAliveHandler: NodeJS.Timeout;

/**
* @class
* @param {Object} options - see {@link EggApplication}
*/
constructor(options = {}) {
options.type = 'agent';
super(options);

this.loader.load();

// dump config after loaded, ensure all the dynamic modifications will be recorded
const dumpStartTime = Date.now();
this.dumpConfig();
this.coreLogger.info(
'[egg:core] dump config after load, %s',
ms(Date.now() - dumpStartTime)
);
constructor(options?: EggApplicationOptions) {
super({
...options,
type: 'agent',
});

// keep agent alive even it doesn't have any io tasks
this.agentAliveHandler = setInterval(() => {}, 24 * 60 * 60 * 1000);

this._uncaughtExceptionHandler = this._uncaughtExceptionHandler.bind(this);
process.on('uncaughtException', this._uncaughtExceptionHandler);
}

_uncaughtExceptionHandler(err) {
if (!(err instanceof Error)) {
err = new Error(String(err));
}
/* istanbul ignore else */
if (err.name === 'Error') {
err.name = 'unhandledExceptionError';
}
this.coreLogger.error(err);
this.#agentAliveHandler = setInterval(() => {
this.coreLogger.info('[]');
}, 24 * 60 * 60 * 1000);
}

get [EGG_LOADER]() {
Expand All @@ -60,26 +42,25 @@ export class Agent extends EggApplication {
wrapMethod(methodName, this.messenger, this.coreLogger);
}

function wrapMethod(methodName, messenger, logger) {
function wrapMethod(methodName: string, messenger: any, logger: EggLogger) {
const originMethod = messenger[methodName];
messenger[methodName] = function() {
const stack = new Error().stack.split('\n').slice(1).join('\n');
messenger[methodName] = function(...args: any[]) {
const stack = new Error().stack!.split('\n').slice(1).join('\n');
logger.warn(
"agent can't call %s before server started\n%s",
methodName,
stack
stack,
);
originMethod.apply(this, arguments);
originMethod.apply(this, args);
};
messenger.prependOnceListener('egg-ready', () => {
messenger[methodName] = originMethod;
});
}
}

close() {
process.removeListener('uncaughtException', this._uncaughtExceptionHandler);
clearInterval(this.agentAliveHandler);
return super.close();
async close() {
clearInterval(this.#agentAliveHandler);
await super.close();
}
}
6 changes: 3 additions & 3 deletions src/lib/application.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import path from 'path';
import fs from 'fs';
import path from 'node:path';
import fs from 'node:fs';
import http from 'node:http';
import ms from 'ms';
import is from 'is-type-of';
import graceful from 'graceful';
import http from 'http';
import cluster from 'cluster-client';
import onFinished from 'on-finished';
import { assign } from 'utility';
Expand Down
25 changes: 13 additions & 12 deletions src/lib/core/base_hook_class.ts
Original file line number Diff line number Diff line change
@@ -1,29 +1,30 @@
import assert from 'node:assert';
import type { ILifecycleBoot } from '@eggjs/core';
import type { Application, Agent } from '../../index.js';

const INSTANCE = Symbol('BaseHookClass#instance');
export class BaseHookClass implements ILifecycleBoot {
fullPath?: string;
#instance: Application | Agent;

export class BaseHookClass {


constructor(instance) {
this[INSTANCE] = instance;
constructor(instance: Application | Agent) {
this.#instance = instance;
}

get logger() {
return this[INSTANCE].logger;
return this.#instance.logger;
}

get config() {
return this[INSTANCE].config;
return this.#instance.config;
}

get app() {
assert(this[INSTANCE].type === 'application', 'agent boot should not use app instance');
return this[INSTANCE];
assert(this.#instance.type === 'application', 'agent boot should not use app instance');
return this.#instance as Application;
}

get agent() {
assert(this[INSTANCE].type === 'agent', 'app boot should not use agent instance');
return this[INSTANCE];
assert(this.#instance.type === 'agent', 'app boot should not use agent instance');
return this.#instance as Agent;
}
}
Loading

0 comments on commit 37abca9

Please sign in to comment.