Skip to content

Instantly share code, notes, and snippets.

@pisceanfoot
Created May 14, 2014 01:19
Show Gist options
  • Save pisceanfoot/21b55b7de3a3e534fd53 to your computer and use it in GitHub Desktop.
Save pisceanfoot/21b55b7de3a3e534fd53 to your computer and use it in GitHub Desktop.
RetryableExcuteQueue
public class RetryableExcuteQueue<T>
where T : CommonContract
{
private Queue<ExuteItem> queue;
public RetryableExcuteQueue()
{
this.queue = new Queue<ExuteItem>();
}
public List<ExuteItem> ErrorItems { get; set; }
public void Add(T o)
{
this.queue.Enqueue(new ExuteItem(o));
}
public void Add(List<T> o)
{
o.ForEach(x => this.queue.Enqueue(new ExuteItem(x)));
}
public void Excute(Action<T> action)
{
while (this.queue.Count > 0)
{
ExuteItem item = this.queue.Dequeue();
try
{
action(item.Value);
}
catch (HttpException ex)
{
Log.Error(string.Format("Error Http Request: Product:{0},PlatformID:{1},Retry:{2}",
item.Value.ProductSysNo, item.Value.PlatformProductSysNo, item.Retry), ex);
if (item.Retry < 3)
{
item.Retry++;
this.queue.Enqueue(item);
}
else
{
if (this.ErrorItems == null)
{
this.ErrorItems = new List<ExuteItem>();
}
this.ErrorItems.Add(item);
}
}
catch (Exception ex)
{
item.Error = ex;
if (this.ErrorItems == null)
{
this.ErrorItems = new List<ExuteItem>();
}
this.ErrorItems.Add(item);
}
}
}
public class ExuteItem
{
public ExuteItem(T value)
{
this.Value = value;
this.Retry = 0;
}
public int Retry { get; set; }
public T Value { get; set; }
public Exception Error;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment