Community Server 作业系统
作业系统是使用Timer对象使背景程序定期运行的一个框架,CS利用作业系统建立搜索索引等操作。CS的作业系统具有可配置性和可扩展性的优点,你很容易使用它创建更多的作业。
首先,作业的启动和停止需要在程序的启动和关闭进行,所以要有一个Module进行注册,在CS里就写在CSHttpModule中了。
public class CSHttpModule : IHttpModule
{
public void Init(HttpApplication application)
{
Jobs.Instance().Start();//作业启动
}
public void Dispose()
{
Jobs.Instance().Stop(); //用业关闭
}
}
作业系统需要几个类相互配合进行工作,分别是:Jobs、Job、IJob和实际IJob的工作类,如:PostAttachmentCleanupJob等。
Jobs类的Start()方法用于提取communityserver.config中设置作业列表,并实例化出对应的Job对象列表,在Job对象的InitializeTimer()期间,作业就会被启动,如果作业配置中使用了"singleThread = false",则作业会在单独的Timer对象中启动,可以设置自已的间隔时间,否则作业会在统一的一个Timer对象中启动,间隔时间都是统一的,默认情况下间隔时间为15分钟,可以通过配置minutes设置间隔时间。
Timer对象在实例化过程中会调用一个委托对象,用于循环作业时间,如:
singleTimer = new Timer(new TimerCallback(call_back),null,Interval, Interval);
在call_back方法中,会循环设置作业的下一个运行时间:
private void call_back(object state)
{
_isRunning = true;
_started = DateTime.Now;
singleTimer.Change( Timeout.Infinite, Timeout.Infinite );
foreach(Job job in jobList.Values)
{
if(job.Enabled && job.SingleThreaded)
{
job.ExecuteJob();
}
}
![](https://www.cnblogs.com/images/OutliningIndicators/InBlock.gif)
singleTimer.Change( Interval, Interval);
_isRunning = false;
_completed = DateTime.Now;
}
其中singleTimer.Change( Timeout.Infinite, Timeout.Infinite );表明必须完成作业指定的任务,才会开始下一个循环。
Job对象是存储一些配置信息和单独启动Timer对象。
IJob接口定义了使用作业系统的作业类必须实现的方法void Execute(XmlNode node);实现IJob接口的作业类从参数XmlNode node提取一些有用的配置信息,则可以有助于作业的运行。如:PostAttachmentCleanupJob类
public class PostAttachmentCleanupJob : IJob
{
public PostAttachmentCleanupJob()
{
}
![](https://www.cnblogs.com/images/OutliningIndicators/InBlock.gif)
public void Execute(XmlNode node)
{
XmlAttribute expiresAfterHoursNode = node.Attributes["expiresAfterHours"];
int expiresAfterHours = 2;
![](https://www.cnblogs.com/images/OutliningIndicators/InBlock.gif)
if(expiresAfterHoursNode != null)
{
try { expiresAfterHours = int.Parse(expiresAfterHoursNode.Value); }
catch { expiresAfterHours = 2; }
}
![](https://www.cnblogs.com/images/OutliningIndicators/InBlock.gif)
CommonDataProvider dp = CommonDataProvider.Instance();
dp.ExpireTemporaryPostAttachments(DateTime.Now.AddHours(-expiresAfterHours));
}
}
通过获取节点expiresAfterHours的值可以设置清除提定时间前的临时附件。如果你要定义一个新的作业,则只需添加一个作业类并实现IJob接口,并写在配置文件中。
首先,作业的启动和停止需要在程序的启动和关闭进行,所以要有一个Module进行注册,在CS里就写在CSHttpModule中了。
![](https://www.cnblogs.com/images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/images/OutliningIndicators/ExpandedBlockStart.gif)
![](https://www.cnblogs.com/images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/images/OutliningIndicators/ExpandedSubBlockStart.gif)
![](https://www.cnblogs.com/images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/images/OutliningIndicators/ExpandedSubBlockEnd.gif)
![](https://www.cnblogs.com/images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/images/OutliningIndicators/ExpandedSubBlockStart.gif)
![](https://www.cnblogs.com/images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/images/OutliningIndicators/ExpandedSubBlockEnd.gif)
![](https://www.cnblogs.com/images/OutliningIndicators/ExpandedBlockEnd.gif)
作业系统需要几个类相互配合进行工作,分别是:Jobs、Job、IJob和实际IJob的工作类,如:PostAttachmentCleanupJob等。
Jobs类的Start()方法用于提取communityserver.config中设置作业列表,并实例化出对应的Job对象列表,在Job对象的InitializeTimer()期间,作业就会被启动,如果作业配置中使用了"singleThread = false",则作业会在单独的Timer对象中启动,可以设置自已的间隔时间,否则作业会在统一的一个Timer对象中启动,间隔时间都是统一的,默认情况下间隔时间为15分钟,可以通过配置minutes设置间隔时间。
Timer对象在实例化过程中会调用一个委托对象,用于循环作业时间,如:
![](https://www.cnblogs.com/images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/images/OutliningIndicators/ExpandedBlockStart.gif)
![](https://www.cnblogs.com/images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/images/OutliningIndicators/ExpandedSubBlockStart.gif)
![](https://www.cnblogs.com/images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/images/OutliningIndicators/ExpandedSubBlockStart.gif)
![](https://www.cnblogs.com/images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/images/OutliningIndicators/ExpandedSubBlockEnd.gif)
![](https://www.cnblogs.com/images/OutliningIndicators/ExpandedSubBlockEnd.gif)
![](https://www.cnblogs.com/images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/images/OutliningIndicators/ExpandedBlockEnd.gif)
Job对象是存储一些配置信息和单独启动Timer对象。
IJob接口定义了使用作业系统的作业类必须实现的方法void Execute(XmlNode node);实现IJob接口的作业类从参数XmlNode node提取一些有用的配置信息,则可以有助于作业的运行。如:PostAttachmentCleanupJob类
![](https://www.cnblogs.com/images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/images/OutliningIndicators/ExpandedBlockStart.gif)
![](https://www.cnblogs.com/images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/images/OutliningIndicators/ExpandedSubBlockStart.gif)
![](https://www.cnblogs.com/images/OutliningIndicators/ExpandedSubBlockEnd.gif)
![](https://www.cnblogs.com/images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/images/OutliningIndicators/ExpandedSubBlockStart.gif)
![](https://www.cnblogs.com/images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/images/OutliningIndicators/ExpandedSubBlockStart.gif)
![](https://www.cnblogs.com/images/OutliningIndicators/ExpandedSubBlockStart.gif)
![](https://www.cnblogs.com/images/OutliningIndicators/ExpandedSubBlockStart.gif)
![](https://www.cnblogs.com/images/OutliningIndicators/ExpandedSubBlockEnd.gif)
![](https://www.cnblogs.com/images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/images/OutliningIndicators/ExpandedSubBlockEnd.gif)
![](https://www.cnblogs.com/images/OutliningIndicators/ExpandedBlockEnd.gif)