This repository has been archived by the owner on Apr 14, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 9
/
JobReservationService.cs
168 lines (131 loc) · 4.61 KB
/
JobReservationService.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.WindowsAzure;
using Microsoft.WindowsAzure.Storage;
using Microsoft.WindowsAzure.Storage.Blob;
using Newtonsoft.Json;
namespace Brisebois.WindowsAzure.Storage.Blobs
{
public class JobReservationService : IDisposable
{
private readonly string reserverName;
private readonly BlobLeaseManager manager;
private CloudBlobContainer container;
public JobReservationService(string connectionString,
string containerName,
string reserverName)
{
this.reserverName = reserverName;
Init(connectionString, containerName);
manager = new BlobLeaseManager();
}
private void Init(string connectionString, string containerName)
{
var cs = CloudConfigurationManager.GetSetting(connectionString);
var account = CloudStorageAccount.Parse(cs);
var client = account.CreateCloudBlobClient();
container = client.GetContainerReference(containerName);
container.CreateIfNotExists();
}
public bool TryReserveJob(string jobName, double jobReservationInSeconds)
{
var blobReference = GetJobReservationBlob(jobName);
if(!blobReference.Exists())
InitializeLeaseBlob(blobReference);
var acquireLease = manager.TryAcquireLease(blobReference, jobReservationInSeconds);
if(acquireLease)
UpdateReservationLog(jobName);
return acquireLease;
}
public bool HasReservation(string jobName)
{
return manager.HasLease(GetJobReservationBlob(jobName));
}
public void CancelReservation(string jobName)
{
manager.ReleaseLease(GetJobReservationBlob(jobName));
}
private CloudBlockBlob GetJobReservationBlob(string jobName)
{
var blobReference = container.GetBlockBlobReference(jobName);
return blobReference;
}
private void InitializeLeaseBlob(CloudBlockBlob blobReference)
{
var log = new JobReservationLog();
UpdateBlobContent(log, blobReference);
}
private void UpdateBlobContent(JobReservationLog jobReservationLog,
CloudBlockBlob jobReservationBlob)
{
jobReservationLog.Add(MakeJobReservation());
string leaseId = manager.GetLeaseId(jobReservationBlob);
AccessCondition accessCondition = string.IsNullOrWhiteSpace(leaseId)
? null
: new AccessCondition
{
LeaseId = leaseId
};
jobReservationBlob.UploadText(jobReservationLog.ToJson(),
null,
accessCondition);
}
private void UpdateReservationLog(string jobName)
{
CloudBlockBlob blobReference = GetJobReservationBlob(jobName);
JobReservationLog jobReservationLog = JobReservationLog.Make(blobReference.DownloadText());
JobReservation lastReservation = jobReservationLog.LastReservation;
if (lastReservation.Reserver == reserverName)
return;
UpdateBlobContent(jobReservationLog, blobReference);
}
private JobReservation MakeJobReservation()
{
return new JobReservation
{
Obtained = DateTime.UtcNow,
Reserver = reserverName,
};
}
public void Dispose()
{
manager.Dispose();
}
private struct JobReservation
{
public string Reserver { get; set; }
public DateTime Obtained { get; set; }
}
private class JobReservationLog
{
private readonly List<JobReservation> reservations = new List<JobReservation>();
public JobReservationLog()
{
}
private JobReservationLog(List<JobReservation> lockReservations)
{
reservations = lockReservations;
}
internal JobReservation LastReservation
{
get { return reservations.FirstOrDefault(); }
}
internal static JobReservationLog Make(string json)
{
var list = JsonConvert.DeserializeObject<List<JobReservation>>(json);
return new JobReservationLog(list);
}
internal void Add(JobReservation jobReservation)
{
reservations.Insert(0, jobReservation);
if (reservations.Count > 10)
reservations.Remove(reservations.Last());
}
internal string ToJson()
{
return JsonConvert.SerializeObject(reservations);
}
}
}
}