Ejemplo n.º 1
0
        protected async override Task ExecuteAsync(CancellationToken stoppingToken)
        {
            ThreadPool.SetMinThreads(MAX_CONCURRENT_SIZE, MAX_CONCURRENT_SIZE);

            Random random = new Random();

            TimeSpan sleepSpan;

            double runAtRange = JobSettings.MinPrepareTime.TotalSeconds;

            for (int i = 0; i < MAX_CONCURRENT_SIZE; i++)
            {
                Task.Run(() =>
                {
                    JobWorker jobWorker = new JobWorker(MAX_CONCURRENT_SIZE);
                });
            }

            await Task.Delay(1);


            using (JobsRepo repo = new JobsRepo())
            {
                while (stoppingToken.IsCancellationRequested == false)
                {
                    JobInfo tmpJobInfo = null;

                    foreach (JobInfo job in repo.GetReadyJobs(TimeSpan.FromSeconds(runAtRange)))
                    {
                        Queue.push(job);
                        //Console.WriteLine(job.Id + "\t" + job.RunAt);

                        tmpJobInfo = job;
                    }


                    //計算SLEEP多久
                    if (tmpJobInfo == null)
                    {
                        sleepSpan = TimeSpan.FromMilliseconds((10 * 1000) + random.Next(5 * 1000));

                        //Console.WriteLine("[random]" + sleepSpan.Seconds + "\t" + sleepSpan.TotalSeconds);
                    }
                    else
                    {
                        sleepSpan = TimeSpan.FromSeconds((tmpJobInfo.RunAt.AddSeconds(random.Next(15)) - DateTime.Now).Seconds + 1);

                        //Console.WriteLine("[next]" + sleepSpan.Seconds + "\t" + sleepSpan.TotalSeconds);
                    }

                    await Task.Delay(sleepSpan, stoppingToken);

                    Console.Write("_");
                }
            }
        }
        protected async override Task ExecuteAsync(CancellationToken stoppingToken)
        {
            ThreadPool.SetMinThreads(MAX_CONCURRENT_SIZE, MAX_CONCURRENT_SIZE);

            for (int i = 0; i < MAX_CONCURRENT_SIZE; i++)
            {
                Task.Run(() =>
                {
                    JobWorker jobWorker = new JobWorker(MAX_CONCURRENT_SIZE);
                });
            }

            await Task.Delay(1);

            TimeSpan sleepSpan;

            using (JobsRepo repo = new JobsRepo("Data Source=(LocalDB)\\MSSQLLocalDB;AttachDbFilename=D:\\workspace\\dotnet\\SchedulingPractice\\SubWorker.AndrewDemo\\JobsDB.mdf;Integrated Security=True"))
            {
                while (stoppingToken.IsCancellationRequested == false)
                {
                    JobInfo tmpJobInfo = null;

                    foreach (JobInfo job in repo.GetReadyJobs())
                    {
                        if (repo.AcquireJobLock(job.Id))
                        {
                            Console.Write("O");
                            Queue.push(job);
                            Console.WriteLine(job.Id + "\t" + job.RunAt);

                            tmpJobInfo = job;
                        }
                        else
                        {
                            Console.Write("X");
                        }
                    }


                    //計算SLEEP多久
                    if (tmpJobInfo == null)
                    {
                        Random random = new Random();
                        sleepSpan = TimeSpan.FromMilliseconds((10 * 1000) + random.Next(5 * 1000));

                        Console.WriteLine("[random]" + sleepSpan.Seconds + "\t" + sleepSpan.TotalSeconds);
                    }
                    else
                    {
                        sleepSpan = TimeSpan.FromSeconds((tmpJobInfo.RunAt.AddSeconds(10) - DateTime.Now).Seconds + 1);

                        Console.WriteLine("[next]" + sleepSpan.Seconds + "\t" + sleepSpan.TotalSeconds);

                        //Console.ReadKey();
                        //System.Environment.Exit(0);
                    }

                    //await Task.Delay(TimeSpan.FromSeconds(29), stoppingToken);
                    await Task.Delay(sleepSpan, stoppingToken);

                    //Console.ReadKey();
                    //System.Environment.Exit(0);
                    Console.Write("_");
                }
            }
        }