Skip to content

Commit d10bc84

Browse files
committed
fix(JobRunner): provide better message when failed to clean up work dir in RunOnLocalMachine
1 parent 3aa464e commit d10bc84

File tree

1 file changed

+27
-16
lines changed

1 file changed

+27
-16
lines changed

src/PollinationSDK/Wrapper/JobRunner.cs

+27-16
Original file line numberDiff line numberDiff line change
@@ -22,16 +22,16 @@ public JobRunner(JobInfo job)
2222
{
2323
this.JobInfo = job;
2424
}
25-
25+
2626
public async Task<CloudJob> RunOnCloudAsync(Project project, Action<string> progressReporting, System.Threading.CancellationToken token)
2727
{
28-
28+
2929
CloudJob cloudJob = null;
3030
try
3131
{
3232
cloudJob = await ScheduleCloudJobAsync(project, this.Job, progressReporting, token);
3333
progressReporting?.Invoke(cloudJob.Status.Status.ToString());
34-
LogHelper.LogInfo( $"A new cloud job {cloudJob.Id} is started in project {project.Name}");
34+
LogHelper.LogInfo($"A new cloud job {cloudJob.Id} is started in project {project.Name}");
3535
}
3636
catch (Exception e)
3737
{
@@ -61,7 +61,7 @@ public static async Task<Job> UploadJobAssetsAsync(
6161
{
6262

6363
// check if all cloud path artifacts are within the same project
64-
var invalidCloudAssets = job.Arguments.SelectMany(_ => _.OfType<JobPathArgument>()).Where(_ => _.IsAssetUploaded() && _.CloudProjectSlug() != project.Slug).Select(_=> $"{_.ToUserFriendlyString(true)}").Distinct();
64+
var invalidCloudAssets = job.Arguments.SelectMany(_ => _.OfType<JobPathArgument>()).Where(_ => _.IsAssetUploaded() && _.CloudProjectSlug() != project.Slug).Select(_ => $"{_.ToUserFriendlyString(true)}").Distinct();
6565
if (invalidCloudAssets.Any())
6666
{
6767
var error = $"Following cloud assets cannot be uploaded to project {project.Slug}:\n\n{string.Join(Environment.NewLine, invalidCloudAssets)}";
@@ -76,7 +76,8 @@ public static async Task<Job> UploadJobAssetsAsync(
7676
// upload artifacts
7777
if (!string.IsNullOrEmpty(tempProjectDir))
7878
{
79-
Action<int> updateMessageProgress = (int p) => {
79+
Action<int> updateMessageProgress = (int p) =>
80+
{
8081
progressLogAction?.Invoke($"Preparing: [{p}%]");
8182
};
8283
await Helper.UploadDirectoryAsync(project, tempProjectDir, updateMessageProgress, cancellationToken);
@@ -90,7 +91,7 @@ public static async Task<Job> UploadJobAssetsAsync(
9091
LogHelper.LogInfo($"Canceled by user");
9192
return null;
9293
}
93-
94+
9495
// update Artifact to cloud's relative path after uploaded.
9596
var newJob = UpdateArtifactPath(project.Slug, job, subfolderPath);
9697

@@ -124,7 +125,7 @@ private async Task<CloudJob> ScheduleCloudJobAsync(
124125

125126
// Upload artifacts
126127
var newJob = await UploadJobAssetsAsync(project, job, this.JobInfo.SubFolderPath, progressLogAction, cancellationToken);
127-
128+
128129
// create a new Simulation
129130
var api = new JobsApi();
130131
progressLogAction?.Invoke($"Start running.");
@@ -174,13 +175,23 @@ public string RunOnLocalMachine(string workFolder, int workerNum, bool silentMod
174175

175176
var workName = this.Job.Name ?? "Unnamed";
176177
workName = new String(workName.Where(c => char.IsLetterOrDigit(c)).ToArray());
177-
178+
178179
var workDir = workFolder;
179-
if (!string.IsNullOrEmpty( this.JobInfo.SubFolderPath))
180+
if (!string.IsNullOrEmpty(this.JobInfo.SubFolderPath))
180181
workDir = Path.Combine(workDir, this.JobInfo.SubFolderPath);
181182
workDir = Path.GetFullPath(workDir);
182183
if (Directory.Exists(workDir))
183-
System.IO.Directory.Delete(workDir, true);
184+
{
185+
try
186+
{
187+
System.IO.Directory.Delete(workDir, true);
188+
}
189+
catch (Exception e)
190+
{
191+
throw new ArgumentException($"Failed to clean up the working folder: {workDir}. Please remove it manually!\n{e}");
192+
}
193+
}
194+
184195
Directory.CreateDirectory(workDir);
185196

186197
var recipeOwner = this.JobInfo.RecipeOwner;
@@ -189,7 +200,7 @@ public string RunOnLocalMachine(string workFolder, int workerNum, bool silentMod
189200

190201
var localArgs = this.Job.Arguments;
191202
var localArg = new LocalRunArguments(localArgs.FirstOrDefault()); //TODO: ignore parametric runs for now
192-
203+
193204
//localArg.Validate(userRecipe);
194205
var inputJson = localArg.SaveToFolder(workDir); //save args to input.json file
195206

@@ -236,7 +247,7 @@ public static RunStatusEnum CheckLocalJobStatus(string workDir)
236247
{
237248
//"C:\Users\mingo\simulation\Unnamed\Unnamed\__logs__\status.json"
238249
var logDir = Directory.GetDirectories(workDir, "*__logs__*", SearchOption.AllDirectories).FirstOrDefault();
239-
250+
240251
var statusFile = Path.Combine(logDir, "status.json");
241252
if (!File.Exists(statusFile))
242253
return RunStatusEnum.Unknown;
@@ -249,7 +260,7 @@ public static RunStatusEnum CheckLocalJobStatus(string workDir)
249260
return RunStatusEnum.Unknown;
250261

251262
var st = statusToken.ToObject<RunStatusEnum>();
252-
263+
253264
if (st == RunStatusEnum.Succeeded || st == RunStatusEnum.Failed)
254265
return st;
255266
else
@@ -286,7 +297,7 @@ public static string GetJobErrors(string workDir)
286297
private static string CheckRecipeInProject(string recipeSource, Project project)
287298
{
288299
var found = Helper.GetRecipeFromRecipeSourceURL(recipeSource, out var recOwner, out var recName, out var recVersion);
289-
if (!found)
300+
if (!found)
290301
{
291302
LogHelper.LogThrowError($"CheckRecipeInProject: invalid recipe source {recipeSource}");
292303
}
@@ -448,14 +459,14 @@ private static Job UpdateArtifactPath(string projSlug, Job job, string subFolder
448459
}
449460
}
450461

451-
462+
452463

453464
}
454465

455466
return newJob;
456467
}
457468

458-
469+
459470

460471

461472

0 commit comments

Comments
 (0)