~ubuntu-branches/ubuntu/trusty/smuxi/trusty-proposed

« back to all changes in this revision

Viewing changes to lib/ServiceStack/src/ServiceStack.Common/ExecExtensions.cs

  • Committer: Package Import Robot
  • Author(s): Mirco Bauer
  • Date: 2013-05-25 22:11:31 UTC
  • mfrom: (1.2.12)
  • Revision ID: package-import@ubuntu.com-20130525221131-nd2mc0kzubuwyx20
Tags: 0.8.11-1
* [22d13d5] Imported Upstream version 0.8.11
* [6d2b95a] Refreshed patches
* [89eb66e] Added ServiceStack libraries to smuxi-engine package
* [848ab10] Enable Campfire engine
* [c6dbdc7] Always build db4o for predictable build result
* [13ec489] Exclude OS X specific libraries from dh_clideps

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
using System;
 
2
using System.Collections.Generic;
 
3
using System.Linq;
 
4
using System.Text;
 
5
using System.Threading;
 
6
using ServiceStack.Logging;
 
7
 
 
8
namespace ServiceStack.Common
 
9
{
 
10
    public static class ExecExtensions
 
11
    {
 
12
        public static void LogError(Type declaringType, string clientMethodName, Exception ex)
 
13
        {
 
14
            var log = LogManager.GetLogger(declaringType);
 
15
            log.Error(string.Format("'{0}' threw an error on {1}: {2}", declaringType.FullName, clientMethodName, ex.Message), ex);
 
16
        }
 
17
 
 
18
        public static void ExecAll<T>(this IEnumerable<T> instances, Action<T> action)
 
19
        {
 
20
            foreach (var instance in instances)
 
21
            {
 
22
                try
 
23
                {
 
24
                    action(instance);
 
25
                }
 
26
                catch (Exception ex)
 
27
                {
 
28
                    LogError(instance.GetType(), action.GetType().Name, ex);
 
29
                }
 
30
            }
 
31
        }
 
32
 
 
33
        public static void ExecAllWithFirstOut<T, TReturn>(this IEnumerable<T> instances, Func<T, TReturn> action, ref TReturn firstResult)
 
34
        {
 
35
            foreach (var instance in instances)
 
36
            {
 
37
                try
 
38
                {
 
39
                    var result = action(instance);
 
40
                    if (!Equals(firstResult, default(TReturn)))
 
41
                    {
 
42
                        firstResult = result;
 
43
                    }
 
44
                }
 
45
                catch (Exception ex)
 
46
                {
 
47
                    LogError(instance.GetType(), action.GetType().Name, ex);
 
48
                }
 
49
            }
 
50
        }
 
51
 
 
52
        public static TReturn ExecReturnFirstWithResult<T, TReturn>(this IEnumerable<T> instances, Func<T, TReturn> action)
 
53
        {
 
54
            foreach (var instance in instances)
 
55
            {
 
56
                try
 
57
                {
 
58
                    var result = action(instance);
 
59
                    if (!Equals(result, default(TReturn)))
 
60
                    {
 
61
                        return result;
 
62
                    }
 
63
                }
 
64
                catch (Exception ex)
 
65
                {
 
66
                    LogError(instance.GetType(), action.GetType().Name, ex);
 
67
                }
 
68
            }
 
69
 
 
70
            return default(TReturn);
 
71
        }
 
72
 
 
73
        public static void RetryUntilTrue(Func<bool> action, TimeSpan? timeOut)
 
74
        {
 
75
            var i = 0;
 
76
            var firstAttempt = DateTime.Now;
 
77
 
 
78
            while (timeOut == null || DateTime.Now - firstAttempt < timeOut.Value)
 
79
            {
 
80
                i++;
 
81
                if (action())
 
82
                {
 
83
                    return;
 
84
                }
 
85
                SleepBackOffMultiplier(i);
 
86
            }
 
87
 
 
88
            throw new TimeoutException(string.Format("Exceeded timeout of {0}", timeOut.Value));
 
89
        }
 
90
 
 
91
        public static void RetryOnException(Action action, TimeSpan? timeOut)
 
92
        {
 
93
            var i = 0;
 
94
            Exception lastEx = null;
 
95
            var firstAttempt = DateTime.Now;
 
96
 
 
97
            while (timeOut == null || DateTime.Now - firstAttempt < timeOut.Value)
 
98
            {
 
99
                i++;
 
100
                try
 
101
                {
 
102
                    action();
 
103
                    return;
 
104
                }
 
105
                catch (Exception ex)
 
106
                {
 
107
                    lastEx = ex;
 
108
 
 
109
                    SleepBackOffMultiplier(i);
 
110
                }
 
111
            }
 
112
 
 
113
            throw new TimeoutException(string.Format("Exceeded timeout of {0}", timeOut.Value), lastEx);
 
114
        }
 
115
 
 
116
        public static void RetryOnException(Action action, int maxRetries)
 
117
        {
 
118
            for (var i = 0; i < maxRetries; i++)
 
119
            {
 
120
                try
 
121
                {
 
122
                    action();
 
123
                    break;
 
124
                }
 
125
                catch
 
126
                {
 
127
                    if (i == maxRetries - 1) throw;
 
128
 
 
129
                    SleepBackOffMultiplier(i);
 
130
                }
 
131
            }
 
132
        }
 
133
 
 
134
        private static void SleepBackOffMultiplier(int i)
 
135
        {
 
136
            //exponential/random retry back-off.
 
137
            var rand = new Random(Guid.NewGuid().GetHashCode());
 
138
            var nextTry = rand.Next(
 
139
                (int)Math.Pow(i, 2), (int)Math.Pow(i + 1, 2) + 1);
 
140
 
 
141
            Thread.Sleep(nextTry);
 
142
        }
 
143
    }
 
144
}