Skip to content

Commit

Permalink
Fix telemetry unsupported async (#1131)
Browse files Browse the repository at this point in the history
* Fix telemetry unsupported async

* log settings

* nitpick

* fix AbortController
  • Loading branch information
DavidGOrtega authored Aug 18, 2022
1 parent 7a11074 commit d6c9a01
Show file tree
Hide file tree
Showing 12 changed files with 57 additions and 132 deletions.
88 changes: 46 additions & 42 deletions bin/cml.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,7 @@ const winston = require('winston');
const yargs = require('yargs');

const CML = require('../src/cml').default;
const { jitsuEventPayload } = require('../src/analytics');
let OPTS;
const { jitsuEventPayload, send } = require('../src/analytics');

const setupOpts = (opts) => {
const legacyEnvironmentVariables = {
Expand All @@ -33,8 +32,6 @@ const setupOpts = (opts) => {
opts.markdownFile = markdownfile;
opts.cmlCommand = opts._[0];
opts.cml = new CML(opts);

OPTS = opts;
};

const setupLogger = (opts) => {
Expand Down Expand Up @@ -68,48 +65,55 @@ const setupTelemetry = async (opts) => {

const runPlugin = async ({ $0: executable, command }) => {
if (command === undefined) throw new Error('no command');
const { argv } = process.argv;
const path = which.sync(`${basename(executable)}-${command}`);
const parameters = process.argv.slice(process.argv.indexOf(command) + 1); // HACK
const parameters = argv.slice(argv.indexOf(command) + 1); // HACK
await pseudoexec(path, parameters);
};

const handleError = async (message, error, yargs) => {
if (error) {
const { telemetryEvent, cml } = OPTS;
const event = { ...telemetryEvent, error: error.message };
await cml.telemetrySend({ event });
return;
const handleError = (message, error) => {
if (!error) {
yargs.showHelp();
console.error('\n' + message);
process.exit(1);
}

yargs.showHelp();
console.error('\n' + message);
};

process.on('uncaughtException', async (err) => {
await handleError('', err, yargs);
});

process.on('unhandledRejection', async (reason) => {
await handleError('', new Error(reason), yargs);
});

yargs
.env('CML')
.options({
log: {
type: 'string',
description: 'Maximum log level',
choices: ['error', 'warn', 'info', 'debug'],
default: 'info'
}
})
.fail(handleError)
.middleware(setupOpts)
.middleware(setupLogger)
.middleware(setupTelemetry)
.commandDir('./cml', { exclude: /\.test\.js$/ })
.command('$0 <command>', false, (builder) => builder.strict(false), runPlugin)
.recommendCommands()
.demandCommand()
.strict()
.parse();
(async () => {
try {
await yargs
.env('CML')
.options({
log: {
type: 'string',
description: 'Maximum log level',
choices: ['error', 'warn', 'info', 'debug'],
default: 'info'
}
})
.fail(handleError)
.middleware(setupOpts)
.middleware(setupLogger)
.middleware(setupTelemetry)
.commandDir('./cml', { exclude: /\.test\.js$/ })
.command(
'$0 <command>',
false,
(builder) => builder.strict(false),
runPlugin
)
.recommendCommands()
.demandCommand()
.strict()
.parse();

const { telemetryEvent } = yargs.parsed.argv;
await send({ event: telemetryEvent });
} catch (err) {
const { telemetryEvent } = yargs.parsed.argv;
const event = { ...telemetryEvent, error: err.message };
await send({ event });
winston.error({ err });
process.exit(1);
}
})();
3 changes: 1 addition & 2 deletions bin/cml/pr.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,9 @@ exports.command = 'pr <glob path...>';
exports.description = 'Create a pull request with the specified files';

exports.handler = async (opts) => {
const { cml, telemetryEvent: event, globpath: globs } = opts;
const { cml, globpath: globs } = opts;
const link = await cml.prCreate({ ...opts, globs });
console.log(link);
await cml.telemetrySend({ event });
};

exports.builder = (yargs) =>
Expand Down
4 changes: 1 addition & 3 deletions bin/cml/publish.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,12 @@ exports.handler = async (opts) => {
opts.native = true;
}

const { file, repo, native, asset: path, telemetryEvent: event } = opts;
const { file, repo, native, asset: path } = opts;
const cml = new CML({ ...opts, repo: native ? repo : 'cml' });
const output = await cml.publish({ ...opts, path });

if (!file) console.log(output);
else await fs.writeFile(file, output);

await cml.telemetrySend({ event });
};

exports.builder = (yargs) =>
Expand Down
3 changes: 1 addition & 2 deletions bin/cml/rerun-workflow.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,8 @@ exports.description = 'Reruns a workflow given the jobId or workflow Id';
const { repoOptions } = require('../../src/cml');

exports.handler = async (opts) => {
const { cml, telemetryEvent: event } = opts;
const { cml } = opts;
await cml.pipelineRerun(opts);
await cml.telemetrySend({ event });
};

exports.builder = (yargs) =>
Expand Down
2 changes: 0 additions & 2 deletions bin/cml/runner.js
Original file line number Diff line number Diff line change
Expand Up @@ -420,10 +420,8 @@ exports.description = 'Launch and register a self-hosted runner';

exports.handler = async (opts) => {
({ cml } = opts);
const { telemetryEvent: event } = opts;
try {
await run(opts);
await cml.telemetrySend({ event });
} catch (error) {
await shutdown({ ...opts, error });
}
Expand Down
5 changes: 2 additions & 3 deletions bin/cml/send-comment.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,8 @@ exports.command = 'send-comment <markdown file>';
exports.description = 'Comment on a commit';

exports.handler = async (opts) => {
const { cml, telemetryEvent: event } = opts;
console.log(await opts.cml.commentCreate(opts));
await cml.telemetrySend({ event });
const { cml } = opts;
console.log(await cml.commentCreate(opts));
};

exports.builder = (yargs) =>
Expand Down
5 changes: 2 additions & 3 deletions bin/cml/send-github-check.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,9 @@ exports.command = 'send-github-check <markdown file>';
exports.description = 'Create a check report';

exports.handler = async (opts) => {
const { cml, telemetryEvent: event, markdownfile } = opts;
const { cml, markdownfile } = opts;
const report = await fs.readFile(markdownfile, 'utf-8');
await opts.cml.checkCreate({ ...opts, report });
await cml.telemetrySend({ event });
await cml.checkCreate({ ...opts, report });
};

exports.builder = (yargs) =>
Expand Down
3 changes: 0 additions & 3 deletions bin/cml/tensorboard-dev.js
Original file line number Diff line number Diff line change
Expand Up @@ -92,9 +92,6 @@ exports.handler = async (opts) => {
const url = await launchAndWaitLink({ ...opts, extraParams });
if (!file) console.log(url);
else await fs.appendFile(file, url);

const { cml, telemetryEvent: event } = opts;
await cml.telemetrySend({ event });
};

exports.builder = (yargs) =>
Expand Down
64 changes: 0 additions & 64 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 0 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,6 @@
"strip-url-auth": "^1.0.1",
"tar": "^6.1.11",
"tempy": "^0.6.0",
"timeout-signal": "^1.1.0",
"timestring": "^6.0.0",
"unist-util-visit": "^2.0.3",
"uuid": "^8.3.2",
Expand Down
6 changes: 4 additions & 2 deletions src/analytics.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ const path = require('path');
const fs = require('fs').promises;

const fetch = require('node-fetch');
const timeoutSignal = require('timeout-signal');
const ProxyAgent = require('proxy-agent');
const { promisify } = require('util');
const { scrypt } = require('crypto');
Expand Down Expand Up @@ -187,8 +186,10 @@ const send = async ({
if (ITERATIVE_DO_NOT_TRACK) return;
if (!event.user_id || event.user_id === ID_DO_NOT_TRACK) return;

const controller = new AbortController();
const id = setTimeout(() => controller.abort(), 5 * 1000);
await fetch(endpoint, {
signal: timeoutSignal(5 * 1000),
signal: controller.signal,
method: 'POST',
headers: {
'X-Auth-Token': token,
Expand All @@ -197,6 +198,7 @@ const send = async ({
body: JSON.stringify(event),
agent: new ProxyAgent()
});
clearInterval(id);
} catch (err) {
winston.debug(`Send analytics failed: ${err.message}`);
}
Expand Down
5 changes: 0 additions & 5 deletions src/cml.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ const {
preventcacheUri,
waitForever
} = require('./utils');
const analytics = require('../src/analytics');

const { GITHUB_REPOSITORY, CI_PROJECT_URL, BITBUCKET_REPO_UUID } = process.env;

Expand Down Expand Up @@ -572,10 +571,6 @@ Automated commits for ${this.repo}/commit/${sha} created by CML.
return await this.getDriver().pipelineJobs(opts);
}

async telemetrySend({ event }) {
await analytics.send({ event });
}

logError(e) {
winston.error(e.message);
}
Expand Down

0 comments on commit d6c9a01

Please sign in to comment.