Beispiel #1
0
 public WaiterTest()
     : base(LogicOf <Nothing> .New((x) =>
 {
     var flagDate       = DateTime.Now.AddSeconds(5);
     ICondition cond    = StrategizedCondition.New(() => { return(DateTime.Now < flagDate); });
     ICondition expCond = StrategizedCondition.New(() => { return(DateTime.Now > flagDate.AddSeconds(1)); });
     var waiter         = ConditionalWaiter.New(cond, expCond);
     waiter.WaitAround();
 }))
 {
 }
Beispiel #2
0
        public LogicTest()
            : base(LogicOf <ILogic> .New((x) =>
        {
            var flagDate       = DateTime.Now.AddSeconds(5);
            ICondition cond    = StrategizedCondition.New(() => { return(DateTime.Now < flagDate); });
            ICondition expCond = StrategizedCondition.New(() => { return(DateTime.Now > flagDate.AddSeconds(1)); });

            var newX = x.WaitUntil(cond, expCond);
            newX.Perform();
        }))
        {
        }
Beispiel #3
0
        public ValueOfTest()
            : base(LogicOf <IValueOf <string> > .New((x) =>
        {
            var flagDate       = DateTime.Now.AddSeconds(5);
            ICondition cond    = StrategizedCondition.New(() => { return(DateTime.Now < flagDate); });
            ICondition expCond = StrategizedCondition.New(() => { return(DateTime.Now > flagDate.AddSeconds(1)); });

            var newX   = x.WaitUntil(cond, expCond);
            var oldVal = x.GetValue().GraphSerializeWithDefaults();
            var newVal = newX.GetValue().GraphSerializeWithDefaults();
            Condition.Requires(oldVal).IsEqualTo(newVal);
        }))
        {
        }
Beispiel #4
0
        public ConditionTest()
            : base(LogicOf <ICondition> .New((x) =>
        {
            var flagDate       = DateTime.Now.AddSeconds(5);
            ICondition cond    = StrategizedCondition.New(() => { return(DateTime.Now < flagDate); });
            ICondition expCond = StrategizedCondition.New(() => { return(DateTime.Now > flagDate.AddSeconds(1)); });

            var newX   = x.WaitUntil(cond, expCond);
            var oldVal = x.Evaluate();
            var newVal = newX.Evaluate();
            Condition.Requires(oldVal).IsEqualTo(newVal);
        }))
        {
        }
Beispiel #5
0
        public LogicTest()
            : base(LogicOf <ILogic> .New((x) =>
        {
            var expiry = DateTime.Now.AddSeconds(5);
            var newX   = x.HasExpirable();
            newX.ExpiresWhen(StrategizedCondition.New(() => { return(DateTime.Now > expiry); }));
            newX.Perform();
            Thread.Sleep(6000);

            var trapX = newX.Traps();
            trapX.Perform();
        }))
        {
        }
Beispiel #6
0
        public ConditionTest()
            : base(LogicOf <ICondition> .New((x) =>
        {
            var expiry = DateTime.Now.AddSeconds(5);
            var newX   = x.HasExpirable();
            newX.ExpiresWhen(StrategizedCondition.New(() => { return(DateTime.Now > expiry); }));
            newX.Evaluate();
            Thread.Sleep(6000);

            var trapX   = newX.Traps();
            var trapVal = trapX.Evaluate();
            Condition.Requires(trapVal).IsEqualTo(false);
        }))
        {
        }
Beispiel #7
0
        public ValueOfTest()
            : base(LogicOf <IValueOf <string> > .New((x) =>
        {
            var expiry = DateTime.Now.AddSeconds(5);
            var newX   = x.HasExpirable();                                                        //expire this
            newX.ExpiresWhen(StrategizedCondition.New(() => { return(DateTime.Now > expiry); })); //specify how it expires
            var oldVal = newX.GetValue();                                                         //eval. should work fine
            Thread.Sleep(6000);

            //we've expired so eval should kack
            var trapX   = newX.Traps();   //trap that shit
            var trapVal = trapX.GetValue();
            Condition.Requires(trapVal == null).IsTrue();
        }))
        {
        }
        public DependencyDecoration(ITask decorated, List <string> prerequisiteTaskIds)
            : base(decorated.Triggered())
        {
            //^^^^ notice how we have applied the trigger decoration in the base CTOR.  this gives us conditions to add our dependency to

            //we can only have one dependency decoration per cake
            if (decorated.HasDecoration <DependencyDecoration>())
            {
                throw new InvalidOperationException("already decorated");
            }

            this.Dependency = new DependencyOf <string>(this.Id);
            prerequisiteTaskIds.WithEach(x => { this.Dependency.Prerequisites.Add(x); });

            //wire the dependency condition
            this.PerformTrigger.AppendAnd(StrategizedCondition.New((() => { return(this.AreAllPrerequisiteTasksComplete()); })));
            this.CancelTrigger.AppendAnd(StrategizedCondition.New((() => { return(this.AreAnyPrerequisiteTasksCancelledOrErrored()); })));
        }
