private Result <IssueData[]> ProcessIssueBatch(ElasticThreadPool pool, string projectId, string filterId, int pageNumber, int issuesInBatch, Tuplet <bool> canceled, Result <IssueData[]> result) { pool.QueueWorkItem(HandlerUtil.WithEnv(delegate { // TODO (steveb): use result.IsCanceled instead of shared tuple once cancellation is supported on the result object // check if request has been canceled if (!canceled.Item1) { IssueData[] issuesForBatch; if (!string.IsNullOrEmpty(filterId)) { issuesForBatch = _service.mc_filter_get_issues(_username, _password, projectId, filterId, pageNumber.ToString(), issuesInBatch.ToString()); } else { issuesForBatch = _service.mc_project_get_issues(_username, _password, projectId, pageNumber.ToString(), issuesInBatch.ToString()); } result.Return(issuesForBatch); } else { // TODO (steveb): throw a more specific exception result.Throw(new Exception("unspecified error")); } }, TaskEnv.Clone())); return(result); }
public void Copied_TaskEnv_has_current_state() { var state = new State(); TaskEnv.Current.SetState("foo", state); Assert.AreEqual(state, TaskEnv.Clone()["foo"]); }
public void TaskEnv_invoke_with_custom_dispatchqueue_sets_task_state() { var dispatchQueue = new TestDispatchQueue(); _log.Debug("setting up envs"); var currentState = new State(); State newState = null; TaskEnv.Current.SetState(currentState); var copiedEnv = TaskEnv.Clone(dispatchQueue); var newEnv = TaskEnv.New(dispatchQueue); var resetEvent = new AutoResetEvent(false); bool?hasState = null; // Note: have to over acquire otherwise env is wiped after invokenow copiedEnv.Acquire(); copiedEnv.Acquire(); copiedEnv.Invoke(() => { _log.Debug("copied env invoke"); hasState = currentState == TaskEnv.Current.GetState <State>(); resetEvent.Set(); }); resetEvent.WaitOne(); dispatchQueue.LastItem.Wait(); Assert.IsTrue(hasState.HasValue); Assert.IsTrue(hasState.Value); hasState = null; // Note: have to over acquire otherwise env is wiped after invokenow newEnv.Acquire(); newEnv.Acquire(); newEnv.Acquire(); newEnv.Invoke(() => { _log.Debug("new env invoke"); hasState = currentState == TaskEnv.Current.GetState <State>(); newState = new State(); TaskEnv.Current.SetState(newState); resetEvent.Set(); }); resetEvent.WaitOne(); dispatchQueue.LastItem.Wait(); Assert.IsTrue(hasState.HasValue); Assert.IsFalse(hasState.Value); Assert.IsNotNull(newState); Assert.AreEqual(currentState, TaskEnv.Current.GetState <State>()); Assert.AreNotEqual(currentState, newState); hasState = null; newEnv.Invoke(() => { _log.Debug("new env invoke 2"); hasState = newState == TaskEnv.Current.GetState <State>(); newEnv = TaskEnv.Current; resetEvent.Set(); }); resetEvent.WaitOne(); dispatchQueue.LastItem.Wait(); Assert.IsTrue(hasState.HasValue); Assert.IsTrue(hasState.Value); }
public void ITaskCloneable_is_cloned_on_task_copy() { var state = new TaskLifeSpanState("baz"); TaskEnv.Current.SetState("foo", state); bool?hasState = null; bool stateExists = false; var env = TaskEnv.Clone(); env.Acquire(); env.InvokeNow(() => { stateExists = TaskEnv.Current.ContainsKey("foo"); hasState = state == TaskEnv.Current.GetState <TaskLifeSpanState>("foo"); }); Assert.IsTrue(hasState.HasValue); Assert.IsFalse(hasState.Value); }
private void OnExpire(TaskTimer timer) { string name = (string)timer.State; lock (_directory) { // check if the record still exists DirectoryRecord record; if (_directory.TryGetValue(name, out record)) { // verify if the record should still be deleted if (record.Expiration <= timer.When) { _directory.Remove(record.Name); } else { timer.Change(record.Expiration, TaskEnv.Clone()); } } } }
/// <summary> /// Enqueue a callback as a work item to be invoked with a clone of the current <see cref="TaskEnv"/>. /// </summary> /// <param name="dispatchQueue">DispatchQueue to enqueue work into.</param> /// <param name="callback">Work item callback.</param> /// <param name="result">Synchronization handle for work item.</param> /// <returns>The synchronization handle provided to the method.</returns> public static Result QueueWorkItemWithClonedEnv(this IDispatchQueue dispatchQueue, Action callback, Result result) { return(dispatchQueue.QueueWorkItemWithEnv(callback, TaskEnv.Clone(), result)); }
/// <summary> /// Enqueue a callback as a work item to be invoked with a clone of the current <see cref="TaskEnv"/>. /// </summary> /// <param name="dispatchQueue">DispatchQueue to enqueue work into.</param> /// <param name="callback">Work item callback.</param> public static void QueueWorkItemWithClonedEnv(this IDispatchQueue dispatchQueue, Action callback) { dispatchQueue.QueueWorkItemWithEnv(callback, TaskEnv.Clone(), null); }
/// <summary> /// Enqueue a callback as a work item to be invoked with a clone of the current <see cref="TaskEnv"/>. /// </summary> /// <typeparam name="T">Result value type of callback.</typeparam> /// <param name="dispatchQueue">DispatchQueue to enqueue work into.</param> /// <param name="callback">Work item callback.</param> /// <param name="result">Synchronization handle for work item.</param> /// <returns>The synchronization handle provided to the method.</returns> public static Result <T> QueueWorkItemWithClonedEnv <T>(this IDispatchQueue dispatchQueue, Func <T> callback, Result <T> result) { return(dispatchQueue.QueueWorkItemWithEnv(callback, TaskEnv.Clone(), result)); }
//--- Methods --- protected override Yield Start(XDoc config, Result result) { yield return(Coroutine.Invoke(base.Start, config, new Result())); // set up defaults _insertTextLimit = config["max-size"].AsLong ?? DEFAULT_TEXT_LIMIT; _memoryCacheTime = config["memory-cache-time"].AsDouble ?? DEFAULT_MEMORY_CACHE_TIME; _log.DebugFormat("max-size: {0}, memory-cache-time: {1}", _insertTextLimit, _memoryCacheTime); // load current cache state Async.Fork(() => Coroutine.Invoke(RefreshCache, new Result(TimeSpan.MaxValue)), TaskEnv.Clone(), null); result.Return(); }
//--- Methods --- public void ResetMemoryExpiration() { _memoryExpire.Change(TimeSpan.FromSeconds(_memoryCacheTime), TaskEnv.Clone()); }
public ServiceRepository.IServiceInfo CreateLocalService(ServiceBE service, string servicePath, XDoc config) { var deki = DekiContext.Current.Deki; Plug location = deki.InternalCreateService(servicePath, service.SID, config, new Result <Plug>()).Wait(); XUri sid = XUri.TryParse(service.SID); string license; DateTime?expiration; if (deki.TryGetServiceLicense(sid, out license, out expiration) && (expiration != null)) { lock (_serviceExpirations) { TaskTimer timer; if (_serviceExpirations.TryGetValue(service.Id, out timer)) { timer.Cancel(); } _serviceExpirations[service.Id] = TimerFactory.New(expiration.Value, _ => ServiceBL.StopService(service), null, TaskEnv.Clone()); } } return(RunningServices.RegisterService(service, location.Uri, true)); }
internal Yield PostEntries(DreamContext context, DreamMessage request, Result <DreamMessage> response) { if (request.ToDocument().Name != "entry") { throw new DreamBadRequestException("invalid format"); } // prepare entry XAtomEntry entry = new XAtomEntry(request.ToDocument()); int number = System.Threading.Interlocked.Increment(ref _counter); XUri link = Self.At(number.ToString()); entry.Id = link; entry.AddLink(link, XAtomBase.LinkRelation.Edit, null, 0, null); // update feed XAtomFeed feed = _feed; if (feed != null) { lock (feed) { feed.Add(entry); } } else { throw new DreamBadRequestException("not initialized"); } // schedule entry deletion double seconds = context.GetParam <double>("ttl", _defaultTTL); if (seconds > 0) { TimerFactory.New(TimeSpan.FromSeconds(seconds), AutoDeletePost, number, TaskEnv.Clone()); } // return updated entry response.Return(DreamMessage.Created(link, entry)); yield break; }