@@ -22,17 +22,15 @@ import { getSpawnEnv } from '../env/resolveEnv'
2222import {
2323 getErrorCode ,
2424 getProjectRoot ,
25+ getRecentResponse ,
2526 getSamCliPathAndVersion ,
2627 getTerminalFromError ,
2728 isDotnetRuntime ,
28- registerTemplateBuild ,
29- throwIfTemplateIsBeingBuilt ,
30- unregisterTemplateBuild ,
3129 updateRecentResponse ,
3230} from './utils'
3331import { getConfigFileUri , validateSamBuildConfig } from './config'
3432import { runInTerminal } from './processTerminal'
35- import { buildMementoRootKey } from './constants'
33+ import { buildMementoRootKey , buildProcessMementoRootKey , globalIdentifier } from './constants'
3634import { SemVer } from 'semver'
3735
3836export interface BuildParams {
@@ -266,11 +264,12 @@ export async function runBuild(arg?: TreeNode): Promise<SamBuildResult> {
266264 isSuccess : true ,
267265 }
268266 } catch ( error ) {
269- await unregisterTemplateBuild ( params . template . uri . path )
270267 throw ToolkitError . chain ( error , 'Failed to build SAM template' , {
271268 details : { terminal : getTerminalFromError ( error ) , ...resolveBuildArgConflict ( buildFlags ) } ,
272269 code : getErrorCode ( error ) ,
273270 } )
271+ } finally {
272+ await unregisterTemplateBuild ( params . template . uri . path )
274273 }
275274}
276275
@@ -312,3 +311,34 @@ export async function resolveBuildFlags(buildFlags: string[], samCliVersion: Sem
312311 }
313312 return buildFlags
314313}
314+
315+ /**
316+ * Returns true if there's an ongoing build process for the provided template, false otherwise
317+ * @Param templatePath The path to the template.yaml file
318+ */
319+ function isBuildInProgress ( templatePath : string ) : boolean {
320+ const expirationDate = getRecentResponse ( buildProcessMementoRootKey , globalIdentifier , templatePath )
321+ if ( expirationDate ) {
322+ return Date . now ( ) < parseInt ( expirationDate )
323+ }
324+ return false
325+ }
326+
327+ /**
328+ * Throws an error if there's a build in progress for the provided template
329+ * @Param templatePath The path to the template.yaml file
330+ */
331+ export function throwIfTemplateIsBeingBuilt ( templatePath : string ) {
332+ if ( isBuildInProgress ( templatePath ) ) {
333+ throw new ToolkitError ( 'This template is already being built' , { code : 'BuildInProgress' } )
334+ }
335+ }
336+
337+ export async function registerTemplateBuild ( templatePath : string ) {
338+ const expirationDate = Date . now ( ) + 5 * 60 * 1000 // five minutes
339+ await updateRecentResponse ( buildProcessMementoRootKey , globalIdentifier , templatePath , expirationDate . toString ( ) )
340+ }
341+
342+ export async function unregisterTemplateBuild ( templatePath : string ) {
343+ await updateRecentResponse ( buildProcessMementoRootKey , globalIdentifier , templatePath , undefined )
344+ }
0 commit comments