Describe the issue
In a couple of places "tmp$(([datetime]::Now).Ticks)" is used to create unique temp folders.
But ([datetime]::Now).Ticks seems not to be guaranteed to be unique when running in different threads.
I'm now about to rebuild our images nightly based on your code at in an other post. But to speed things up I'm building the images in parallell jobs in my pipeline (first I determine the versions to be used for current/nextminor/nextmajor), resulting in that the different agents are creating the temp folders at pretty much the same time.
And on a few occations this fails when the folder names get the exact same name in two different jobs:
New-Item : An item with the specified name F:\bcartifacts.cache\tmp637400422340201954 already exists.
At C:\Agent_AS011_03\_work\125\s\PowerShell\bccontainerhelper\ContainerHandling\New-NavImage.ps1:326 char:13
+ New-Item $buildFolder -ItemType Directory | Out-Null
And when I check out another job in the same pipeline run I see that this folder is already created:
Downloading f:\bcartifacts.cache\tmp637400422340201954\my\license.flf
Files in f:\bcartifacts.cache\tmp637400422340201954\my:
- license.flf
Copying Platform Artifacts
Copying Database
Copying Licensefile
Copying ConfigurationPackages
Copying Extensions
Copying Applications.SE
f:\bcartifacts.cache\tmp637400422340201954
Sending build context to Docker daemon 1.759GB
So how strange it may sound, ([datetime]::Now).Ticks is not unique.
So I suggest that this is replaced with something else that is unique, as a GUID or something else.
Damn. A Guid is too long - the paths underneath will hit the powershell limit.
I will replace with a mutex and create a short folder which doesn't exist.
Oh, now I understand your choice of implementation. :-)
What about using [System.IO.Path]::GetRandomFileName() as a foldername instead? (https://docs.microsoft.com/en-us/dotnet/api/system.io.path.getrandomfilename?view=netcore-3.1)
that is nice, but it actually doesn't state that it is unique.
I will do this:
do {
$buildFolder = Join-Path (Get-ContainerHelperConfig).bcartifactsCacheFolder ([System.IO.Path]::GetRandomFileName())
}
until (New-Item $buildFolder -ItemType Directory -ErrorAction SilentlyContinue)
Like we say in Denmark: Livrem og seler!
Even though 32^11 number of combinations should be enough, I'm with you.
In Sweden it is: H盲ngslen och livrem!
@freddydk Don't you think that Download-Artifacts.ps1 needs to be updated in a similar way?
I will switch to use getrandomfilename in download-artifacts, but in that one, I am not actually creating the folder.
I will trust the 32^11 in that case.