public void Run() { if (_status != JobStatus.Scheduled) { // Do nothing. Work is already started. return; } _status = JobStatus.Running; _lastStartTime = _context.GetCurrentTime(); _definition.Factory().ContinueWith(t => { _status = JobStatus.Pending; _lastCompleteTime = _context.GetCurrentTime(); var run = new JobRun { Started = _lastStartTime.Value, Completed = _lastCompleteTime.Value }; if (t.Status == TaskStatus.Faulted) { var ex = t.Exception.Flatten().GetBaseException(); run.Result = JobRunResult.Failure; run.ResultMessage = ex.ToString(); // We need to log this out. //_trace.Value.Error("Command " + cmdKey + " failed to execute.\r\n" + ex.Message, new { Exception = ex, Command = cmd }); } else if (t.Status == TaskStatus.RanToCompletion) { run.Result = JobRunResult.Success; } _previousRuns.Add(run); var newWorkState = new WorkState(_lastCompleteTime.Value); _context.State.Store(_definition.JobKey, newWorkState); }); }