Exemple #1
0
 private static WatchedProcessDto MapWatchedProcessToDto(WatchedProcess watchedProcess)
 {
     return(new WatchedProcessDto {
         Id = watchedProcess.Id,
         Name = watchedProcess.Name,
         TimeAllowedPerDay = watchedProcess.TimeAllowedPerDay,
         TimeLeft = watchedProcess.TimeLeft,
         LastWatchedDate = watchedProcess.LastWatchedDate,
         IsCurrentlyRunning = ProcessesHelper.IsProcessRunning(watchedProcess.Name)
     });
 }
Exemple #2
0
        public void UpdateTimeForWatchedProcess(string processName)
        {
            var process = this.repository.Filter <WatchedProcess>(x => x.Name == processName).FirstOrDefault();

            if (process == null)
            {
                throw new InvalidDataException("Process does not exist in the database!");
            }

            if (DateTime.Now.Date != process.LastWatchedDate)
            {
                process.LastWatchedDate = DateTime.Now.Date;
                process.TimeLeft        = process.TimeAllowedPerDay;
                this.repository.Update(process);
                this.logger.Debug($"Updated watched process '{process.Name}' - last watched: {process.LastWatchedDate.ToShortDateString()}, time left: {process.TimeLeft}.");
                return;
            }

            if (!ProcessesHelper.IsProcessRunning(processName))
            {
                return;
            }

            process.TimeLeft = process.TimeLeft.Add(TimeSpan.FromMinutes(-1));
            if (process.TimeLeft < new TimeSpan())
            {
                ProcessesHelper.KillProcess(process.Name);
                process.TimeLeft = new TimeSpan();
                this.repository.Update(process);
                this.logger.Debug($"Killed process '{process.Name}'.");
            }
            else
            {
                this.repository.Update(process);
            }
            this.logger.Debug($"Updated watched process '{process.Name}' - last watched: {process.LastWatchedDate.ToShortDateString()}, time left: {process.TimeLeft}.");
        }