Skip to content

Commit

Permalink
Fix telemetry unsupported async
Browse files Browse the repository at this point in the history
  • Loading branch information
DavidGOrtega committed Aug 16, 2022
1 parent 7a11074 commit 8fdafcf
Show file tree
Hide file tree
Showing 9 changed files with 54 additions and 67 deletions.
91 changes: 47 additions & 44 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 All @@ -47,14 +44,14 @@ const setupLogger = (opts) => {
winston.format.simple()
)
: winston.format.combine(
winston.format.errors({ stack: true }),
winston.format.errors({ stack: false }),
winston.format.json()
),
transports: [
new winston.transports.Console({
stderrLevels: Object.keys(winston.config.npm.levels),
handleExceptions: true,
handleRejections: true,
handleExceptions: false,
handleRejections: false,
level
})
]
Expand All @@ -73,43 +70,49 @@ const runPlugin = async ({ $0: executable, command }) => {
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
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

1 comment on commit 8fdafcf

@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.