private void ResetErroredOrCompletedJob() { if (_job.IsErrored() || _job.IsCompleted()) { Console.WriteLine($"[Worker]: Job {_job.BackgroundJobId} is being reset to initial state."); _job.ResetToInitialState(); var jobResetResult = _jobUpdateConductor.Update(_job); jobResetResult.ThrowIfAnyErrorsOrResultIsFalse(); } }
public IActionResult Post([FromRoute] long articleId, [FromBody] ArticleDto article) { article.Id = articleId; var authResult = _authorizationConductor.IsAuthorized(article.Id, CurrentUser.Id); if (authResult.HasErrorsOrResultIsFalse()) { return(Ok <ArticleDto>(null, authResult.Errors)); } var getResult = _readConductor.FindById(article.Id); if (getResult.HasErrorsOrResultIsNull()) { return(Ok(false, getResult.Errors)); } var updatedArticle = getResult.ResultObject; updatedArticle.Title = article.Title; updatedArticle.Body = article.Body; var updateResult = _updateConductor.Update(updatedArticle); if (updateResult.HasErrors) { return(Ok(updateResult.ResultObject, updateResult.Errors)); } return(Ok(true, null)); }
public IActionResult Post([FromRoute] long sectionId, [FromBody] SectionDto section) { section.Id = sectionId; var authResult = _authorizationConductor.IsAuthorized(sectionId, CurrentUser.Id); if (authResult.HasErrorsOrResultIsFalse()) { return(Ok <SectionDto>(null, authResult.Errors)); } var getResult = _readConductor.FindById(section.Id); if (getResult.HasErrorsOrResultIsNull()) { return(Ok(false, getResult.Errors)); } var updatedSection = getResult.ResultObject; updatedSection.Name = section.Name; var updateResult = _updateConductor.Update(updatedSection); if (updateResult.HasErrors) { return(Ok(updateResult.ResultObject, updateResult.Errors)); } return(Ok(true, null)); }
/// <summary> /// Updates the locking fields to be set, which denotes that the user locking the record should /// have exclusive access to modifying it for the lock's duration. This, however, does /// not prohibit the record from being modified by others. When a record is locked, the /// ValidateLock method below should be used to determine if the record can be modified. This will /// ensure that only the user that locked the record is actually able to modify its contents. /// </summary> /// <param name="id">The record id</param> /// <param name="lockUntil">The time the record should be locked until</param> /// <param name="lockedById">The id of the user locking the record</param> /// <returns>the updated record if it is successfully locked, null otherwise</returns> public virtual IResult <T> Lock(long id, DateTimeOffset lockUntil, long?lockedById) => Do <T> .Try(r => { var readResult = _repositoryReadConductor.FindById(id); if (readResult.HasErrors) { r.AddErrors(readResult.Errors); return(null); } if (readResult.ResultObject == null) { r.AddErrorAndLog( _logger, ERROR_LOCK_RECORD_NOT_FOUND, $"Read Result of {typeof(T).Name} id: {id} was null", id ); return(null); } var record = readResult.ResultObject; if (record.IsLocked) { r.AddErrorAndLog( _logger, ERROR_LOCK_RECORD_ALREADY_LOCKED, $"{typeof(T).Name} id: {id} is already locked", id ); return(null); } var now = DateTimeOffset.Now; if (lockUntil < now) { r.AddErrorAndLog( _logger, ERROR_LOCK_TIME_IN_PAST, $"LockedUntil time is in the past", id ); return(null); } record.LockedById = lockedById; record.LockedOn = DateTimeOffset.Now; record.LockedUntil = lockUntil; var updateResult = _repositoryUpdateConductor.Update(record, lockedById); if (updateResult.HasErrors) { r.AddErrors(updateResult.Errors); return(null); } return(record); }).Result;
public IResult <Job> Enqueue( Type workerType, List <object> workerArgs, long?startedById = null ) => Do <Job> .Try((r) => { Type typeToFind = workerType.IsInterface ? _serviceProvider.GetService(workerType).GetType() : workerType; var worker = _workers.ToList().Find((e) => e.GetType() == typeToFind); string workerName = worker?.Name; Console.WriteLine("[JobEnqueueConductor]: Enqueue called."); // Create a new job in initial state to enqueue. var job = new Job(); job.SetToInitialState(workerName, workerArgs, startedById); var createJobResult = _jobCreateConductor.Create(job); createJobResult.ThrowIfAnyErrorsOrResultIsNull(); job = createJobResult.ResultObject; // If we don't have a worker or worker name, record the error and return. if (worker == null || string.IsNullOrWhiteSpace(workerName)) { r.AddError(_localizer, ERROR_WORKER_NOT_FOUND, workerType.Name); job.SetToErrored(error: r.ListErrors()); var findWorkerCreateJobResult = _jobUpdateConductor.Update(job); findWorkerCreateJobResult.ThrowIfAnyErrorsOrResultIsNull(); return(job); } Console.WriteLine($"[JobEnqueueConductor]: Enqueueing '{workerName}'"); var backgroundJobId = _workerProvider.Enqueue( () => worker.Execute(job.Id, JobCancellationToken.Null) ); // The worker provider returns a string if the enqueue is successful. if (backgroundJobId.IsNullOrEmpty()) { r.AddError( _localizer, ERROR_BACKGROUND_JOB_COULD_NOT_BE_ENQUEUED, nameof(_workerProvider), nameof(workerName) ); job.SetToErrored <Job>(result: r); var nonEnqueuedJobResult = _jobUpdateConductor.Update(job); nonEnqueuedJobResult.ThrowIfAnyErrorsOrResultIsNull(); return(job); } job.BackgroundJobId = backgroundJobId; var enqueuedJob = _jobUpdateConductor.Update(job); enqueuedJob.ThrowIfAnyErrorsOrResultIsNull(); Console.WriteLine($"[JobEnqueueConductor]: '{workerName}' Job Id {job.Id} Complete"); return(job); }) .Result;