Skip to content

Commit

Permalink
feat(core): Add support for ready hooks, and credentials overwrite en…
Browse files Browse the repository at this point in the history
…dpoint in workers (#6954)
  • Loading branch information
netroy committed Aug 17, 2023
1 parent e71ff76 commit e5986c5
Showing 1 changed file with 40 additions and 3 deletions.
43 changes: 40 additions & 3 deletions packages/cli/src/commands/worker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,9 @@ import { generateFailedExecutionFromError } from '@/WorkflowHelpers';
import { N8N_VERSION } from '@/constants';
import { BaseCommand } from './BaseCommand';
import { ExecutionRepository } from '@/databases/repositories';
import bodyParser from 'body-parser';
import { ICredentialsOverwrite } from '@/Interfaces';
import { CredentialsOverwrites } from '@/CredentialsOverwrites';

export class Worker extends BaseCommand {
static description = '\nStarts a n8n worker';
Expand Down Expand Up @@ -350,9 +353,39 @@ export class Worker extends BaseCommand {
},
);

server.listen(port, () => {
this.logger.info(`\nn8n worker health check via, port ${port}`);
});
let presetCredentialsLoaded = false;
const endpointPresetCredentials = config.getEnv('credentials.overwrite.endpoint');
if (endpointPresetCredentials !== '') {
// POST endpoint to set preset credentials
app.post(
`/${endpointPresetCredentials}`,
bodyParser.json(),
async (req: express.Request, res: express.Response) => {
if (!presetCredentialsLoaded) {
const body = req.body as ICredentialsOverwrite;

if (req.headers['content-type'] !== '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 }) => {
if (error.code === 'EADDRINUSE') {
Expand All @@ -362,6 +395,10 @@ export class Worker extends BaseCommand {
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
Expand Down

0 comments on commit e5986c5

Please sign in to comment.