Beispiel #1
0
        public WebBackgroundJobImpl Execute(string methodName, ArrayImpl parameters, string key = "", string description = "")
        {
            WebBackgroundJob     job     = new WebBackgroundJob();
            WebBackgroundJobImpl jobImpl = new WebBackgroundJobImpl(job);

            if (key != null)
            {
                job.Key = key;
            }

            if (description != null)
            {
                job.Description = description;
            }

            job.ExecutionParameters = new IValue[0];

            if (parameters != null)
            {
                job.ExecutionParameters = new IValue[parameters.Count()];
                int index = 0;

                foreach (IValue cv in parameters)
                {
                    job.ExecutionParameters[index] = cv;
                    index++;
                }
            }

            job.MethodName = methodName;

            ThreadPool.QueueUserWorkItem(new WaitCallback(WebBackgroundJobsManager.ExecuteJob), job);

            return(jobImpl);
        }
Beispiel #2
0
        public static void ExecuteJob(object stateInfo)
        {
            WebBackgroundJob job = (WebBackgroundJob)stateInfo;

            System.Collections.Hashtable syncJobsKeys;
            syncJobsKeys = System.Collections.Hashtable.Synchronized(jobsKeys);

            if (job.Key != "" && job.Key != null)
            {
                // Пробуем вставить в таблицу ключей
                //, если вставка неудачна, значит фоновое задание уже выполняется

                try
                {
                    syncJobsKeys.Add(job.Key, job.Key);
                }
                catch (ArgumentException)
                {
                    // Такое значение уже есть в списке, не запускаем задание
                    return;
                }
            }

            // Заполняем значения работы и вставляем ее в список
            System.Collections.Hashtable syncJobs = System.Collections.Hashtable.Synchronized(jobs);
            syncJobs.Add(job.UUID, job);
            job.Begin = DateTime.Now;

            try
            {
                AspNetHostEngine engine = new AspNetHostEngine();
                engine.CallCommonModuleProcedure(job.MethodName, job.ExecutionParameters);
                job.State = BackgroundJobState.Completed;
                job.ExecutionParameters = null;
            }
            catch (ScriptEngine.ScriptException ex)
            {
                job.ErrorInfo = new ExceptionInfoContext(ex);
                job.State     = BackgroundJobState.Failed;

                System.IO.TextWriter logWriter = AspNetLog.Open();
                AspNetLog.Write(logWriter, "Error executing background job ");
                AspNetLog.Write(logWriter, ex.ToString());
                AspNetLog.Close(logWriter);
            }
            catch (Exception ex)
            {
                job.State = BackgroundJobState.Failed;

                System.IO.TextWriter logWriter = AspNetLog.Open();
                AspNetLog.Write(logWriter, "Error executing background job ");
                AspNetLog.Write(logWriter, ex.ToString());
                AspNetLog.Close(logWriter);
            }
            finally
            {
                job.End = DateTime.Now;

                try
                {
                    syncJobs.Remove(job.UUID);
                }
                catch { /* Ничего не делаем*/ }

                try
                {
                    if (job.Key != null)
                    {
                        syncJobsKeys.Remove(job.Key);
                    }
                }
                catch { /* Ничего не делаем*/ }
            }
        }
 public WebBackgroundJobImpl(WebBackgroundJob job)
 {
     _job = job;
 }
        public static void ExecuteJob(object stateInfo)
        {
            WebBackgroundJob job = (WebBackgroundJob)stateInfo;

            if (job.Key != "" && job.Key != null)
            {
                // Пробуем вставить в таблицу ключей
                //, если вставка неудачна, значит фоновое задание уже выполняется
                if (!jobsKeys.TryAdd(job.Key, job.Key))
                {
                    // Такое значение уже есть в списке, не запускаем задание?
                    throw new RuntimeException("Фоновое задание с таким значением ключа уже выполняется");
                }
            }

            // Заполняем значения работы и вставляем ее в список
            jobs.TryAdd(job.UUID, job);
            job.Begin = DateTime.Now;
            AspNetHostEngine engine = null;

            try
            {
                if (!engines.TryDequeue(out engine))
                {
                    throw new RuntimeException("cannot deque engine");
                }

                engine.CallCommonModuleProcedure(job.MethodName, job.ExecutionParameters);
                job.State = BackgroundJobState.Completed;
                job.ExecutionParameters = null;
            }
            catch (ScriptEngine.ScriptException ex)
            {
                job.ErrorInfo = new ExceptionInfoContext(ex);
                job.State     = BackgroundJobState.Failed;

                System.IO.TextWriter logWriter = AspNetLog.Open();
                AspNetLog.Write(logWriter, "Error executing background job ");
                AspNetLog.Write(logWriter, ex.ToString());
                AspNetLog.Close(logWriter);
            }
            catch (Exception ex)
            {
                job.State = BackgroundJobState.Failed;

                System.IO.TextWriter logWriter = AspNetLog.Open();
                AspNetLog.Write(logWriter, "Error executing background job ");
                AspNetLog.Write(logWriter, ex.ToString());
                AspNetLog.Close(logWriter);
            }
            finally
            {
                job.End = DateTime.Now;
                if (engine != null)
                {
                    engines.Enqueue(engine);
                }

                try
                {
                    WebBackgroundJob outjob;
                    jobs.TryRemove(job.UUID, out outjob);
                }
                catch { /* Ничего не делаем*/ }

                try
                {
                    string outStr;
                    if (job.Key != null && job.Key != "")
                    {
                        jobsKeys.TryRemove(job.Key, out outStr);
                    }
                }
                catch { /* Ничего не делаем*/ }
            }
        }