Example #1
0
        private void RestockMonitor(ObservableCollection <string> log, RestockMonitorTask task, CancellationToken cancelToken)
        {
            Logger.LogEvent(log, "RESTOCK MONITOR", "Started!");
            Logger.LogEvent(log, "RESTOCK MONITOR", "Paused. Waiting for signal.");

            WaitHandle.WaitAny(new WaitHandle[] { task.StartEvent, task.CompleteEvent, cancelToken.WaitHandle });

            while (WaitHandle.WaitAny(new WaitHandle[] { cancelToken.WaitHandle, task.CompleteEvent }, task.Period) == WaitHandle.WaitTimeout)
            {
                CheckStock(task.CheckoutTasks, task.Proxy, cancelToken);
            }

            Logger.LogEvent(log, "RESTOCK MONITOR", "Exit");
        }
Example #2
0
        public async Task <bool> Start(CancellationToken cancelToken)
        {
            bool ret = true;

            lock (m_lock)
            {
                if (m_state == ReleaseTaskState.Working)
                {
                    ret = false;
                }
                m_state = ReleaseTaskState.Working;
            }

            if (ret)
            {
                OnPropertyChanged("State");

                await Task.Run(() =>
                {
                    Log.Clear();

                    ReleaseProductInfo productInfo = new ReleaseProductInfo()
                    {
                        ProductLink = Release.ProductLink,
                        KeyWords    = Release.KeyWords.ToList()
                    };

                    ProductMonitorTask productMonitorTask = new ProductMonitorTask()
                    {
                        Period      = Release.Footsite.Settings.ProductMonitorPeriod,
                        ProductInfo = productInfo,
                        Proxy       = Release.GlobalProxy
                    };

                    Thread monitorThread = new Thread(() => MonitorProduct(cancelToken, Log, productMonitorTask));
                    monitorThread.Start();

                    List <CheckoutTask> checkoutTasks = new List <CheckoutTask>();

                    int taskId          = 0;
                    ProxyPool proxyPool = new ProxyPool(Release.Proxies.ToList(), 1);

                    foreach (ReleaseCheckoutProfile profile in Release.Profiles)
                    {
                        CheckoutTaskCcProfile checkoutTaskCcProfile = new CheckoutTaskCcProfile(new ReleaseCheckoutProfile(profile)
                        {
                            CheckoutProfile = new CheckoutProfile(profile.CheckoutProfile)
                        }, Release.Footsite.Settings.PurchaseLimitPerProfile);
                        Profiles.Add(checkoutTaskCcProfile);

                        for (int i = 0; i < profile.TasksCount; i++)
                        {
                            CheckoutTask checkoutTask = new CheckoutTask()
                            {
                                Id                    = taskId++,
                                Size                  = profile.Size,
                                VariantId             = profile.VariantId,
                                Profile               = checkoutTaskCcProfile,
                                ProxyPool             = proxyPool,
                                ProductAvailableEvent = productMonitorTask.ProductAvailableEvent,
                                Footsite              = Release.Footsite,
                                ProductInfo           = productInfo
                            };
                            Logger.LogEvent(checkoutTask.Log, $"TASK {checkoutTask.Id}", "Initializing...");

                            checkoutTaskCcProfile.CheckoutTasks.Add(checkoutTask);
                            checkoutTasks.Add(checkoutTask);
                        }
                    }

                    RestockMonitorTask restockMonitorTask = new RestockMonitorTask()
                    {
                        Period        = TimeSpan.FromMilliseconds(Release.Footsite.Settings.ProductMonitorPeriod),
                        CheckoutTasks = checkoutTasks,
                        ProductInfo   = productInfo,
                    };

                    Thread restockMonitorThread = new Thread(() => RestockMonitor(Log, restockMonitorTask, cancelToken));
                    restockMonitorThread.Start();

                    CheckoutTasksWatcher checkoutTasksWatcher = new CheckoutTasksWatcher(checkoutTasks);

                    Task.Run(() =>
                    {
                        foreach (CheckoutTask checkoutTask in checkoutTasks)
                        {
                            if (cancelToken.IsCancellationRequested)
                            {
                                break;
                            }

                            Thread checkoutThread = new Thread(() => Checkout(cancelToken, checkoutTask));
                            checkoutThread.Start();
                        }
                    });

                    checkoutTasksWatcher.FinishedEvent.WaitOne(cancelToken);

                    productMonitorTask.CompleteEvent.Set();
                    restockMonitorTask.CompleteEvent.Set();
                });

                TierControl.ReleaseQuota(Release.TasksCount, Release.Footsite.Type);

                State = ReleaseTaskState.Idle;
            }

            return(ret);
        }