Esempio n. 1
0
    /// <summary>
    /// Creation of a job to create the next reminder
    /// </summary>
    /// <returns>A <see cref="Task"/> representing the asynchronous operation.</returns>
    public async Task CreateNextReminderJobAsync()
    {
        var timeStamp = DateTime.Now.AddHours(2);

        await using (var unused = (await _lockFactory.CreateLockAsync().ConfigureAwait(false)).ConfigureAwait(false))
        {
            if (_currentJobName != null)
            {
                _jobScheduler.RemoveJob(_currentJobName);

                _currentJobName = null;
                _currentJob     = null;
            }

            using (var dbFactory = RepositoryFactory.CreateInstance())
            {
                var next = await dbFactory.GetRepository <FractalRegistrationRepository>()
                           .GetQuery()
                           .Where(obj => obj.AppointmentTimeStamp > timeStamp)
                           .GroupBy(obj => new
                {
                    obj.ConfigurationId,
                    obj.AppointmentTimeStamp
                })
                           .Select(obj => new
                {
                    TimeStamp = obj.Key.AppointmentTimeStamp,
                    Count     = obj.Count()
                })
                           .Where(obj => obj.Count >= 5)
                           .Select(obj => (DateTime?)obj.TimeStamp)
                           .OrderBy(obj => obj)
                           .FirstOrDefaultAsync()
                           .ConfigureAwait(false);

                if (next != null)
                {
                    _currentJob     = new FractalReminderJob(next.Value);
                    _currentJobName = _jobScheduler.AddJob(_currentJob, next.Value.AddHours(-2));
                }
            }
        }
    }