You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
125 lines
2.8 KiB
125 lines
2.8 KiB
using Apewer;
|
|
using System;
|
|
using System.Collections.Generic;
|
|
using System.Text;
|
|
using System.Threading;
|
|
|
|
namespace Apewer.Web
|
|
{
|
|
|
|
internal sealed class CronInstance
|
|
{
|
|
|
|
private Thread _thread = null;
|
|
private Action _action = null;
|
|
private Type _type = null;
|
|
private bool _break = false;
|
|
private bool _latest = false;
|
|
private CronAttribute _attribute = null;
|
|
private Nullable<DateTime> _ended = null;
|
|
|
|
public CronInvoker Invoker { get; set; }
|
|
|
|
public Thread Thread
|
|
{
|
|
get { return _thread; }
|
|
}
|
|
|
|
public bool Alive
|
|
{
|
|
get { return GetAlive(); }
|
|
}
|
|
|
|
/// <summary>再次启动 Cron 的时间间隔。</summary>
|
|
public int Interval
|
|
{
|
|
get { return GetInterval(); }
|
|
}
|
|
|
|
/// <summary>最后一次检查的 Alive 值。</summary>
|
|
public bool Latest
|
|
{
|
|
get { return _latest; }
|
|
set { _latest = value; }
|
|
}
|
|
|
|
public Type Type
|
|
{
|
|
get { return _type; }
|
|
set { _type = value; }
|
|
}
|
|
|
|
public bool Break
|
|
{
|
|
get { return _break; }
|
|
set { _break = value; }
|
|
}
|
|
|
|
public CronAttribute Attribute
|
|
{
|
|
get { return _attribute; }
|
|
set { _attribute = value; }
|
|
}
|
|
|
|
public Nullable<DateTime> Ended
|
|
{
|
|
get { return _ended; }
|
|
set { _ended = value; }
|
|
}
|
|
|
|
public CronInstance()
|
|
{
|
|
_thread = new Thread(Listen);
|
|
_thread.IsBackground = true;
|
|
}
|
|
|
|
void Log(params object[] content) => Invoker?.Log(content);
|
|
|
|
public void Start()
|
|
{
|
|
if (Alive) return;
|
|
_thread = new Thread(Listen);
|
|
_thread.IsBackground = true;
|
|
_thread.Start();
|
|
}
|
|
|
|
public void Abort()
|
|
{
|
|
if (_thread != null)
|
|
{
|
|
_thread.Abort();
|
|
_thread = null;
|
|
}
|
|
}
|
|
|
|
int GetInterval()
|
|
{
|
|
if (Attribute != null) return Attribute.Interval;
|
|
return CronAttribute.DefaultInterval;
|
|
}
|
|
|
|
bool GetAlive()
|
|
{
|
|
if (_thread == null) return false;
|
|
if (_thread.IsAlive != true) return false;
|
|
if (Thread.ThreadState != ThreadState.Running) return false;
|
|
return true;
|
|
}
|
|
|
|
void Listen()
|
|
{
|
|
if (Type == null) return;
|
|
try
|
|
{
|
|
Activator.CreateInstance(Type);
|
|
}
|
|
catch (Exception exception)
|
|
{
|
|
Log(Type.FullName, exception.GetType().FullName, exception.Message);
|
|
}
|
|
_thread = null;
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|