Beispiel #9
0
        /// <summary>
        /// provide store (with defined eviction strategies)
        /// </summary>
        /// <param name="store"></param>
        public Job(string id, LogicOfTo <IHasId, IExpirable> evictionPolicy)
            : base(StrategizedTask.NewBlank(id))
        {
            /*Overall process:
             * -create a managing task (this guy) to manage and store all of the subtasks.
             *  -the core of this managing task is a blank strategized core which is decorated
             *  with a bunch of behaviours that help accomplish the managing task's purpose.
             *
             * The layers are described below:
             * -Core
             *      -PerformLogic - on perform, injects polling strategy checkTriggers() that moves the tasks along
             *      -CancelLogic - cancels all cancellable tasks
             * -Async
             *       -Complete trigger: When all tasks complete
             *       -Error trigger:When any tasks go wrong
             * -Events
             *       -OnCancelled, release waithandle
             *       -OnComplete, release waithandle
             *       -OnError, release waithandle
             * -Polling
             *       -run checkTriggers as injected above
             *
             * The job task also contains the TaskStore which all contains all of the tasks to manage.
             *
             * RunToCompletion() kicks the job off and waits until it is complete.
             */

            //create new in memory store for the tasks to live in
            var taskStore = NaturalInMemoryStore.New().MakeTaskStore(evictionPolicy);

            this.TaskStore = taskStore;

            //get the core task (is also the Decorated property as we're a 2-layer cake at this point)
            StrategizedTask coreTask = this.Core as StrategizedTask;

            //define the Perform and Cancel Logic
            coreTask.Performs(Logic.New(
                                  () =>
            {
                //the perform task is to turn on the polling process
                this.As <IPollingDecoration>(false)
                .SetBackgroundAction(LogicOf <ITask> .New(
                                         (x) =>
                {
                    this.checkTriggers();
                })
                                     );
            }));
            coreTask.Cancels(Logic.New(() => { this.CancelTasks(); }));

            //define the action to flip the waithandle that is waited on in RunToCompletion
            Action flipWaitHandle = () =>
            {
                lock (_stateLock)
                {
                    if (this.Status == DecoratidTaskStatusEnum.Cancelled ||
                        this.Status == DecoratidTaskStatusEnum.Complete ||
                        this.Status == DecoratidTaskStatusEnum.Errored)
                    {
                        Monitor.Pulse(_stateLock);
                    }
                }
            };

            //now decorate this core task with the layers described above

            //decorate as asynch (under the hood this will decorate with Trigger Conditions). Also implies that we need a polling process to check the conditions
            ITask decoratedTask = coreTask.IsAsynchronous(
                StrategizedCondition.New(() => { return(this.AreTasksComplete()); }),
                StrategizedCondition.New(() => { return(this.HaveTasksErrored()); }));

            //decorate with event handlers to flip the wait handle
            decoratedTask = decoratedTask.Eventing().DoOnTaskCancelled(flipWaitHandle).DoOnTaskCompleted(flipWaitHandle).DoOnTaskErrored(flipWaitHandle);
            //decorate with polling without a polling strategy
            decoratedTask = decoratedTask.DecorateWithPolling();

            //set the task store
            decoratedTask.TaskStore = this.TaskStore;
            decoratedTask.Save();

            //inject decoration that we've just built
            this.ReplaceDecorated((decorated) =>
            {
                return(decoratedTask);
            });

            //subscribe to eviction event where we initiate a cancel if it happens
            this.TaskStore.ItemEvicted += Job_ItemEvicted;
        }