Exemple #1
0
        protected void worker_DoWork(object sender, DoWorkEventArgs e)
        {
            int wait = 15;
            for (int i = 0; i < wait * 100; i++)
            {
                // The sleep should happen on the worker thread rather than the application thread
                System.Threading.Thread.Sleep(10);
                if (e.Cancel)
                {
                    // Poll the thread every [wait] seconds to work out if the worker should be cancelled
                    return;
                }
            }

            //HttpContext.Current = (HttpContext)e.Argument;

            Mysql db = new Mysql(WebConfigurationManager.AppSettings["mysql-user"],
                WebConfigurationManager.AppSettings["mysql-password"],
                WebConfigurationManager.AppSettings["mysql-database"],
                WebConfigurationManager.AppSettings["mysql-host"]);

            Core core = new Core(db);

            string processViews = WebConfigurationManager.AppSettings["queue-process-views"];

            if (!string.IsNullOrEmpty(processViews) && processViews.ToLower() == "true")
            {
                try
                {
                    ItemView.ProcessViews(core);
                }
                catch (Exception ex)
                {
                    InsertQuery iQuery = new InsertQuery(typeof(ApplicationError));
                    iQuery.AddField("error_title", "An Error occured at " + Hyperlink.Domain + " in global.asax");
                    iQuery.AddField("error_body", "EXCEPTION THROWN:\n" + ex.ToString());
                    core.Db.Query(iQuery);
                }
            }

            string cronApplication = WebConfigurationManager.AppSettings["queue-cron-application"];
            if (!string.IsNullOrEmpty(cronApplication))
            {
                List<ApplicationEntry> aes = new List<ApplicationEntry>();

                if (cronApplication == "*")
                {
                    aes.AddRange(core.GetCronApplications());
                }
                else
                {
                    ApplicationEntry ae = core.GetApplication(cronApplication);
                    if (ae != null)
                    {
                        BoxSocial.Internals.Application.LoadApplication(core, AppPrimitives.Any, ae);
                        aes.Add(ae);
                    }
                }

                foreach (ApplicationEntry ae in aes)
                {
                    if (UnixTime.UnixTimeStamp() % ae.CronFrequency < 15)
                    {
                        Application jobApplication = BoxSocial.Internals.Application.GetApplication(core, AppPrimitives.Any, ae);

                        if (jobApplication != null)
                        {
                            try
                            {
                                if (!jobApplication.ExecuteCron())
                                {
                                    StringBuilder failedCronLog = new StringBuilder();
                                    failedCronLog.AppendLine("Application Id: " + ae.ApplicationId);

                                    InsertQuery iQuery = new InsertQuery(typeof(ApplicationError));
                                    iQuery.AddField("error_title", "Cron failed at " + Hyperlink.Domain);
                                    iQuery.AddField("error_body", "FAILED CRON:\n" + failedCronLog.ToString());
                                    core.Db.Query(iQuery);
                                }
                            }
                            catch (Exception ex)
                            {
                                InsertQuery iQuery = new InsertQuery(typeof(ApplicationError));
                                iQuery.AddField("error_title", "An Error occured at " + Hyperlink.Domain + " in global.asax");
                                iQuery.AddField("error_body", "EXCEPTION THROWN:\n" + ex.ToString() + "\n\n" + core.Console.ToString());
                                core.Db.Query(iQuery);
                            }
                        }
                    }
                }
            }

            try
            {
                bool flag = false;
                lock (queueLock)
                {
                    if (queue != null)
                    {
                        flag = true;
                    }
                }

                if (flag)
                {
                    int failedJobs = 0;

                    // Retrieve Jobs
                    Dictionary<string, int> queues = new Dictionary<string, int>();
                    queues.Add(WebConfigurationManager.AppSettings["queue-default-priority"], 10);

                    StringBuilder failedJobsLog = new StringBuilder();

                    foreach (string queueName in queues.Keys)
                    {
                        List<Job> jobs = null;
                        lock (queueLock)
                        {
                            try
                            {
                                jobs = Queue.ClaimJobs(queueName, queues[queueName]);
                            }
                            catch
                            {
                                return;
                            }
                        }

                        foreach (Job job in jobs)
                        {
                            core.LoadUserProfile(job.UserId);
                        }

                        foreach (Job job in jobs)
                        {
                            core.CreateNewSession(core.PrimitiveCache[job.UserId]);

                            // Load Application
                            if (job.ApplicationId > 0)
                            {
                                ApplicationEntry ae = core.GetApplication(job.ApplicationId);
                                BoxSocial.Internals.Application.LoadApplication(core, AppPrimitives.Any, ae);
                            }

                            // Execute Job
                            if (core.InvokeJob(job))
                            {
                                lock (queueLock)
                                {
                                    Queue.DeleteJob(job);
                                }
                            }
                            else
                            {
                                failedJobs++;
                                //queue.ReleaseJob(job);
            #if DEBUG
                                failedJobsLog.AppendLine("Application Id: " + job.ApplicationId);
                                failedJobsLog.AppendLine("Item Id: " + job.ItemId);
                                failedJobsLog.AppendLine("Item Type Id: " + job.ItemTypeId);
                                failedJobsLog.AppendLine("Function: " + job.Function);
                                failedJobsLog.AppendLine("Body: " + job.Body);
                                failedJobsLog.AppendLine("Message: " + job.Message);
                                failedJobsLog.AppendLine("Error: " + job.Error);
                                failedJobsLog.AppendLine("====================");
            #endif
                            }
                        }
                    }

                    if (failedJobs > 0)
                    {
                        InsertQuery iQuery = new InsertQuery(typeof(ApplicationError));
                        iQuery.AddField("error_title", "Jobs failed at " + Hyperlink.Domain);
                        iQuery.AddField("error_body", "FAILED JOB COUNT:\n" + failedJobs.ToString() + "\n\n" + failedJobsLog.ToString());
                        core.Db.Query(iQuery);
            #if DEBUG
                        core.Email.SendEmail(WebConfigurationManager.AppSettings["error-email"], "Jobs failed at " + Hyperlink.Domain, "FAILED JOB COUNT:\n" + failedJobs.ToString() + "\n\n" + failedJobsLog.ToString());
            #endif
                    }
                }
            }
            catch (Exception ex)
            {
                try
                {
                    InsertQuery iQuery = new InsertQuery(typeof(ApplicationError));
                    iQuery.AddField("error_title", "An Error occured at " + Hyperlink.Domain + " in global.asax");
                    iQuery.AddField("error_body", "EXCEPTION THROWN:\n" + ex.ToString());
                    core.Db.Query(iQuery);
                    core.Email.SendEmail(WebConfigurationManager.AppSettings["error-email"], "An Error occured at " + Hyperlink.Domain + " in global.asax", "EXCEPTION THROWN:\n" + ex.ToString());
                }
                catch
                {
                    try
                    {
                        core.Email.SendEmail(WebConfigurationManager.AppSettings["error-email"], "An Error occured at " + Hyperlink.Domain + " in global.asax", "EXCEPTION THROWN:\n" + ex.ToString());
                    }
                    catch
                    {
                    }
                }
            }

            // Cleanup

            lock (queueLock)
            {
                if (queue != null)
                {
                    Queue.CloseConnection();
                    Queue = null;
                }
            }

            core.CloseProse();
            core.CloseSearch();
            core.CloseCache();
            core = null;
            db.CloseConnection();
            db = null;
        }