Skip to content

Commit

Permalink
Merge pull request #66 from RoadieHQ/sc-19687-liveness-probe
Browse files Browse the repository at this point in the history
Add liveness probe handling for custom scaffolder actions
  • Loading branch information
Xantier authored Mar 22, 2024
2 parents 03b1f41 + 5608494 commit 7bb09f6
Show file tree
Hide file tree
Showing 5 changed files with 34 additions and 1 deletion.
5 changes: 5 additions & 0 deletions .changeset/honest-laws-visit.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@roadiehq/roadie-agent': patch
---

Add an endpoint logic to handle custom scaffolder action liveness probe
9 changes: 8 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,11 @@ RoadieAgent.fromConfig()
handler: myEntityProviderHandler
}),
)
// Add second entity provider
// .addEntityProvider(...)

// Add a custom scaffolder action
// .addScaffolderAction(...)
.start();

```
Expand Down Expand Up @@ -162,11 +167,13 @@ RoadieAgent.fromConfig(config)
while (count < 5) { // Additional other actions that is wanted to be taken. This time looping for 5 seconds
await new Promise((resolve) => setTimeout(resolve, 1000));
count++;
ctx.log(`hello world`); // Sending a log message to be displayed to the enduser
await ctx.log(`hello world`); // Sending a log message to be displayed to the end user
}
},
}),
)
// Add a second custom scaffolder action
// .addScaffolderAction(...)
.start();

```
Expand Down
7 changes: 7 additions & 0 deletions src/lib/RoadieAgentReceiver.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import { getLogger } from '@/logger';
import { BaseLogger } from 'pino';
import { CustomScaffolderAction } from '@/scaffolderAction/CustomScaffolderAction';
import { createEntityEmitter } from '@/entityProvider/createEntityEmitter';
import { isActionRunning } from '@/scaffolderAction/runtimeContext';

export class RoadieAgentReceiver {
private server: Express;
Expand Down Expand Up @@ -93,6 +94,12 @@ export class RoadieAgentReceiver {
) {
// Specifying routes explicitly
app.post(`/scaffolder-action/${configuration.name}`, (req, res) => {
if (req.body.livenessProbe) {
this.logger.info(`Received scaffolder action liveness probe`);
return res.json({
ok: isActionRunning(req.body.actionId),
});
}
this.logger.info(
`Received scaffolder action trigger for endpoint ${configuration.name}`,
);
Expand Down
8 changes: 8 additions & 0 deletions src/lib/scaffolderAction/CustomScaffolderAction.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,10 @@ import {
downloadFile,
generateAndStreamZipfileToS3,
} from '@/scaffolderAction/workspaceHandler';
import {
addRunningAction,
removeRunningAction,
} from '@/scaffolderAction/runtimeContext';

export class CustomScaffolderAction {
private readonly brokerClientUrl: string;
Expand Down Expand Up @@ -52,6 +56,7 @@ export class CustomScaffolderAction {
async start() {
this.logger.info('Starting a custom scaffolder action');
try {
addRunningAction(this.actionId);
if (this.getPresign && this.getPresign !== '') {
await downloadFile(this.getPresign, this.localWorkspacePath);
}
Expand Down Expand Up @@ -85,6 +90,9 @@ export class CustomScaffolderAction {
payload,
actionId: this.actionId,
});

removeRunningAction(this.actionId);

const response = await fetch(url, {
method: 'POST',
body,
Expand Down
6 changes: 6 additions & 0 deletions src/lib/scaffolderAction/runtimeContext.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
let runningActions: string[] = [];

export const isActionRunning = (id: string) => runningActions.includes(id);
export const addRunningAction = (id: string) => runningActions.push(id);
export const removeRunningAction = (id: string) =>
(runningActions = runningActions.filter((it) => it !== id));

0 comments on commit 7bb09f6

Please sign in to comment.