Example #1
0
        /// <summary>
        /// Aufruf durch JobScheduler
        /// </summary>
        /// <param name="id"></param>
        public void RunLottery(Guid id)
        {
            var lotteryService = new DrawingService(db, id);

            lottery = lotteryService.Lottery;

            // Nachsehen, ob es die Lottery noch gibt
            if (lottery == null)
            {
                logger.ErrorFormat("Lottery with id {0} does not exist.", id);
                RemoveJob(id);
                return;
            }

            // schauen, ob es passt
            var today = DateTime.Today;

            if (lottery.FirstDrawing > today)
            {
                // Vor der ersten Ausführung => nix machen
                logger.InfoFormat("Verlosung {0} startet erst am {1}", lottery.Name, lottery.FirstDrawing.ToShortDateString());
                return;
            }

            if (lottery.LastDrawing < today)
            {
                logger.InfoFormat("Verlosung {0} wurde beendet am {1}", lottery.Name, lottery.LastDrawing.ToShortDateString());
                // Jon löschen, wird nicht mehr benötigt
                RemoveJob(id);
                return;
            }

            logger.InfoFormat("Start Verlosung {0}", lottery.Name);

            lotteryService.InitLotPots();
            var rounds = lotteryService.ExecuteDrawing();

            db.SaveChanges();

            logger.InfoFormat("Ende Verlosung {0} - mit {1} Iterationen", lottery.Name, rounds);
            logger.InfoFormat("Start Mailversand {0}", lottery.Name);

            var drawing = new LotteryDrawing();

            drawing.Start   = DateTime.Now;
            drawing.Lottery = lottery;

            var mailService = new LotteryMailService(lotteryService);

            mailService.SendDrawingMails(drawing);
            logger.InfoFormat("Ende Mailversand {0}", lottery.Name);

            if (lottery.LastDrawing.Date == today)
            {
                // nach der letzen Verlosung den Job löschen
                RemoveJob(id);
            }
        }