1
1
using System ;
2
- using System . Collections . Generic ;
3
2
using System . Linq ;
4
3
using System . Threading ;
5
4
using Hangfire . FluentNHibernateStorage . Entities ;
@@ -11,7 +10,6 @@ namespace Hangfire.FluentNHibernateStorage
11
10
#pragma warning disable 618
12
11
public class ExpirationManager : IBackgroundProcess , IServerComponent
13
12
{
14
- #pragma warning restore 618
15
13
private const string DistributedLockKey = "expirationmanager" ;
16
14
private const int NumberOfRecordsInSinglePass = 1000 ;
17
15
private static readonly ILog Logger = LogProvider . GetCurrentClassLogger ( ) ;
@@ -38,72 +36,11 @@ public void Execute(BackgroundProcessContext context)
38
36
{
39
37
var cancellationToken = context . CancellationToken ;
40
38
Execute ( cancellationToken ) ;
41
-
42
- }
43
-
44
- private long DeleteExpirableWithId < T > ( SessionWrapper session , DateTime baseDate ) where T : IExpirableWithId
45
-
46
- {
47
- List < long > ids ;
48
- ids = session . Query < T > ( )
49
- . Where ( i => i . ExpireAt < baseDate )
50
- . Take ( NumberOfRecordsInSinglePass )
51
- . Select ( i => i . Id )
52
- . ToList ( ) ;
53
- return session . DeleteByInt64Id < T > ( ids ) ;
54
- }
55
-
56
-
57
- private void BatchDelete < T > ( CancellationToken cancellationToken ,
58
- Func < SessionWrapper , DateTime , long > deleteFunc )
59
-
60
- {
61
- var entityName = typeof ( T ) . Name ;
62
- Logger . InfoFormat ( "Removing outdated records from table '{0}'..." , entityName ) ;
63
-
64
- long removedCount = 0 ;
65
-
66
- do
67
- {
68
- try
69
- {
70
- using ( var distributedLock =
71
- new FluentNHibernateDistributedLock ( _storage , DistributedLockKey , DefaultLockTimeout ,
72
- cancellationToken ) . Acquire ( ) )
73
- {
74
- using ( var session = _storage . GetSession ( ) )
75
- {
76
- removedCount = deleteFunc ( session , _storage . UtcNow ) ;
77
- }
78
- }
79
-
80
- Logger . InfoFormat ( "removed records count={0}" , removedCount ) ;
81
- }
82
- catch ( Exception ex )
83
- {
84
- Logger . Error ( ex . ToString ( ) ) ;
85
- }
86
-
87
-
88
- if ( removedCount > 0 )
89
- {
90
- Logger . Info ( string . Format ( "Removed {0} outdated record(s) from '{1}' table." , removedCount ,
91
- entityName ) ) ;
92
-
93
- cancellationToken . WaitHandle . WaitOne ( DelayBetweenPasses ) ;
94
- cancellationToken . ThrowIfCancellationRequested ( ) ;
95
- }
96
- } while ( removedCount != 0 ) ;
97
- }
98
-
99
- public override string ToString ( )
100
- {
101
- return GetType ( ) . ToString ( ) ;
102
39
}
103
40
104
41
public void Execute ( CancellationToken cancellationToken )
105
42
{
106
- BatchDelete < _JobState > ( cancellationToken , ( session , baseDate2 ) =>
43
+ BatchDelete < _JobState > ( cancellationToken , session =>
107
44
{
108
45
var idList = session . Query < _JobState > ( )
109
46
. Where ( i => i . Job . ExpireAt < session . Storage . UtcNow )
@@ -112,7 +49,7 @@ public void Execute(CancellationToken cancellationToken)
112
49
. ToList ( ) ;
113
50
return session . DeleteByInt64Id < _JobState > ( idList ) ;
114
51
} ) ;
115
- BatchDelete < _JobQueue > ( cancellationToken , ( session , baseDate2 ) =>
52
+ BatchDelete < _JobQueue > ( cancellationToken , session =>
116
53
{
117
54
var idList = session . Query < _JobQueue > ( )
118
55
. Where ( i => i . Job . ExpireAt < session . Storage . UtcNow )
@@ -121,7 +58,7 @@ public void Execute(CancellationToken cancellationToken)
121
58
. ToList ( ) ;
122
59
return session . DeleteByInt64Id < _JobState > ( idList ) ;
123
60
} ) ;
124
- BatchDelete < _JobParameter > ( cancellationToken , ( session , baseDate2 ) =>
61
+ BatchDelete < _JobParameter > ( cancellationToken , session =>
125
62
{
126
63
var idList = session . Query < _JobParameter > ( )
127
64
. Where ( i => i . Job . ExpireAt < session . Storage . UtcNow )
@@ -130,7 +67,7 @@ public void Execute(CancellationToken cancellationToken)
130
67
. ToList ( ) ;
131
68
return session . DeleteByInt64Id < _JobParameter > ( idList ) ;
132
69
} ) ;
133
- BatchDelete < _DistributedLock > ( cancellationToken , ( session , baseDate2 ) =>
70
+ BatchDelete < _DistributedLock > ( cancellationToken , session =>
134
71
{
135
72
var idList = session . Query < _DistributedLock > ( )
136
73
. Where ( i => i . ExpireAtAsLong < session . Storage . UtcNow . ToUnixDate ( ) )
@@ -147,5 +84,68 @@ public void Execute(CancellationToken cancellationToken)
147
84
148
85
cancellationToken . WaitHandle . WaitOne ( _checkInterval ) ;
149
86
}
87
+
88
+ internal static long DeleteExpirableWithId < T > ( SessionWrapper session ) where T : IExpirableWithId
89
+
90
+ {
91
+ return session . DeleteExpirableWithId < T > ( NumberOfRecordsInSinglePass ) ;
92
+ }
93
+
94
+
95
+ private void BatchDelete < T > ( CancellationToken cancellationToken ,
96
+ Func < SessionWrapper , long > deleteFunc )
97
+
98
+ {
99
+ try
100
+ {
101
+ var entityName = typeof ( T ) . Name ;
102
+ Logger . InfoFormat ( "Removing outdated records from table '{0}'..." , entityName ) ;
103
+
104
+ long removedCount = 0 ;
105
+
106
+ while ( true )
107
+ {
108
+ try
109
+ {
110
+ using ( new FluentNHibernateDistributedLock ( _storage , DistributedLockKey , DefaultLockTimeout ,
111
+ cancellationToken ) . Acquire ( ) )
112
+ {
113
+ using ( var session = _storage . GetSession ( ) )
114
+ {
115
+ removedCount = deleteFunc ( session ) ;
116
+ }
117
+ }
118
+
119
+ Logger . InfoFormat ( "removed records count={0}" , removedCount ) ;
120
+ }
121
+ catch ( Exception ex )
122
+ {
123
+ Logger . Error ( ex . ToString ( ) ) ;
124
+ }
125
+
126
+
127
+ if ( removedCount <= 0 )
128
+ {
129
+ break ;
130
+ }
131
+
132
+ Logger . Info ( string . Format ( "Removed {0} outdated record(s) from '{1}' table." , removedCount ,
133
+ entityName ) ) ;
134
+
135
+ cancellationToken . WaitHandle . WaitOne ( DelayBetweenPasses ) ;
136
+ cancellationToken . ThrowIfCancellationRequested ( ) ;
137
+ }
138
+ }
139
+ catch ( Exception ) when ( cancellationToken . IsCancellationRequested )
140
+ {
141
+ //do nothing
142
+ }
143
+ }
144
+
145
+ public override string ToString ( )
146
+ {
147
+ return GetType ( ) . ToString ( ) ;
148
+ }
149
+ #pragma warning restore 618
150
150
}
151
151
}
0 commit comments