public Job ScheduleJob(Job job) { var req = (HttpWebRequest)WebRequest.Create( string.Format(_momentAppUrl, _apiKey)); req.Method = "POST"; req.ContentType = "application/json"; byte[] data = Encoding.UTF8.GetBytes ( "{'job':" + JsonConvert.SerializeObject(job, new IsoDateTimeConverter()) + "}" ); req.ContentLength = data.Length; using (var stream = req.GetRequestStream()) { stream.Write(data, 0, data.Length); } using (var response = req.GetResponse()) { using (var reader = new StreamReader(response.GetResponseStream())) { var responseData = reader.ReadToEnd(); dynamic jobData = JsonConvert.DeserializeObject(responseData); if (jobData.success != null) { job.id = jobData.success.job.id; return job; } throw new Exception("Failed: " + jobData.error); } } }
private static void ScheduleKeepAlive(string Url) { lock (_momentLock) { if (_momentJob == null) { _momentJob = new Moment(_momentApiKey).ScheduleJob(new Job() { at = DateTime.Now.AddMinutes(MINUTES_BETWEEN_KEEPALIVES), method = "GET", uri = new Uri(Url) }); } } }
public KeepAliveModule() : base("/keepalive") { Get["/"] = _ => { if (_momentJob != null) { if ((DateTime.Now - _momentJob.at).TotalMilliseconds < 0) { throw new InvalidOperationException(String.Format("A keepalive is already scheduled for {0}", _momentJob.at)); } } _momentJob = null; ScheduleKeepAlive(_hostBaseUrl + "/keepalive"); return "OK"; }; }