Exemple #1
0
        private static void Executor_JobDone(object sender, JobDoneEventArg e)
        {
            string log = null;
            string message;

            switch (e.CurrentJobState)
            {
            case JobState.Finished:
                var executionTime = e.ExecutionTime.HasValue ? $" in {e.ExecutionTime.Value} ms." : ".";
                log     = $"Finished job: {e.JobResult?.MessageToShow ?? string.Empty}, ID: {e.CurrentJobId}";
                message = log + executionTime + Environment.NewLine;

                if (Executor.CanStartNewJob() && !AllJobsHaveFinished())
                {
                    Job newJob = PriorityQueue.Dequeue();
                    Executor.StartJob(newJob);
                }


                break;

            case JobState.Cancelled:
                message = $"Job: {e.CurrentJobType} (ID: {e.CurrentJobId}) was cancelled by user." + Environment.NewLine;
                log     = $"Canceled job: ID: {e.CurrentJobId}." + Environment.NewLine;
                if (Executor.CanStartNewJob() && !AllJobsHaveFinished())
                {
                    Job newJob = PriorityQueue.Dequeue();
                    Executor.StartJob(newJob);
                }

                break;

            default:
                return;
            }
            Debug.WriteLine(message);
            LogHelper.WriteLog(log);
        }
Exemple #2
0
        /// <summary>
        /// Schedules given job within PriorityQueue
        /// </summary>
        /// <param name="jobs">Jobs to schedule</param>
        public static void ScheduleJobs(params BaseJob[] jobs)
        {
            foreach (var job in jobs)
            {
                var log = $"Scheduling {job?.GetType()?.Name?.Replace("Job", string.Empty)} job (ID: {job.Id}) with {job.Priority} priority.";
                Debug.WriteLine(log);
                LogHelper.WriteLog(log);
                PriorityQueue.Enqueue(job);
            }

            // TODO perform some operation/s here
            while (!AllJobsHaveFinished())
            {
                if (Executor.CanStartNewJob())
                {
                    var job = PriorityQueue.Dequeue();
                    Executor.ExecuteJob(job);
                }
            }
        }