Skip to content

Commit

Permalink
Remove redundant code
Browse files Browse the repository at this point in the history
  • Loading branch information
APErebus committed Dec 14, 2023
1 parent 4c0c8c5 commit 33fa2b1
Show file tree
Hide file tree
Showing 4 changed files with 6 additions and 22 deletions.
14 changes: 4 additions & 10 deletions source/Octopus.Tentacle/Kubernetes/Scripts/RunningKubernetesJob.cs
Original file line number Diff line number Diff line change
Expand Up @@ -80,12 +80,14 @@ public async Task Execute()
{
using var writer = ScriptLog.CreateWriter();

scriptCancellationToken.Register(() =>
//register a cancellation callback so that when the script is cancelled, we cancel the job
//we use a using to make sure this callback is deregistered
using var cancellationTokenRegistration = scriptCancellationToken.Register(() =>
{
writer.WriteOutput(ProcessOutputSource.StdOut, "Script execution canceled.");
writer.WriteVerbose($"Deleting Kubernetes Job '{jobName}'");

//we spawn the job cancellation on a background thread
//we spawn the job cancellation on a background thread (as this callback runs synchronously)
Task.Run(() => jobService.Delete(scriptTicket, CancellationToken.None), CancellationToken.None);
});

Expand Down Expand Up @@ -141,7 +143,6 @@ public async Task Execute()
async Task<int> MonitorJobAndLogs(IScriptLogWriter writer)
{
var jobCompletionCancellationTokenSource = new CancellationTokenSource();

var checkJobTask = CheckIfJobHasCompleted(jobCompletionCancellationTokenSource);

//we pass the job completion CTS here because its used to cancel the writing of the job stream
Expand Down Expand Up @@ -313,12 +314,5 @@ void RecordScriptHasCompleted(int exitCode)
}
}
}


public async Task Cancel()
{
// The cancel cannot be cancelled (via cancellation token)
await jobService.Delete(scriptTicket, CancellationToken.None);
}
}
}
2 changes: 0 additions & 2 deletions source/Octopus.Tentacle/Scripts/IRunningScript.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
using System;
using System.Threading.Tasks;
using Octopus.Tentacle.Contracts;

namespace Octopus.Tentacle.Scripts
Expand All @@ -9,6 +8,5 @@ public interface IRunningScript
int ExitCode { get; }
ProcessState State { get; }
IScriptLog ScriptLog { get; }
Task Cancel();
}
}
2 changes: 0 additions & 2 deletions source/Octopus.Tentacle/Scripts/RunningScript.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
using System;
using System.Threading;
using System.Threading.Tasks;
using Octopus.Diagnostics;
using Octopus.Tentacle.Contracts;
using Octopus.Tentacle.Util;
Expand Down Expand Up @@ -49,7 +48,6 @@ public RunningScript(IShell shell,
public IScriptLog ScriptLog { get; }

//Cancellation of the local shell is handled by the cancellation token supplied
public Task Cancel() => Task.CompletedTask;

public void Execute()
{
Expand Down
10 changes: 2 additions & 8 deletions source/Octopus.Tentacle/Services/Scripts/ScriptServiceV3Alpha.cs
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ public async Task<ScriptStatusResponseV3Alpha> CancelScriptAsync(CancelScriptCom
{
if (runningScripts.TryGetValue(command.ScriptTicket, out var runningScript))
{
await runningScript.Cancel();
runningScript.Cancel();
}

return await GetResponse(command.ScriptTicket, command.LastLogSequence, runningScript?.Process);
Expand Down Expand Up @@ -179,15 +179,9 @@ public RunningScriptWrapper(ScriptStateStore scriptStateStore)

public CancellationToken CancellationToken { get; }

public async Task Cancel()
public void Cancel()
{
cancellationTokenSource.Cancel();

//We don't want to just rel on the cancellation token performing the cancellation
//This is because the K8s Jobs need to be cancelled/deleted via an async API call
//and CancellationTokens cancellation registrations are synchronous callbacks
if (Process is not null)
await Process.Cancel();
}

public void Dispose()
Expand Down

0 comments on commit 33fa2b1

Please sign in to comment.