Beispiel #1
0
 public async void StartBackgroundJobs()
 {
     AddBackgroundInformation("Background jobs", "Starting background jobs");
     if (BackgroundJobs == null)
     {
         try
         {
             InitializeBackgroundJobs();
         }
         catch (Exception e)
         {
             AddBackgroundError("Starting error", e);
         }
     }
     //AddBackgroundInformation("Background jobs", "Starting background jobs 2");
     foreach (var backgroundJob in BackgroundJobs)
     {
         //var thread = new Thread(new ParameterizedThreadStart(RunBackgroundEventLoop));
         var task = Task.Run(async() => RunBackgroundEventLoop(backgroundJob));
         BackgroundThreads.Add(task);
         //thread.Start(backgroundJob);
     }
     BackgroundManager.StartWorkers(); // starts background timers that process jobs in the queue
     AddBackgroundInformation("Background jobs", "Background jobs started");
 }
Beispiel #2
0
        public void Dispose()
        {
            try
            {
                Started = false;
                BackgroundManager.StopWorkers(); // this call cancellation token . cancel

                //if (BackgroundThreads != null)
                //{
                //    foreach (var t in BackgroundThreads)
                //    {
                //        //if (t.IsAlive)
                //        //{
                //        //    t.Abort();
                //        //}
                //        if (t != null)
                //        {
                //            t.c
                //        }
                //    }
                //}
            }
            catch (Exception e)
            {
                AddBackgroundError("dispose of background service", e);
            }
        }
Beispiel #3
0
        /// <summary>
        /// This methods runs for each registered background process and either runs it or adds it to the queue of jobs to run.
        /// This runs an infinite while loop for all background events and sleeps the amount of time determined by CalculateNextRunTime.
        /// Then it either executes the event or adds it to the queue of jobs to be done.
        /// The difference is that the first will wait for the result before sleeping the time allocated.
        /// Whereas the second will schedule the job as soon as it is done sleeping.
        /// </summary>
        /// <param name="jobObject"></param>
        private async Task RunBackgroundEventLoop(object jobObject)
        {
            //AddBackgroundInformation("Background jobs", "Background work 1");
            var job = (BackgroundJob)jobObject;

            job.EventNumber = job.Event.GetEventId();
            var firstTime = true;

            BackgroundWorker worker = null;

            if (job.Event.RunSynchronously == true)
            {
                worker = new BackgroundWorker(this);
            }

            while (true)
            {
                if (firstTime && job.Event.RunImmediatelyFirstTime)
                {
                    firstTime = false;
                }
                else
                {
                    /* First calculate the amount of time to wait before doing work */
                    job.NextRunTime = job.Event.CalculateNextRunTime(job.LastRunTime);

                    //TODO: This might not be what we want.
                    //      The next run time might depend on the result of previous run

                    AddBackgroundInformation(job.Event.Description, String.Format("Background process {0} is going to sleep until {1}", job.Event.Description, job.NextRunTime));
                    var sleepTime = job.NextRunTime.Subtract(DateTime.Now); /* Do this after adding background info because it takes time too */
                    if (sleepTime < TimeSpan.FromSeconds(0))
                    {
                        sleepTime = TimeSpan.FromMinutes(1);
                    }

                    Thread.Sleep(sleepTime);
                }

                job.LastRunTime = DateTime.Now;
                if (BackupService.BusyWithBackups == true)
                {
                    continue;
                }

                if (job.Event.RunSynchronously == true)
                {
                    await worker.DoWork(job, CancelToken);
                }
                else
                {
                    BackgroundManager.AddJobToQueue(job);
                }
            }
        }
Beispiel #4
0
        public BackgroundService(DataService dataService, UserContext userContext, BackgroundManager manager)
        {
            DataService       = dataService;
            UserContext       = userContext;
            BackgroundManager = manager;
            CancelToken       = BackgroundManager.Token;

            if (Started == false)
            {
                Setup();
            }
        }
Beispiel #5
0
        async Task Run(CancellationToken token)
        {
            //Logger.Info("Running background worker " + Thread.CurrentThread.ManagedThreadId);
            while (true)
            {
                //Logger.Info("WaitOne in " + Thread.CurrentThread.ManagedThreadId);
                BackgroundManager.MainEvent.WaitOne();
                //Logger.Info("WaitOne passed in" + Thread.CurrentThread.ManagedThreadId);

                if (token.IsCancellationRequested)
                {
                    //Logger.Info("Cancellation token received for " + Thread.CurrentThread.ManagedThreadId);
                    token.ThrowIfCancellationRequested(); // sets task status to cancel
                    break;                                // or sets task as RanToCompletion (i like this option).
                }

                //object item;
                //Logger.Info("Dequeing background job " + Thread.CurrentThread.ManagedThreadId);
                var backgroundJob = BackgroundManager.Dequeue();
                if (backgroundJob == null)
                {
                    //Logger.Info("Background job is null for " + Thread.CurrentThread.ManagedThreadId);

                    break;
                }
                while (backgroundJob != null)
                {
                    //Thread.Sleep(1000);
                    //Console.WriteLine("Main  event doing work 1 time - " + Thread.CurrentThread.ManagedThreadId);

                    if (backgroundJob != null)
                    {
                        //Logger.Info("Doing work in " + Thread.CurrentThread.ManagedThreadId + " for " + backgroundJob.Event.Description);
                        await DoWork(backgroundJob, token);

                        backgroundJob = BackgroundManager.Dequeue();
                    }
                    else
                    {
                        //Console.WriteLine("Thread " + Thread.CurrentThread.ManagedThreadId + " got nothing");
                    }
                }
            }
        }