public static void onScheduledWork(Scheduler scheduler) throws Exception {
		RequestedAction nextAction = CONTINUE_ACTION;
		boolean idleOrPaused = false;

		while (nextAction == CONTINUE_ACTION) {
			if (scheduler.isIdle() || scheduler.isPausing) {
				idleOrPaused = true;
				break;
			}

			WorkItem currentWorkItem = scheduler.firstWorkItem;

			scheduler.firstWorkItem = scheduler.workItemQueue != null &&
					scheduler.workItemQueue.count() > 0 ?
					scheduler.workItemQueue.dequeue() :
					null;

			nextAction = scheduler.executeWorkItem(currentWorkItem);
		}

		// we must process events or dispose workflow resources until idle or paused

		if (idleOrPaused || nextAction == ABORT_ACTION) {
			scheduler.isRunning = false;
			scheduler.isPausing = false;
			scheduler.scheduleIdle();
			return;
		}

		if (nextAction != YIELD_SILENTLY_ACTION) {
			Helper.assertEquals(NotifyUnhandledExceptionAction.class, nextAction.getClass());
			NotifyUnhandledExceptionAction notifyAction = (NotifyUnhandledExceptionAction) nextAction;
			scheduler.isRunning = false;
			scheduler.notifyUnhandledException(notifyAction.getException(), notifyAction.getSource());
		}
	}