Skip to content

Commit

Permalink
Merge pull request #201 from snyk/chore/CLI-490_extraDebugLogs
Browse files Browse the repository at this point in the history
chore: adds extra debug logs
  • Loading branch information
sandor-trombitas authored Aug 28, 2024
2 parents 4392d70 + 3068089 commit 97c33a4
Showing 1 changed file with 29 additions and 2 deletions.
31 changes: 29 additions & 2 deletions snykTask/src/install/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,10 @@ export function getSnykDownloadInfo(
platform: Platform,
versionString: string = 'stable',
): SnykDownloads {
console.log(
`Getting Snyk download info for platform: ${platform} version: ${versionString}`,
);

const baseUrl = 'https://downloads.snyk.io';
const fallbackUrl = 'https://static.snyk.io';
const distributionChannel = sanitizeVersionInput(versionString);
Expand Down Expand Up @@ -65,6 +69,7 @@ export async function downloadExecutable(
maxRetries = 5,
) {
const filePath = path.join(targetDirectory, executable.filename);
console.log(`Downloading executable to: ${filePath}`);

// Check if the file already exists
if (fs.existsSync(filePath)) {
Expand All @@ -79,12 +84,24 @@ export async function downloadExecutable(
});

// Wrapping the download in a function for easy retrying
const doDownload = (url, filename) =>
const doDownload = (urlString, filename) =>
new Promise<void>((resolve, reject) => {
const url = new URL(urlString);
const requestOpts: https.RequestOptions = {
host: url.hostname,
path: url.pathname,
timeout: 300000, // 5mins
};
https
.get(url, (response) => {
.get(requestOpts, (response) => {
const isResponseError = response.statusCode !== 200;

response.on('finish', () => {
console.log(`Response finished for ${urlString}`);
});
response.on('close', () => {
console.log(`Download connection closed for ${urlString}`);
});
response.on('error', (err) => {
console.error(`Download of ${filename} failed: ${err.message}`);
reject(err);
Expand All @@ -105,6 +122,10 @@ export async function downloadExecutable(

response.pipe(fileWriter);
})
.on('timeout', () => {
console.error(`Download of ${filename} timed out`);
reject();
})
.on('error', (err) => {
console.error(`Request for ${filename} failed: ${err.message}`);
reject(err);
Expand All @@ -114,6 +135,9 @@ export async function downloadExecutable(
// Try to download the file, retry up to `maxRetries` times if the attempt fails
for (let attempt = 0; attempt < maxRetries; attempt++) {
try {
console.log(
`Downloading: ${executable.filename} from: ${executable.downloadUrl}`,
);
await doDownload(executable.downloadUrl, executable.filename);
console.log(`Download successful for ${executable.filename}`);
return;
Expand All @@ -139,6 +163,9 @@ export async function downloadExecutable(
// Try to download the file from fallback url, retry up to `maxRetries` times if the attempt fails
for (let attempt = 0; attempt < maxRetries; attempt++) {
try {
console.log(
`Downloading: ${executable.filename} from: ${executable.downloadUrl}`,
);
await doDownload(executable.fallbackUrl, executable.filename);
console.log(`Download successful for ${executable.filename}`);
return;
Expand Down

0 comments on commit 97c33a4

Please sign in to comment.