📄 job.cs
字号:
//------------------------------------------------------------------------------
// <copyright company="Telligent Systems">
// Copyright (c) Telligent Systems Corporation. All rights reserved.
// </copyright>
//------------------------------------------------------------------------------
using System;
using System.Threading;
using System.Xml;
using System.Xml.Serialization;
using CommunityServer.Components;
namespace CommunityServer.Configuration
{
/// <summary>
/// Configurable IJob descritpion
/// </summary>
[Serializable]
[XmlRoot("job")]
public class Job : IDisposable
{
public Job(Type ijob, XmlNode node)
{
_node = node;
_jobType = ijob;
XmlAttribute att = node.Attributes["enabled"];
if(att != null)
this._enabled = bool.Parse(att.Value);
att = node.Attributes["enableShutDown"];
if(att != null)
this._enableShutDown = bool.Parse(att.Value);
att = node.Attributes["name"];
if(att != null)
this._name = att.Value;
att = node.Attributes["minutes"];
if(att != null)
{
try
{
this._minutes = Int32.Parse(att.Value);
}
catch
{
this._minutes = 15;
}
}
att = node.Attributes["singleThread"];
if(att != null && !Globals.IsNullorEmpty(att.Value) && string.Compare(att.Value,"false", false) == 0)
_singleThread = false;
}
#region Private Members
private IJob _ijob;
private bool _enabled = true;
private Type _jobType;
private string _name;
private bool _enableShutDown = false;
private int _minutes = 15;
private Timer _timer = null;
private bool disposed = false;
private XmlNode _node = null;
private bool _singleThread = true;
private DateTime _lastStart;
private DateTime _lastSucess;
private DateTime _lastEnd;
private bool _isRunning;
protected int Interval
{
get
{
//#if DEBUG
// return Minutes * 12000;
//#else
return Minutes * 60000;
//#endif
}
}
#endregion
/// <summary>
/// Creates the timer and sets the callback if it is enabled
/// </summary>
public void InitializeTimer(Guid id)
{
if(_timer == null && Enabled)
{
_timer = new Timer(new TimerCallback(timer_Callback), id,Interval, Interval);
}
}
/// <summary>
/// Internal call back which is responsible for firing IJob.Execute()
/// </summary>
/// <param name="state"></param>
private void timer_Callback(object state)
{
Guid id = (Guid)state;
if(id != Jobs.Instance().CurrentID)
{
this.Dispose();
return;
}
if(!Enabled)
return;
_timer.Change( Timeout.Infinite, Timeout.Infinite );
ExecuteJob();
if(Enabled)
_timer.Change( Interval, Interval);
else
this.Dispose();
}
public void ExecuteJob()
{
_isRunning = true;
IJob ijob = this.CreateJobInstance();
if(ijob != null)
{
_lastStart = DateTime.Now;
try
{
ijob.Execute(this._node);
_lastEnd = _lastSucess = DateTime.Now;
}
catch(Exception)
{
this._enabled = !this.EnableShutDown;
_lastEnd = DateTime.Now;
}
}
_isRunning = false;
}
#region Public Properities
public bool IsRunning
{
get{return _isRunning;}
}
public DateTime LastStarted
{
get{ return _lastStart;}
}
public DateTime LastEnd
{
get{ return _lastEnd;}
}
public DateTime LastSuccess
{
get{ return _lastSucess;}
}
public bool SingleThreaded
{
get{ return _singleThread;}
}
/// <summary>
/// Named type of class which implements IJob
/// </summary>
public Type JobType
{
get { return this._jobType; }
}
public int Minutes
{
get{return _minutes;}
set{_minutes = value;}
}
/// <summary>
/// On an exception, should this job be deactivated
/// </summary>
public bool EnableShutDown
{
get { return this._enableShutDown; }
}
/// <summary>
/// Name of Job
/// </summary>
public string Name
{
get { return this._name; }
}
/// <summary>
/// Is this job enabled
/// </summary>
public bool Enabled
{
get { return this._enabled; }
}
/// <summary>
/// Attempts to create an instance of the IJob. If the type
/// can not be created, this Job will be disabled.
/// </summary>
/// <returns></returns>
public IJob CreateJobInstance()
{
if(Enabled)
{
if(_ijob == null)
{
if(_jobType != null)
{
_ijob = Activator.CreateInstance(_jobType) as IJob;
}
_enabled = (_ijob != null);
if(!_enabled)
this.Dispose();
}
}
return _ijob;
}
#endregion
#region IDisposable Members
public void Dispose()
{
if(_timer!= null && !disposed)
{
lock(this)
{
_timer.Dispose();
_timer = null;
disposed = true;
}
}
}
#endregion
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -