using System;
using System.Collections.Generic;
using System.Text;
using Ubik.Engine.Server;
using Ubik.Engine.Client;
using System.Reflection;
using StoresAndStockPricing.Model;
namespace StoresAndStockPricing.ServerTasks{
public class DataMaintenanceTask : Ubik.Engine.Server.ServerTask
{
public DataMaintenanceTask()
: base(new TimeSpan(0, 0, 10))
{
}
public override string ToString()
{
return DataMaintenanceTaskResources.TaskName;
}
protected override void DoWork(Server server)
{
if (server == null)
throw new ArgumentNullException("server");
LocalEndpoint endpoint = new LocalEndpoint(server);
List<Assembly> modelAssemblies = new List<Assembly>();
modelAssemblies.Add(Assembly.Load("StoresAndStockPricing.Model"));
using (Session session = new Session(endpoint, modelAssemblies))
{
IList<DataMaintenanceEvent> waitingEvents;
using (Transaction txn = session.BeginTransaction())
{
waitingEvents = session.Select<DataMaintenanceEvent>(
"Status = DataMaintenanceEventStatus.Pending && PlannedDateTime <= #"
+ EngineDateTime.UtcNow.ToString() + "#");
foreach (DataMaintenanceEvent dmEvent in waitingEvents)
{
dmEvent.Detected();
}
txn.Commit();
}
foreach (DataMaintenanceEvent dmEvent in waitingEvents)
{
try
{
using (Transaction txn = session.BeginTransaction())
{
dmEvent.Apply();
txn.Commit();
}
}
catch (Exception ex)
{
// Exceptions thrown from here indicate a server issue, so we will let them propagate
// and try again once the server is back online.
using (Transaction txn = session.BeginTransaction())
{
dmEvent.RecordFailure(ex);
txn.Commit();
}
}
}
}
}
}
}
|