Batch icon indicating copy to clipboard operation
Batch copied to clipboard

Delete task automatically with retention

Open andrew-vdb opened this issue 7 years ago • 4 comments

Just like job, we should be able to delete task, preferable with some retention, deleted after x hours/x days for example, allowing us to inspect the task first before its deleted completely.

andrew-vdb avatar Dec 19 '18 07:12 andrew-vdb

You can specify a task constraint with a retention time which will automatically clean up disk space consumed by the task on the respective compute node(s). Note that this doesn't delete the task, only the associated data on the compute node(s) for the task.

It is unclear where such capability (auto deletion after retention) exists for jobs.

alfpark avatar Jan 16 '19 22:01 alfpark

We created weekly cron job at this moment to clean up tasks. As you said, task constraint doesn't delete task. This is more feature request than question...

andrew-vdb avatar Jan 17 '19 08:01 andrew-vdb

@andrew-vdb , Could you please help me with your cron script (removing secure information) to delete jobs. This will help me and others alot.

Thank you in advance.

chanakyaMT avatar Jul 29 '19 19:07 chanakyaMT

public async Task CleanupCompletedTasks(int taskStorageDays = -3,int failureStorageDays = -7)
        {
            using (BatchClient batchClient = BatchClient.Open(credentials))
            {
                var listJobs = await batchClient.JobOperations.ListJobs().ToListAsync();
                foreach (var job in listJobs)
                {


                    var completedTasks = await job
                        .ListTasks(new ODATADetailLevel($"state eq 'completed' ")).ToListAsync();


                    List<Task> tasks = new List<Task>();
                    foreach (var task in completedTasks)
                    {
                        if (task.ExecutionInformation.ExitCode == 0 && task.CreationTime < DateTime.Today.AddDays(taskStorageDays))
                        {
                            
                            tasks.Add(task.DeleteAsync());
                        }
                       else if (task.CreationTime < DateTime.Today.AddDays(failureStorageDays))
                        {
                            tasks.Add(task.DeleteAsync());
                        }
                    }
                    await Task.WhenAll(tasks);
                }
            }
        }

andrew-vdb avatar Jul 30 '19 07:07 andrew-vdb