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

Fix telemetry unsupported async #1131

Merged
merged 4 commits into from
Aug 18, 2022
Merged
Show file tree
Hide file tree
Changes from 3 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
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 () => {
0x2b3bfa0 marked this conversation as resolved.
Show resolved Hide resolved
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
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