Exemplo n.º 1
0
        public async Task <CommandResult> ExecuteAsync(string username)
        {
            if (username.IsEmpty())
            {
                return(CommandResult.Failed(new CommandResultError()
                {
                    Code = (int)HttpStatusCode.BadRequest,
                    Message = HttpMessage.SomeDataEmptyOrInvalid
                }));
            }

            var recoveryCode = _randomGenerator.RandomNumber(0, 99999).ToString("D5");
            var user         = await _userRepository.Table
                               .Where(n => n.Username == username || n.Email == username || n.PhoneNumber == username)
                               .FirstOrDefaultAsync();

            if (user == null)
            {
                return(CommandResult.Failed(new CommandResultError()
                {
                    Code = (int)HttpStatusCode.NotFound,
                    Message = HttpMessage.NotFound
                }));
            }

            user.RecoveryCode        = recoveryCode;
            user.RecoveryRequestTime = DateTime.UtcNow;
            await _userRepository.UpdateAsync(user);

            _backgroundJobWrapper.Enqueue <ISendRecoveryCode>(job => job.ExecuteAsync(user.Email, recoveryCode));

            return(CommandResult.Success);
        }
        public IActionResult CreateBackgroundJobs()
        {
            // Queue Job not concurrency
            _backgroundJobWrapper.Enqueue <ISampleJob>(job => job.ExecuteAsync("Input value of Queue Job 1"));
            _backgroundJobWrapper.Enqueue <ISampleJob>(job => job.ExecuteAsync("Input value of Queue Job 2"));

            // Schedule Job
            _backgroundJobWrapper.Schedule <ISampleScheduleJob>(job => job.ExecuteAsync("Input value of Schedule Job"), DateTime.Now.AddMinutes(2));

            // Recurring Job
            HangfireServiceEntity service = new HangfireServiceEntity()
            {
                Id   = 123,
                Name = "ID - Run every minute",
                Cron = "* * * * *"
            };

            _recurringJobWrapper.AddOrUpdate <ISampleRecurringJob>(service.Name, job => job.ExecuteAsync("Input value of Recurring Job"), service.Cron, TimeZoneInfo.Local);

            return(new ObjectResult(new { success = true }));
        }