//完成发布任务后更新发布表中对应发布规则的已发布数量 private void updatePublishState(ArticlePublish articlePublish) { int pubID = articlePublish.PubID; int coTypeID = articlePublish.CoTypeID; int publishedNums = articlePublish.CurrentExportedArticles; mySqlDB coMyDB = new mySqlDB(_coConnString); string sResult = ""; int counts = 0; //更新发布配置表中的信息 string sql = "update pub_config set published_nums=published_nums+'" + publishedNums.ToString() + "'"; sql = sql + ",pub_export_date = CURRENT_TIMESTAMP"; sql = sql + " where id = '" + pubID.ToString() + "'"; counts = coMyDB.executeDMLSQL(sql, ref sResult); if (sResult != mySqlDB.SUCCESS) { List <Exception> pubException = articlePublish.PubException; Exception mysqlError = new Exception(sResult); pubException.Add(mysqlError); } //更新分类表中的统计信息,发不完以后对应的分类中的可发布数量应该减去当前的数量 sql = "update arc_type set unused_nums=unused_nums-'" + publishedNums.ToString() + "'"; sql = sql + " where tid='" + coTypeID.ToString() + "'"; counts = coMyDB.executeDMLSQL(sql, ref sResult); if (sResult != mySqlDB.SUCCESS) { List <Exception> pubException = articlePublish.PubException; Exception mysqlError = new Exception(sResult); pubException.Add(mysqlError); } }
//输出发布过程中的错误信息 private void printErrors(ArticlePublish articlePublish) { //输出错误信息 Exception cancelException = articlePublish.CancelException; if (cancelException != null) { tboxPubTestResult.AppendText(cancelException.Message + "\n"); if (cancelException.Data != null) { foreach (DictionaryEntry de in cancelException.Data) { tboxPubTestResult.AppendText(string.Format("{0} : {1} \n", de.Key, de.Value)); } } } List <Exception> listException = articlePublish.PubException; if (listException.Count > 0) { foreach (Exception item in listException) { tboxPubTestResult.AppendText(item.Message + "\n"); if (item.Data != null) { foreach (DictionaryEntry de in item.Data) { tboxPubTestResult.AppendText(string.Format("{0} : {1} \n", de.Key, de.Value)); } } } } }
//取消当前采集任务 private void cancelCurrentTask() { foreach (var pubItem in _articlePubCollections) { try { Dictionary <string, object> dic = pubItem.Value; long currentPubID = pubItem.Key; ArticlePublish currentPublishWork = (ArticlePublish)dic["publish"]; currentPublishWork.CancelTokenSource.Cancel(); } catch (Exception) { } } }
//用来监控当前并行采集中的各个进程的进度状态 private void updateForm(object state) { foreach (var pubItem in _articlePubCollections) { try { Dictionary <string, object> dic = pubItem.Value; int currentPubID = pubItem.Key; ArticlePublish currentCollectWork = (ArticlePublish)dic["publish"]; if (currentCollectWork.PubState != "发布完毕" && dic != null) { Stopwatch currentWatch = (Stopwatch)dic["watch"]; ListViewItem currentItem = listViewPubArticles.Items.Cast <ListViewItem>().First(item => item.SubItems[1].Text == currentPubID.ToString()); listViewPubArticles.BeginUpdate(); if (currentCollectWork.PubState != "") { currentItem.SubItems[0].Text = currentCollectWork.PubState; } currentItem.SubItems[6].Text = currentCollectWork.CurrentExportedArticles.ToString(); string timeUsed = currentWatch.Elapsed.ToString(); currentItem.SubItems[7].Text = timeUsed.Remove(8, 8); listViewPubArticles.EndUpdate(); } else if (dic == null) { tboxErrorOutput.AppendText(string.Format("ID: {0} NULL \n", currentPubID)); } if (_articlePubCollections.Count > 0) { labTime.Text = string.Format("总共耗时: {0}\n", swGlobal.Elapsed.ToString()); } else { swGlobal.Stop(); } } catch (Exception) { } } }
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 btnPubTest_Click(object sender, EventArgs e) { tabctrPubform.SelectedTab = tabPubTest; setVarValue(); //先将变量中的值更新成当前控件中的值 bool validateResult = validatePubConfig(); if (!validateResult) { MessageBox.Show("必填项未填写完整或未填写正确,请检查表达是否填写完整并正确填写!"); } else { int coTypeid = int.Parse(_coTypeid); int pubTypeid = int.Parse(_pubTypeid); string[] pubFilterKeywords = new string[0]; if (_pubFilterKeywords != "") { pubFilterKeywords = _pubFilterKeywords.Split('|'); } //测试发布一篇文章 ArticlePublish articlePublishTest = new ArticlePublish(_pubID, _coConnString, _pubConnString, _pubTablePrename, coTypeid, pubTypeid, 1, pubFilterKeywords, _randomDateStart, _randomDateStop); cancelTokenSource = new CancellationTokenSource(); articlePublishTest.CancelTokenSource = cancelTokenSource; articlePublishTest.ProcessPublishArticles(); long aid = articlePublishTest.LastExportedCoid; long cmsAid = articlePublishTest.LastExportedCmsid; int exportedNums = articlePublishTest.CurrentExportedArticles; if (aid == -1 || cmsAid == -1) { Exception cancelException = articlePublishTest.CancelException; if (cancelException != null) { tboxPubTestResult.AppendText(cancelException.Message + "\n"); if (cancelException.Data != null) { foreach (DictionaryEntry de in cancelException.Data) { tboxPubTestResult.AppendText(string.Format("{0} : {1} \n", de.Key, de.Value)); } } } List <Exception> listException = articlePublishTest.PubException; if (listException.Count > 0) { foreach (Exception item in listException) { tboxPubTestResult.AppendText(item.Message + "\n"); if (item.Data != null) { foreach (DictionaryEntry de in item.Data) { tboxPubTestResult.AppendText(string.Format("{0} : {1} \n", de.Key, de.Value)); } } } } } else { tboxPubTestResult.AppendText(string.Format("发布文章成功! 采集文章ID是:{0} CMS文章ID是:{1} \n", aid, cmsAid)); tboxPubTestResult.AppendText(string.Format("此次发布数量:{0}\n", exportedNums)); updatePublishState(articlePublishTest); printErrors(articlePublishTest); } } }
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)); } } }
private ArticlePublish ProcessPublishArticles(ArticlePublish articlePublish) { articlePublish.ProcessPublishArticles(); return(articlePublish); }