Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(core): Add support for ready hooks, and credentials overwrite endpoint in workers #6954

Merged
merged 3 commits into from
Aug 17, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
53 changes: 42 additions & 11 deletions packages/cli/src/commands/worker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,9 @@ import { BaseCommand } from './BaseCommand';
import { ExecutionRepository } from '@db/repositories';
import { OwnershipService } from '@/services/ownership.service';
import { generateHostInstanceId } from '@/databases/utils/generators';
// eslint-disable-next-line import/no-extraneous-dependencies
import { IConfig } from '@oclif/config';
import type { ICredentialsOverwrite } from '@/Interfaces';
import { CredentialsOverwrites } from '@/CredentialsOverwrites';
import { rawBodyReader, bodyParser } from '@/middlewares';

export class Worker extends BaseCommand {
static description = '\nStarts a n8n worker';
Expand All @@ -46,12 +47,7 @@ export class Worker extends BaseCommand {

static jobQueue: JobQueue;

readonly uniqueInstanceId: string;

constructor(argv: string[], cmdConfig: IConfig) {
super(argv, cmdConfig);
this.uniqueInstanceId = generateHostInstanceId('worker');
}
readonly uniqueInstanceId = generateHostInstanceId('worker');

/**
* Stop n8n in a graceful way.
Expand Down Expand Up @@ -360,9 +356,40 @@ 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 !== '') {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

its basically a copy of code here right? lgmt 👍

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yeah. I'd like to refactor worker to also use AbstractServer, but that's a much larger change that I'd like to avoid in this PR.

// 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') {
netroy marked this conversation as resolved.
Show resolved Hide resolved
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 @@ -372,6 +399,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
Loading