示例#1
0
文件: Linger.cs 项目: t9mike/vault
        internal static ScheduledJob CreateScheduledJob(dynamic job, int priority, DateTime?runAt)
        {
            var scheduled = new ScheduledJob
            {
                Priority = priority,
                Handler  = HandlerSerializer.Serialize(job),
                RunAt    = runAt
            };

            return(scheduled);
        }
示例#2
0
        private bool Perform(ScheduledJob job)
        {
            var            success = false;
            Perform        handler = null;
            IList <string> methods = null;

            try
            {
                // Acquire the handler
                handler = HandlerSerializer.Deserialize <object>(job.Handler).ActLike <Perform>();
                if (handler == null)
                {
                    job.LastError = "Missing handler";
                    return(false);
                }

                // Acquire and cache method manifest
                var handlerType = handler.GetType();
                if (!Cache.TryGetValue(handlerType, out methods))
                {
                    methods = handlerType.GetMethods().Select(m => m.Name).ToList();
                    Cache.Add(handlerType, methods);
                }

                _pending.TryAdd(handler, methods);

                // Before
                if (methods.Contains("Before"))
                {
                    handler.ActLike <Before>().Before();
                }

                // Perform
                success = handler.Perform();

                if (success)
                {
                    if (methods.Contains("Success"))
                    {
                        handler.ActLike <Success>().Success();
                    }
                }

                // Failure
                if (JobWillFail(job) && methods.Contains("Failure"))
                {
                    {
                        handler.ActLike <Failure>().Failure();
                    }
                }

                // After
                if (methods.Contains("After"))
                {
                    handler.ActLike <After>().After();
                }

                _pending.TryRemove(handler, out methods);
            }
            catch (OperationCanceledException)
            {
                job.LastError = "Cancelled";
            }
            catch (Exception ex)
            {
                job.LastError = ex.Message;
                if (methods != null && methods.Contains("Error"))
                {
                    handler.ActLike <Error>().Error(ex);
                }
            }
            return(success);
        }