mirror of
https://github.com/n8n-io/n8n.git
synced 2025-01-12 13:27:31 -08:00
feat(core): Add support for ready hooks, and credentials overwrite endpoint in workers (#6954)
This commit is contained in:
parent
e6cff3fce4
commit
8f8a1de3dd
|
@ -24,8 +24,9 @@ import { BaseCommand } from './BaseCommand';
|
||||||
import { ExecutionRepository } from '@db/repositories';
|
import { ExecutionRepository } from '@db/repositories';
|
||||||
import { OwnershipService } from '@/services/ownership.service';
|
import { OwnershipService } from '@/services/ownership.service';
|
||||||
import { generateHostInstanceId } from '@/databases/utils/generators';
|
import { generateHostInstanceId } from '@/databases/utils/generators';
|
||||||
// eslint-disable-next-line import/no-extraneous-dependencies
|
import type { ICredentialsOverwrite } from '@/Interfaces';
|
||||||
import { IConfig } from '@oclif/config';
|
import { CredentialsOverwrites } from '@/CredentialsOverwrites';
|
||||||
|
import { rawBodyReader, bodyParser } from '@/middlewares';
|
||||||
|
|
||||||
export class Worker extends BaseCommand {
|
export class Worker extends BaseCommand {
|
||||||
static description = '\nStarts a n8n worker';
|
static description = '\nStarts a n8n worker';
|
||||||
|
@ -46,12 +47,7 @@ export class Worker extends BaseCommand {
|
||||||
|
|
||||||
static jobQueue: JobQueue;
|
static jobQueue: JobQueue;
|
||||||
|
|
||||||
readonly uniqueInstanceId: string;
|
readonly uniqueInstanceId = generateHostInstanceId('worker');
|
||||||
|
|
||||||
constructor(argv: string[], cmdConfig: IConfig) {
|
|
||||||
super(argv, cmdConfig);
|
|
||||||
this.uniqueInstanceId = generateHostInstanceId('worker');
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Stop n8n in a graceful way.
|
* Stop n8n in a graceful way.
|
||||||
|
@ -360,9 +356,40 @@ export class Worker extends BaseCommand {
|
||||||
},
|
},
|
||||||
);
|
);
|
||||||
|
|
||||||
server.listen(port, () => {
|
let presetCredentialsLoaded = false;
|
||||||
this.logger.info(`\nn8n worker health check via, port ${port}`);
|
const endpointPresetCredentials = config.getEnv('credentials.overwrite.endpoint');
|
||||||
});
|
if (endpointPresetCredentials !== '') {
|
||||||
|
// POST endpoint to set preset credentials
|
||||||
|
app.post(
|
||||||
|
`/${endpointPresetCredentials}`,
|
||||||
|
rawBodyReader,
|
||||||
|
bodyParser,
|
||||||
|
async (req: express.Request, res: express.Response) => {
|
||||||
|
if (!presetCredentialsLoaded) {
|
||||||
|
const body = req.body as ICredentialsOverwrite;
|
||||||
|
|
||||||
|
if (req.contentType !== 'application/json') {
|
||||||
|
ResponseHelper.sendErrorResponse(
|
||||||
|
res,
|
||||||
|
new Error(
|
||||||
|
'Body must be a valid JSON, make sure the content-type is application/json',
|
||||||
|
),
|
||||||
|
);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
CredentialsOverwrites().setData(body);
|
||||||
|
presetCredentialsLoaded = true;
|
||||||
|
ResponseHelper.sendSuccessResponse(res, { success: true }, true, 200);
|
||||||
|
} else {
|
||||||
|
ResponseHelper.sendErrorResponse(
|
||||||
|
res,
|
||||||
|
new Error('Preset credentials can be set once'),
|
||||||
|
);
|
||||||
|
}
|
||||||
|
},
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
server.on('error', (error: Error & { code: string }) => {
|
server.on('error', (error: Error & { code: string }) => {
|
||||||
if (error.code === 'EADDRINUSE') {
|
if (error.code === 'EADDRINUSE') {
|
||||||
|
@ -372,6 +399,10 @@ export class Worker extends BaseCommand {
|
||||||
process.exit(1);
|
process.exit(1);
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
|
await new Promise<void>((resolve) => server.listen(port, () => resolve()));
|
||||||
|
await this.externalHooks.run('worker.ready');
|
||||||
|
this.logger.info(`\nn8n worker health check via, port ${port}`);
|
||||||
}
|
}
|
||||||
|
|
||||||
// Make sure that the process does not close
|
// Make sure that the process does not close
|
||||||
|
|
Loading…
Reference in a new issue