Exemple #1
0
        public void Dequeue(WebQItem item)
        {
            if (!_waitingInQueue.HasValue) return;
            if (!_waitingInQueue.Value) return;

            var request = WebRequest.Create(new Uri(_webQAddress, item.Environment + "/" + item.Id));
            request.Method = "DELETE";
            request.ContentLength = 0;

            using(request.GetResponse())
            _waitingInQueue = false;
        }
Exemple #2
0
        public void WaitInQueue(TimeSpan timeout)
        {
            const string positionMessage = "Waiting in deployment queue. There are {0} deployment(s) ahead of you.";
            var timeForTimeout = DateTime.Now + timeout;

            _item = _client.Enqueue(_environment);
            OnWebQueuePositionUpdate(this, new WebQueueEventArgs(string.Format(positionMessage, _item.Position), _item));

            if (_item.Position == 0)
            {
                _client.SetAsStarted(_item);
                return;
            }

            var currentPosition = _item.Position;
            do
            {
                Thread.Sleep(_refreshInterval);
                _item = _client.Peek(_item);

                if(currentPosition != _item.Position)
                {
                    currentPosition = _item.Position;
                    if (currentPosition == 0) break;
                    OnWebQueuePositionUpdate(this, new WebQueueEventArgs(string.Format(positionMessage, currentPosition), _item));
                }

                if (DateTime.Now > timeForTimeout)
                {
                    try
                    {
                        _client.Dequeue(_item);
                    }
                    catch
                    {
                        throw new TimeoutException("ConDep timed out waiting in queue and failed to remove itself from the queue.");
                    }
                    throw new TimeoutException("ConDep timed out waiting in queue.");
                }

                OnWebQueueTimeoutUpdate(this, new WebQueueEventArgs(string.Format("Will wait in queue for {0} minutes before timing out.", (timeForTimeout - DateTime.Now).ToString("c", CultureInfo.CurrentCulture)), _item));
            } while (true);

            _item = _client.SetAsStarted(_item);
        }
Exemple #3
0
        public WebQItem Peek(WebQItem item)
        {
            try
            {
                var request = WebRequest.Create(new Uri(_webQAddress, item.Environment + "/" + item.Id));
                request.Method = "GET";
                request.ContentLength = 0;

                using(var response = request.GetResponse())
                {
                    var serializer = new DataContractJsonSerializer(typeof(WebQItem));

                    using(var stream = response.GetResponseStream())
                    {
                        if (stream != null)
                        {
                            return serializer.ReadObject(stream) as WebQItem;
                        }
                        return item;
                    }
                }

            }
            catch (WebException webEx)
            {
                if (webEx.Status == WebExceptionStatus.ProtocolError && webEx.Response != null)
                {
                    var exResponse = (HttpWebResponse)webEx.Response;
                    if (exResponse.StatusCode == HttpStatusCode.NotFound)
                    {
                        throw new WebQItemNoLongerInQueueException();
                    }
                }
                throw;
            }
        }
Exemple #4
0
        public WebQItem SetAsStarted(WebQItem item)
        {
            var request = WebRequest.Create(new Uri(_webQAddress, item.Environment + "/" + item.Id));
            request.Method = "POST";
            request.ContentLength = 0;

            using(var response = request.GetResponse())
            {
                var serializer = new DataContractJsonSerializer(typeof(WebQItem));
                using(var stream = response.GetResponseStream())
                {
                    if (stream != null)
                    {
                        return serializer.ReadObject(stream) as WebQItem;
                    }
                    return item;
                }
            }
        }
Exemple #5
0
        public WebQItem Enqueue(string environment)
        {
            if (string.IsNullOrWhiteSpace(environment))
            {
                _eventLog.WriteEntry("Environment argument is null or empty.", EventLogEntryType.Error);
                return null;
            }

            List<WebQItem> env;

            if (_environments.ContainsKey(environment.ToLower()))
            {
                env = _environments[environment.ToLower()];
            }
            else
            {
                env = new List<WebQItem>();
                _environments.Add(environment.ToLower(), env);
            }

            var item = new WebQItem {Id = Guid.NewGuid().ToString(), Environment = environment, CreatedTime = DateTime.Now};
            env.Add(item);
            item.Position = env.IndexOf(item);
            Save();
            return item;
        }
Exemple #6
0
 public WebQueueEventArgs(string message, WebQItem item)
 {
     _message = message;
     _item = item;
 }
Exemple #7
0
 public static HttpStatusCode Created(HttpListenerContext context, WebQItem item)
 {
     var serializer = new DataContractJsonSerializer(item.GetType());
     serializer.WriteObject(context.Response.OutputStream, item);
     return HttpStatusCode.Created;
 }
Exemple #8
0
 public WebQItem SetAsStarted(WebQItem item)
 {
     item.StartTime = DateTime.Now;
     return item;
 }
Exemple #9
0
 public WebQItem Peek(WebQItem item)
 {
     item.Position--;
     return item;
 }
Exemple #10
0
 public void Dequeue(WebQItem item)
 {
 }