Example #1
0
 protected virtual void FreeResources(ICheckoutTaskContext context, CheckoutTask task)
 {
     task.ProxyPool.Release(task.Proxy);
     task.Proxy = null;
     context?.Dispose();
     TierControl.Release();
 }
Example #2
0
        private bool IsCanceled(CancellationToken cancelToken, CheckoutTask task, ICheckoutTaskContext context = null, bool isTierAcquired = true)
        {
            bool ret = false;

            if (cancelToken.IsCancellationRequested)
            {
                task.State = CheckoutTaskState.Canceled;
                context?.Dispose();
                if (isTierAcquired)
                {
                    TierControl.Release();
                }
                Logger.LogEvent(task.Log, $"TASK {task.Id}", "Task is canceled!");
                ret = true;
            }

            return(ret);
        }
Example #3
0
        private void Checkout(CancellationToken cancelToken, CheckoutTask task)
        {
            ICheckoutTaskContext context = null;

            CheckoutStep waitProxy   = new CheckoutStep(WaitProxy, "Waiting proxy", CheckoutTaskState.WaitingProxy);
            CheckoutStep waitProduct = new CheckoutStep(WaitProduct, "Waiting product", CheckoutTaskState.WaitingProduct);
            CheckoutStep addToCart   = new CheckoutStep(AddToCart, "Carting", CheckoutTaskState.Carting);
            CheckoutStep checkCart   = new CheckoutStep(CheckCart, "Checking cart", CheckoutTaskState.Checking);
            CheckoutStep billing     = new CheckoutStep(Billing, "Billing", CheckoutTaskState.Billing);
            CheckoutStep waitQuota   = new CheckoutStep(WaitQuota, "Waiting quota", CheckoutTaskState.WaitingQuota);
            CheckoutStep paying      = new CheckoutStep(Paying, "Paying", CheckoutTaskState.Paying);

            StepResult res = StepResult.Ok;
            TimeSpan   executionTime;

            if (!TierControl.Wait(cancelToken))
            {
                if (IsCanceled(cancelToken, task, null, false))
                {
                    return;
                }
                throw new Exception();
            }

            Logger.LogEvent(task.Log, $"TASK {task.Id}", "Initializing... done");

            //res = waitProxy.Run(null, task, TimeSpan.FromMilliseconds(task.Footsite.Settings.RetryPeriod), out executionTime, cancelToken);
            task.Proxy = task.ProxyPool.GetOne();

            if (IsCanceled(cancelToken, task))
            {
                return;
            }

            if (res != StepResult.Ok)
            {
                Logger.LogEvent(task.Log, $"TASK {task.Id}", "ATC can not be continued without proxy");

                return;
            }

            while (true)
            {
                if (res != StepResult.Ok)
                {
                    cancelToken.WaitHandle.WaitOne(TimeSpan.FromMilliseconds(task.Footsite.Settings.RetryPeriod));

                    if (IsCanceled(cancelToken, task, context))
                    {
                        return;
                    }
                }

                context?.Dispose();
                context = CreateContext(task);

                res = waitProduct.Run(context, task, TimeSpan.FromMilliseconds(task.Footsite.Settings.RetryPeriod), out executionTime, cancelToken);

                if (IsCanceled(cancelToken, task, context))
                {
                    return;
                }

                if (res != StepResult.Ok)
                {
                    Logger.LogEvent(task.Log, $"TASK {task.Id}", $"{waitProduct.Name} is failed... retry");

                    continue;
                }

                if (task.Size == ClothesSizeSystemCollection.RandomSize)
                {
                    task.Size = GetRandomSize();

                    Logger.LogEvent(task.Log, $"TASK {task.Id}", $"Size {task.Size} is selected");
                }

                res = addToCart.Run(context, task, TimeSpan.FromMilliseconds(task.Footsite.Settings.RetryPeriod), out executionTime, cancelToken);

                if (IsCanceled(cancelToken, task, context))
                {
                    return;
                }

                if (res == StepResult.OutOfStock)
                {
                    Logger.LogEvent(task.Log, $"TASK {task.Id}", $"{addToCart.Name} is failed... Out of Stock. Retry ATC");

                    continue;
                }

                if (res != StepResult.Ok)
                {
                    Logger.LogEvent(task.Log, $"TASK {task.Id}", $"{addToCart.Name} is failed... retry ATC");

                    continue;
                }

                while (true)
                {
                    if (res != StepResult.Ok)
                    {
                        cancelToken.WaitHandle.WaitOne(TimeSpan.FromMilliseconds(task.Footsite.Settings.RetryPeriod));

                        if (IsCanceled(cancelToken, task, context))
                        {
                            return;
                        }
                    }

                    res = checkCart.Run(context, task, TimeSpan.FromMilliseconds(task.Footsite.Settings.RetryPeriod), out executionTime, cancelToken);

                    if (IsCanceled(cancelToken, task, context))
                    {
                        return;
                    }

                    if (res != StepResult.Ok)
                    {
                        Logger.LogEvent(task.Log, $"TASK {task.Id}", $"{checkCart.Name} is failed... retry ATC");

                        break;
                    }

                    res = billing.Run(context, task, TimeSpan.FromMilliseconds(task.Footsite.Settings.RetryPeriod), out executionTime, cancelToken);

                    if (IsCanceled(cancelToken, task, context))
                    {
                        return;
                    }

                    if (res != StepResult.Ok)
                    {
                        Logger.LogEvent(task.Log, $"TASK {task.Id}", $"{billing.Name} is failed... retrying checkout");
                    }

                    res = waitQuota.Run(context, task, TimeSpan.FromMilliseconds(task.Footsite.Settings.RetryPeriod), out executionTime, cancelToken);

                    if (IsCanceled(cancelToken, task, context))
                    {
                        return;
                    }

                    if (res == StepResult.Failed)
                    {
                        Logger.LogEvent(task.Log, $"TASK {task.Id}", "Checkout quota has ended");
                    }

                    res = paying.Run(context, task, TimeSpan.FromMilliseconds(task.Footsite.Settings.RetryPeriod), out executionTime, cancelToken);

                    if (res == StepResult.Ok)
                    {
                        task.Profile.Release(true);

                        OnSuccessfulCheckout(new CheckoutInfo()
                        {
                            ReleaseName            = Release.Name,
                            ProductName            = ProductName,
                            ReleaseCheckoutProfile = task.Profile.ReleaseCheckoutProfile,
                            Size      = task.Size,
                            TimeStamp = DateTime.Now
                        });
                    }
                    else
                    {
                        task.Profile.Release(false);
                    }

                    if (IsCanceled(cancelToken, task, context))
                    {
                        return;
                    }

                    if (res != StepResult.Ok)
                    {
                        Logger.LogEvent(task.Log, $"TASK {task.Id}", $"{paying.Name} is failed... retrying checkout");

                        continue;
                    }

                    break;
                }

                if (res != StepResult.Ok)
                {
                    continue;
                }

                break;
            }

            Logger.LogEvent(task.Log, $"TASK {task.Id}", $"Finished!");

            task.State = CheckoutTaskState.Finished;

            FreeResources(context, task);
        }