Skip to content

Commit

Permalink
chore: use less verbose logging
Browse files Browse the repository at this point in the history
  • Loading branch information
rorlic committed Nov 28, 2023
1 parent 1ba9f94 commit 6c3e6b9
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 14 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,4 @@ node_modules
dist
coverage
TARGETURL
tmp
28 changes: 14 additions & 14 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import fetch from 'node-fetch';

const args = minimist(process.argv.slice(2));
const silent: boolean = (/true/i).test(args['silent']);
if (!silent) console.debug("Arguments: ", args);
if (!silent) console.warn("Arguments: ", args);

const cron = args['cron'] || '* * * * * *';
const mimeType = args['mimeType'];
Expand All @@ -27,6 +27,7 @@ const job = new CronJob(cron, async () => {
const targetUrl = args['targetUrl'] || (existsSync('./TARGETURL') && readFileSync('./TARGETURL', 'utf-8').trimEnd());
if (targetUrl && !mimeType) throw new Error('Missing mimeType');

const startTime = new Date().valueOf();
for await (const body of messages) {
messageCount++;

Expand All @@ -35,23 +36,17 @@ const job = new CronJob(cron, async () => {
let retries = 0;
do {
try {
if (!silent) {
const prefix = retry ? `Retrying (${retries} of ${maxRetries}) send` : "Sending";
const msg = `${prefix} message ${messageCount} to ${targetUrl} (size: ${body.length})`;
console.debug(msg);
if (retry) {
const msg = `Retrying (${retries} of ${maxRetries}) send message ${messageCount} to ${targetUrl}`;
console.warn(msg);
}

const response = await fetch(targetUrl, {
method: 'post',
body: body,
headers: {'Content-Type': mimeType}
});
if (silent) {
const postfix = retry ? ` (retry ${retries} of ${maxRetries})` : ' ';
const msg = `[${timestamp}] POST ${targetUrl} (msg: ${messageCount}) ${response.status}${postfix}`;
console.info(msg);
} else {
console.debug(`Response: ${response.statusText}`);
}

retry = nonFatalHttpCodes.includes(response.status);
} catch (error) {
const msg = error instanceof Error ? `${error.name}: ${error.message}, reason: ${error.cause}` : String(error);
Expand All @@ -64,8 +59,13 @@ const job = new CronJob(cron, async () => {
}
};

if (!silent) console.debug('Next run at: ', job.nextDate().toString());
if (!silent) {
const endTime = new Date().valueOf();
const deltaMillis = endTime - startTime;
const msg = `[${timestamp}] sent ${messages.length} messages to ${targetUrl} in ${deltaMillis} milliseconds.`;
console.warn(msg);
}
});

if (!silent) console.debug('Runs at: ', cron);
if (!silent) console.warn('Runs at: ', cron);
job.start();

0 comments on commit 6c3e6b9

Please sign in to comment.