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);
        }
Esempio n. 2
0
        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 (!AspNetHostEngine.Pool.TryDequeue(out engine))
                {
                    throw new RuntimeException("cannot deque engine");
                }

                engine.Engine.EngineInstance.Environment.LoadMemory(MachineInstance.Current);
                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)
                {
                    AspNetHostEngine.Pool.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 { /* Ничего не делаем*/ }
            }
        }
 public WebBackgroundJobImpl(WebBackgroundJob job)
 {
     _job = job;
 }