private async Task ProcessJob() { _switch.SetStateAction(DoNothing); if (!_jobHandler.IsCompleted()) { var heartbeat = await Try.Do(() => _krakerApi.SendAgentStatus(_agentId, _jobHandler.GetJobDescription()), e => { _logger.Warning("Can't sent the status: {0}", e); return(WorkStatus.Continue); }); if (heartbeat.Status == Constants.WorkStatuses.Stop) { _logger.Information("The job is canceled"); _jobHandler.Cancel(); } _switch.SetStateAction(ProcessJob); return; } await _jobHandler.Finish(); _jobHandler = _incorrectJobHandler; _switch.SetStateAction(WaitJob); }