private void StartWorker(IDeploymentTargetWorker deploymentTargetWorker, CancellationToken stoppingToken) { try { TryStartWorker(deploymentTargetWorker, stoppingToken); } catch (Exception ex) { _logger.Error(ex, "Could not start worker for target id {TargetId}", deploymentTargetWorker.TargetId); } }
private void TryStartWorker(IDeploymentTargetWorker deploymentTargetWorker, CancellationToken stoppingToken) { _logger.Debug("Trying to start worker for target id {TargetId}", deploymentTargetWorker.TargetId); if (_tasks.ContainsKey(deploymentTargetWorker.TargetId)) { if (deploymentTargetWorker.IsRunning) { _logger.Debug("Worker for target id {TargetId} is already running", deploymentTargetWorker.TargetId); return; } Task task = _tasks[deploymentTargetWorker.TargetId]; if (!task.IsCompleted && _cancellations.ContainsKey(deploymentTargetWorker.TargetId)) { CancellationTokenSource tokenSource = _cancellations[deploymentTargetWorker.TargetId]; try { if (!tokenSource.IsCancellationRequested) { tokenSource.Cancel(); } tokenSource.Dispose(); } catch (ObjectDisposedException) { // ignore } _cancellations.Remove(deploymentTargetWorker.TargetId); } _tasks.Remove(deploymentTargetWorker.TargetId); } else { _logger.Debug("Start worker task was not found for target id {TargetId}", deploymentTargetWorker.TargetId); } var cancellationTokenSource = _timeoutHelper.CreateCancellationTokenSource(); var linked = CancellationTokenSource.CreateLinkedTokenSource(stoppingToken, cancellationTokenSource.Token); _cancellations.TryAdd(deploymentTargetWorker.TargetId, cancellationTokenSource); _tasks.Add(deploymentTargetWorker.TargetId, Task.Run(() => deploymentTargetWorker.ExecuteAsync(linked.Token), linked.Token)); }
public WorkerCreated(IDeploymentTargetWorker worker) => Worker = worker;