eggjs / egg

🥚 Born to build better enterprise frameworks and apps with Node.js & Koa
https://eggjs.org
MIT License
18.88k stars 1.81k forks source link

SingleMode #4308

Open sm2017 opened 4 years ago

sm2017 commented 4 years ago

I asked this question before https://github.com/eggjs/egg/issues/3262 https://github.com/eggjs/egg/issues/4183

It is too complicated for me!!

How can I run eggjs in single mode and create an anonymous context? I want to Run eggjs as library

I create a complex RESTful web service by eggjs , it have many services and plugins I want to create workers (some js scripts) for my messaging queue (rabbitmq) and I want to reuse services and plugins , so I need to access to ctx object

const runWorker = ()=>{
    // some code here

   //here I need eggjs service
  const configs = {
      baseDir : 'mainAppDir'
  };

  const egg= //Create a single mode eggjs instance
  const ctx = egg.createAnonymousContext();

}
atian25 commented 4 years ago

@sm2017 try this:


const { Application, Agent } = require('egg');

const agent = new Agent(options);
await agent.ready();

const app = new Application(options);
await app.ready();

app.agent = agent;
agent.application = app;

that is the mini init work at egg-cluster or egg-mock

but you need to start HTTP server yourself and handler gracefull exit.

we will provide a createInstance to do this at egg 3.0 later this year.

sm2017 commented 4 years ago

@atian25 I must enable all plugins too How you pass single mode in options?

Also I don't need Agent must I create an instance of agent?

sm2017 commented 4 years ago

@atian25 I run your code in a fresh installed simple mode eggjs

2020-05-17 10:36:22,279 WARN 3912 [egg:core:ready_timeout] 10 seconds later node_modules\egg-watcher\lib\init.js:15:14 was still unable to finish.
atian25 commented 4 years ago
const { Application } = require('egg');
const app = new Application(options);
await app.ready();

but some plugin required Agent, so maybe you could disable the plugin.

since this way is more like a hacky, so I can't promise everything will work fine.

sm2017 commented 4 years ago

@atian25 I run your code in a fresh installed simple mode eggjs

2020-05-17 10:36:22,279 WARN 3912 [egg:core:ready_timeout] 10 seconds later node_modules\egg-watcher\lib\init.js:15:14 was still unable to finish.

What about this error?

sm2017 commented 4 years ago

@atian25 reply please

sm2017 commented 3 years ago

@atian25 just to remove inactive tag