public async Task StartAsync(CancellationToken cancellationToken) { using var scope = services.CreateScope(); var sp = scope.ServiceProvider; try { var hostEnv = sp.GetService <IHostEnvironment>(); var hubApi = sp.GetService <HubAppApi>(); var gitFactory = sp.GetService <GitFactory>(); var publishProcess = new PublishProcess(hostEnv, hubApi, gitFactory); var options = sp.GetService <IOptions <PublishOptions> >().Value; var appKey = new AppKey(new AppName(options.AppName), AppType.Values.Value(options.AppType)); await publishProcess.Run(appKey, options.AppsToImport, options.RepoOwner, options.RepoName); if (!appKey.Type.Equals(AppType.Values.Package) && !options.NoInstall) { await publishProcess.RunInstall(appKey, options.DestinationMachine); } } catch (Exception ex) { Console.WriteLine(ex); Environment.ExitCode = 999; } var lifetime = scope.ServiceProvider.GetService <IHostApplicationLifetime>(); lifetime.StopApplication(); }
private void ProcessPublishArticlesComplete(IAsyncResult itfAR) { PublishProcess publishProcesArticles = (PublishProcess)((AsyncResult)itfAR).AsyncDelegate; ArticlePublish articlePublish = publishProcesArticles.EndInvoke(itfAR); long aid = articlePublish.LastExportedCoid; long cmsAid = articlePublish.LastExportedCmsid; int pubID = articlePublish.PubID; //输出错误信息 printErrors(articlePublish); //更新对应发布规则的已发布数量 updatePublishState(articlePublish); articlePublish.PubState = "发布结束"; Thread.Sleep(2000); //延时2秒,等待监控状态最后一次更新完毕 articlePublish.PubState = "发布完毕"; Thread.Sleep(2000); //延时2秒,等待监控状态最后一次更新完毕 //移除当前正在监控的发布任务 removeOneCollection(pubID); //继续执行下一个发布任务 ThreadPool.QueueUserWorkItem(startOneTask, null); }
private void startOneTask(object state) { int pubID = getOnePubID(); if (pubID != -1) { mySqlDB coMyDB = new mySqlDB(_coConnString); string sResult = ""; int counts = 0; string sql = "select * from pub_config where id='" + pubID.ToString() + "'"; List <Dictionary <string, object> > pubConfigRecords = coMyDB.GetRecords(sql, ref sResult, ref counts); if (sResult == mySqlDB.SUCCESS && counts > 0) { Dictionary <string, object> dicConfig = pubConfigRecords[0]; Dictionary <string, string> checkFields = new Dictionary <string, string>(); checkFields.Add("id", dicConfig["id"].ToString()); checkFields.Add("pub_name", dicConfig["pub_name"].ToString()); checkFields.Add("co_typeid", dicConfig["co_typeid"].ToString()); checkFields.Add("co_typename", dicConfig["co_typename"].ToString()); checkFields.Add("pub_typeid", dicConfig["pub_typeid"].ToString()); checkFields.Add("pub_typename", dicConfig["pub_typename"].ToString()); checkFields.Add("pub_nums", dicConfig["pub_nums"].ToString()); checkFields.Add("random_date_start", dicConfig["random_date_start"].ToString()); checkFields.Add("random_date_stop", dicConfig["random_date_stop"].ToString()); if (!validatePubConfig(checkFields)) { tboxErrorOutput.AppendText(string.Format("采集规则: (ID:{0}) 配置检查错误,请重新编辑采集规则项,确认必填项数据都已正确填写! \n", pubID)); } else { int coTypeid = int.Parse(dicConfig["co_typeid"].ToString()); int pubTypeid = int.Parse(dicConfig["pub_typeid"].ToString()); int pubNums = int.Parse(dicConfig["pub_nums"].ToString()); string randomDateStart = dicConfig["random_date_start"].ToString(); string randomDateStop = dicConfig["random_date_stop"].ToString(); string[] pubFilterKeywords = new string[0]; if (dicConfig["pub_filter_keywords"].ToString() != "") { pubFilterKeywords = dicConfig["pub_filter_keywords"].ToString().Split('|'); } ArticlePublish articlePublish = new ArticlePublish(pubID, _coConnString, _pubConnString, _pubTablePrename, coTypeid, pubTypeid, pubNums, pubFilterKeywords, randomDateStart, randomDateStop); CancellationTokenSource cancelTokenSource = new CancellationTokenSource(); articlePublish.CancelTokenSource = cancelTokenSource; //articlePublish.ProcessPublishArticles(); //创建发布任务的监控时钟,并且开始记时 Stopwatch sw = new Stopwatch(); sw.Start(); //通过委托代理异步执行发布任务 PublishProcess publishProcesArticles = new PublishProcess(ProcessPublishArticles); publishProcesArticles.BeginInvoke(articlePublish, ProcessPublishArticlesComplete, null); //创建新的Dictionary集合,其中包括采集对象,包括用来监控耗时的Stopwatch对象 Dictionary <string, object> oneCollect = new Dictionary <string, object>(); oneCollect.Add("publish", articlePublish); oneCollect.Add("watch", sw); //将当前采集对象添加到全局用来监控采集进程的采集对象集合中。 bool addResult = false; do { addResult = _articlePubCollections.TryAdd(pubID, oneCollect); } while (!addResult); } } else { tboxErrorOutput.AppendText(string.Format("发布规则(ID:{0}) 读取数据库采集配置错误!:{1} \n", pubID, sResult)); } } }