Skip to content

Commit

Permalink
chore: allow retries
Browse files Browse the repository at this point in the history
  • Loading branch information
rorlic committed Nov 14, 2023
1 parent b97df3e commit 24b4247
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 11 deletions.
6 changes: 5 additions & 1 deletion Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,11 @@ ARG MIMETYPE=
ENV MIMETYPE=${MIMETYPE}
ARG RANGE=
ENV RANGE=${RANGE}
ARG MAX_RETRIES=5
ENV MAX_RETRIES=${MAX_RETRIES}
ARG RETRY_TIMEOUT=10
ENV RETRY_TIMEOUT=${RETRY_TIMEOUT}
## set start command
ENTRYPOINT ["/usr/bin/dumb-init", "--"]
USER node
CMD ["sh", "-c", "node index.js --silent=${SILENT} --range=${RANGE} --mimeType=\"${MIMETYPE}\" --targetUrl=${TARGETURL} --cron=\"${CRON}\" --template=\"${TEMPLATE}\" --templateFile=${TEMPLATEFILE}"]
CMD ["sh", "-c", "node index.js --silent=${SILENT} --retryTimeout=${RETRY_TIMEOUT} --maxRFetries=${MAX_RETRIES} --range=${RANGE} --mimeType=\"${MIMETYPE}\" --targetUrl=${TARGETURL} --cron=\"${CRON}\" --template=\"${TEMPLATE}\" --templateFile=${TEMPLATEFILE}"]
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,8 @@ The generator takes the following command line arguments:
* `--template='<content>'` allows to provide the template on the command line, no default (if not provided, you MUST provide `--templateFile`)
* `--templateFile=<partial-or-full-pathname>` allows to provide the template in a file, no default (if not provided, you MUST provide `--template`)
* `--range=<range>` allows to create a range of values (1 to `<range>`) on every tick instead of an increasing index
* `--maxRetries=<number>` allows to retry some (possibly retryable) HTTP status codes (429, 500 & 503) a maximum number of times
* `--retryTimeout=<ms>` the amount of time (in milliseconds) to wait between retrying to send the message again
The template or template file should simply contain a message with mustache variables (between `{{` and `}}`). E.g.:
```json
Expand Down
43 changes: 33 additions & 10 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,28 +10,51 @@ if (!silent) console.debug("Arguments: ", args);

const cron = args['cron'] || '* * * * * *';
const mimeType = args['mimeType'];
const maxRetries = Number.parseInt(args['maxRetries'] || '5');
const retryTimeout = Number.parseInt(args['retryTimeout'] || '10');
let template: string = args['template'] || (args['templateFile'] && readFileSync(args['templateFile'], {encoding: 'utf8'}));
if (!template) throw new Error('Missing template or templateFile');

const generator: Generator = new Generator(template);
const range = Number.parseInt(args['range'] || '0');
const nonFatalHttpCodes : number[] = [429, 500, 503];
const sleep = (ms: number) => new Promise((resolve) => setTimeout(resolve, ms));
let messageCount = 0;

const job = new CronJob(cron, async () => {
const timestamp = new Date().toISOString();
const messages = range ? generator.createRange(range) : [generator.createNext()];
const targetUrl = args['targetUrl'] || (existsSync('./TARGETURL') && readFileSync('./TARGETURL', 'utf-8').trimEnd());
for await (const body of messages) {
const targetUrl = args['targetUrl'] || (existsSync('./TARGETURL') && readFileSync('./TARGETURL', 'utf-8').trimEnd());
++messageCount;

if (targetUrl) {
if (!mimeType) throw new Error('Missing mimeType');

if (!silent) console.debug(`Sending to '${targetUrl}':`, body);
const response = await fetch(targetUrl, {
method: 'post',
body: body,
headers: {'Content-Type': mimeType}
});
if (!silent) console.debug(`Response: ${response.statusText}`);
if (silent) console.info(`[${timestamp}] POST ${targetUrl} ${response.status}`);
let retry = false;
let retries = 0;
do {
try {
if (!silent) {
const msg = retry ? `Retrying (${retries} of ${maxRetries}) send` : "Sending";
console.debug(`${msg} message ${messageCount} to ${targetUrl}`, body);
}
const response = await fetch(targetUrl, {
method: 'post',
body: body,
headers: {'Content-Type': mimeType}
});
if (!silent) console.debug(`Response: ${response.statusText}`);
if (silent) {
const msg = retry ? ` (retry ${retries} of ${maxRetries})` : ' ';
console.info(`[${timestamp}] POST ${targetUrl} (msg: ${messageCount}) ${response.status}${msg}`);
}
retry = nonFatalHttpCodes.includes(response.status);
} catch (error) {
const msg = error instanceof Error ? `${error.name}: ${error.message}, reason: ${error.cause}` : String(error);
console.error(`ERROR: cannot POST to ${targetUrl} because: ${msg}`);
retry = true;
}
} while (retry && (++retries <= maxRetries) && (await sleep(retryTimeout), true));
} else { // if no targetUrl specified, send to console
console.info(body);
}
Expand Down

0 comments on commit 24b4247

Please sign in to comment.