11import { env , EventEmitter } from "vscode" ;
22import { getVariantAnalysis } from "./gh-api/gh-api-client" ;
3+ import { RequestError } from "@octokit/request-error" ;
34
45import {
56 isFinalVariantAnalysisStatus ,
@@ -27,6 +28,8 @@ export class VariantAnalysisMonitor extends DisposableObject {
2728 ) ;
2829 readonly onVariantAnalysisChange = this . _onVariantAnalysisChange . event ;
2930
31+ private readonly monitoringVariantAnalyses = new Set < number > ( ) ;
32+
3033 constructor (
3134 private readonly app : App ,
3235 private readonly shouldCancelMonitor : (
@@ -36,9 +39,37 @@ export class VariantAnalysisMonitor extends DisposableObject {
3639 super ( ) ;
3740 }
3841
42+ public isMonitoringVariantAnalysis ( variantAnalysisId : number ) : boolean {
43+ return this . monitoringVariantAnalyses . has ( variantAnalysisId ) ;
44+ }
45+
3946 public async monitorVariantAnalysis (
4047 variantAnalysis : VariantAnalysis ,
4148 ) : Promise < void > {
49+ if ( this . monitoringVariantAnalyses . has ( variantAnalysis . id ) ) {
50+ void extLogger . log (
51+ `Already monitoring variant analysis ${ variantAnalysis . id } ` ,
52+ ) ;
53+ return ;
54+ }
55+
56+ this . monitoringVariantAnalyses . add ( variantAnalysis . id ) ;
57+ try {
58+ await this . _monitorVariantAnalysis ( variantAnalysis ) ;
59+ } finally {
60+ this . monitoringVariantAnalyses . delete ( variantAnalysis . id ) ;
61+ }
62+ }
63+
64+ private async _monitorVariantAnalysis (
65+ variantAnalysis : VariantAnalysis ,
66+ ) : Promise < void > {
67+ const variantAnalysisLabel = `${ variantAnalysis . query . name } (${
68+ variantAnalysis . query . language
69+ } ) [${ new Date ( variantAnalysis . executionStartTime ) . toLocaleString (
70+ env . language ,
71+ ) } ]`;
72+
4273 let attemptCount = 0 ;
4374 const scannedReposDownloaded : number [ ] = [ ] ;
4475
@@ -61,11 +92,7 @@ export class VariantAnalysisMonitor extends DisposableObject {
6192 } catch ( e ) {
6293 const errorMessage = getErrorMessage ( e ) ;
6394
64- const message = `Error while monitoring variant analysis ${
65- variantAnalysis . query . name
66- } (${ variantAnalysis . query . language } ) [${ new Date (
67- variantAnalysis . executionStartTime ,
68- ) . toLocaleString ( env . language ) } ]: ${ errorMessage } `;
95+ const message = `Error while monitoring variant analysis ${ variantAnalysisLabel } : ${ errorMessage } ` ;
6996
7097 // If we have already shown this error to the user, don't show it again.
7198 if ( lastErrorShown === errorMessage ) {
@@ -75,6 +102,19 @@ export class VariantAnalysisMonitor extends DisposableObject {
75102 lastErrorShown = errorMessage ;
76103 }
77104
105+ if ( e instanceof RequestError && e . status === 404 ) {
106+ // We want to show the error message to the user, but we don't want to
107+ // keep polling for the variant analysis if it no longer exists.
108+ // Therefore, this block is down here rather than at the top of the
109+ // catch block.
110+ void extLogger . log (
111+ `Variant analysis ${ variantAnalysisLabel } no longer exists or is no longer accessible, stopping monitoring.` ,
112+ ) ;
113+ // Cancel monitoring on 404, as this probably means the user does not have access to it anymore
114+ // e.g. lost access to repo, or repo was deleted
115+ return ;
116+ }
117+
78118 continue ;
79119 }
80120
0 commit comments