示例#1
0
		public long ExecuteTask(DbSession dbSession, IOrganisationSessionLocal organisationComponent)
		{
			long millisToWait = DEFAULT_INTERVAL;

			DateTime now = DateTime.Now;

			IEnumerator iter = dbSession.Iterate(queryFindJobsToBeExecuted, now, DbType.TIMESTAMP).GetEnumerator();
			if (iter.MoveNext())
			{
				JobImpl job = (JobImpl) iter.Current;

				try
				{
					log.Debug("executing activation '" + job.Id + "' scheduled for " + job.Date.ToString());
					log.Debug("activation's flow-context is :" + job.Context);

					String userId = job.UserId;

					DelegationImpl actionDelegation = job.ActionDelegation;

					ExecutionContextImpl executionContext = new ExecutionContextImpl(userId, dbSession, organisationComponent);
					IFlow context = job.Context;
					if (context != null)
					{
						executionContext.SetFlow(context);
						executionContext.SetProcessInstance(context.ProcessInstance);
						executionContext.SetProcessDefinition(context.ProcessInstance.ProcessDefinition);
					}
					else
					{
						executionContext.SetProcessDefinition(job.ProcessDefinition);
					}

					delegationHelper.DelegateScheduledAction(actionDelegation, executionContext);

				}
				catch (Exception t)
				{
					log.Error("scheduler-exception : couldn't perform task : " + t.Message, t);
				}

				dbSession.Delete(job);
				dbSession.Flush();
				if (iter.MoveNext())
				{
					return 0;
				}
			} 

			iter = dbSession.Iterate(queryFindJobsInTheFuture, now, DbType.TIMESTAMP).GetEnumerator();
			if (iter.MoveNext())
			{
				JobImpl activation = (JobImpl) iter.Current;
				long activationDate = activation.Date.Ticks;
				millisToWait = activationDate - now.Ticks;
				log.Debug("next activation is scheduled at " + activation.Date.ToString() + ", (in " + millisToWait + " millis)");
				if (millisToWait < 0)
					millisToWait = 0;
				if (millisToWait > DEFAULT_INTERVAL)
					millisToWait = DEFAULT_INTERVAL;
			}

			return millisToWait;
		}