private void MonitorProduct(CancellationToken cancelToken, ObservableCollection <string> log, ProductMonitorTask task) { bool isFirstTime = true; Logger.LogEvent(log, "PRODUCT MONITOR", "Started!"); while (true) { List <string> availableSizes = null; Logger.LogEvent(log, "PRODUCT MONITOR", "Checking product..."); try { if (!CheckProduct(task.ProductInfo, task.Proxy, cancelToken, out availableSizes)) { Logger.LogEvent(log, "PRODUCT MONITOR", "Product is available!"); if (availableSizes != null && availableSizes.Count > 0) { Logger.LogEvent(log, "PRODUCT MONITOR", $"Sizes: {string.Join(", ", availableSizes)}"); AvailableSizes = availableSizes.GetRange(0, availableSizes.Count / 2); } else { Logger.LogEvent(log, "PRODUCT MONITOR", "Could not fetch available sizes. All range of sizes will be used for RANDOM option."); AvailableSizes = Release.Profiles[0].ClothesSizeSystem.Sizes.ToList(); } task.ProductAvailableEvent.Set(); IsProductAvailable = true; break; } } catch (Exception e) { Logger.LogEvent(log, "PRODUCT MONITOR", "Could not fetch available sizes. All range of sizes will be used for RANDOM option."); } if (WaitHandle.WaitAny(new WaitHandle[] { cancelToken.WaitHandle, task.CompleteEvent }, TimeSpan.FromMilliseconds(task.Period)) != WaitHandle.WaitTimeout) { break; } } Logger.LogEvent(log, "PRODUCT MONITOR", "Exit"); }
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); }