Example #1
0
        public void TestJobInterruption()
        {
            // create a simple scheduler

            NameValueCollection config = new NameValueCollection();

            config["quartz.scheduler.instanceName"] = "InterruptableJobTest_Scheduler";
            config["quartz.scheduler.instanceId"]   = "AUTO";
            config["quartz.threadPool.threadCount"] = "2";
            config["quartz.threadPool.type"]        = "Quartz.Simpl.SimpleThreadPool";
            IScheduler sched = new StdSchedulerFactory(config).GetScheduler();

            sched.Start();

            // add a job with a trigger that will fire immediately

            IJobDetail job = JobBuilder.Create <TestInterruptableJob>()
                             .WithIdentity("j1")
                             .Build();

            ITrigger trigger = TriggerBuilder.Create()
                               .WithIdentity("t1")
                               .ForJob(job)
                               .StartNow()
                               .Build();

            sched.ScheduleJob(job, trigger);

            sync.WaitOne(); // make sure the job starts running...

            IList <IJobExecutionContext> executingJobs = sched.GetCurrentlyExecutingJobs();

            Assert.AreEqual(1, executingJobs.Count, "Number of executing jobs should be 1 ");

            IJobExecutionContext jec = executingJobs[0];

            bool interruptResult = sched.Interrupt(jec.FireInstanceId);

            sync.WaitOne(); // wait for the job to terminate

            Assert.IsTrue(interruptResult, "Expected successful result from interruption of job ");
            Assert.IsTrue(TestInterruptableJob.interrupted, "Expected interrupted flag to be set on job class ");

            sched.Clear();
            sched.Shutdown();
        }
Example #2
0
        public ProcessAttendanceResult ProcessAtt(string dtFrom, string dtTo, string strEmpIds, string machineName, string ipAddress)
        {
            log4net.Config.XmlConfigurator.Configure();



            var dtF = dtFrom.ToDate();

            if (!dtF.HasValue)
            {
                _log.Info("Client Machine Attendance Processing Request Failed: Conversion Error");
                return(CouldNotConvertToDateException(dtFrom));
            }
            var dtT = dtTo.ToDate();

            if (!dtT.HasValue)
            {
                _log.Info("Client Machine Attendance Processing Request Failed: Conversion Error");
                return(CouldNotConvertToDateException(dtTo));
            }
            string strMachineIP = "[" + machineName + " - " + ipAddress + "]";

            _log.Info(strMachineIP + " Attendance Processing Request Started at " + DateTime.Now + " for Date Range : " + dtF.ToString() + " To " + dtT.ToString());
            //------------------------------------------------------------------------------------------
            //Pause currently running scheduled attendance job to avoid db update concurrency conflicts
            //-------------------------------------------------------------------------------------------
            IScheduler sched = new StdSchedulerFactory().GetScheduler();
            //DataTable jobsTable = GetJobs(sched);
            JobKey updateJobKey  = null;
            var    executingJobs = sched.GetCurrentlyExecutingJobs();

            foreach (var job in executingJobs)
            {
                JobKey jobKey = job.JobDetail.Key;
                updateJobKey = jobKey;
                if (jobKey.Name == "updateAtt")
                {
                    sched.PauseJob(jobKey);
                    break;
                }
            }
            //---------------------------------------------------------------------------------------------
            // -----To do : If no job currently running check if its about to start and reschedule the job
            //---------------------------------------------------------------------------------------------
            string[] EmpIds = GetPostedEmpIds(strEmpIds);

            double empCount = 0;

            foreach (var empId in EmpIds)
            {
                int employeeId       = Convert.ToInt16(empId);
                EnrolledEmployee emp = unitOfWork.EnrolledEmployees.All()
                                       .Where(x => x.workstatus == true)
                                       .Where(x => x.isdeleted == false)
                                       .Where(x => x.id == employeeId)
                                       .FirstOrDefault();
                if (emp != null)
                {
                    string tableName = GetCurrentAttTable(emp, DateTime.Parse(dtF.ToString()));
                    foreach (DateTime punchDate in Common.Common.GetDateRange(DateTime.Parse(dtFrom.ToString()), DateTime.Parse(dtTo.ToString())))
                    {
                        empCount++;
                        ProcessRawData(emp, punchDate, punchDate.AddDays(1), tableName);
                        if (Common.Common._processedOk == false)
                        {
                            _log.Info("Exception encountered while processing attendance update for :" + strMachineIP + " at " + DateTime.Now, Common.Common._exception);
                        }
                    }
                }
            }
            _log.Info(strMachineIP + " Attendance Processing Request Finished at " + DateTime.Now + ".  No of Attendance Records Updated : " + empCount + " From " + dtFrom.ToString() + " to " + dtTo.ToString());
            //---------Resume paused job Here..---------------------
            if (updateJobKey != null)
            {
                sched.ResumeJob(updateJobKey);
            }

            return(new ProcessAttendanceResult
            {
                Count = empCount,
                Message = "Attendance has been processed successfully! Dates: " + dtFrom + " to  " + dtTo
            });
        }