Skip to content

Commit

Permalink
Simplify duplicate telemetry sending logic
Browse files Browse the repository at this point in the history
  • Loading branch information
0x2b3bfa0 authored Aug 9, 2022
1 parent 6271cb8 commit 9842062
Show file tree
Hide file tree
Showing 9 changed files with 42 additions and 46 deletions.
59 changes: 34 additions & 25 deletions bin/cml.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ const yargs = require('yargs');

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

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 @@ -74,8 +71,8 @@ const runPlugin = async ({ $0: executable, command }) => {
};

const handleError = async (message, error, yargs) => {
if (error) {
const { telemetryEvent, cml } = OPTS;
if (error && yargs.parsed) {
const { telemetryEvent, cml } = await yargs.parsed.argv;
const event = { ...telemetryEvent, error: error.message };
await cml.telemetrySend({ event });
return;
Expand All @@ -85,6 +82,11 @@ const handleError = async (message, error, yargs) => {
console.error('\n' + message);
};

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

process.on('uncaughtException', async (err) => {
await handleError('', err, yargs);
});
Expand All @@ -93,23 +95,30 @@ 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();
handleSuccess(
yargs
.env('CML')
.options({
log: {
type: 'string',
description: 'Maximum log level',
choices: ['error', 'warn', 'info', 'debug'],
default: 'info'
}
})
.middleware(setupOpts)
.middleware(setupLogger)
.middleware(setupTelemetry)
.fail(handleError)
.commandDir('./cml', { exclude: /\.test\.js$/ })
.command(
'$0 <command>',
false,
(builder) => builder.strict(false),
runPlugin
)
.recommendCommands()
.demandCommand()
.strict()
.parse()
);
3 changes: 1 addition & 2 deletions bin/cml/ci.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,8 @@ exports.command = 'ci';
exports.description = 'Fixes specific CI setups';

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

exports.builder = (yargs) =>
Expand Down
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
3 changes: 0 additions & 3 deletions bin/cml/runner.js
Original file line number Diff line number Diff line change
Expand Up @@ -419,11 +419,8 @@ exports.command = 'runner';
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

1 comment on commit 9842062

@github-actions
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Test Comment

CML watermark

Please sign in to comment.