Search in sources :

Example 1 with HumanTaskJobDAO

use of org.wso2.carbon.humantask.core.dao.HumanTaskJobDAO in project carbon-business-process by wso2.

the class SimpleScheduler method updateJob.

/**
 * Update the schedule time for a job
 *
 * @param taskId        Task ID
 * @param scheduledTime Time to be updated
 * @param name          Name of the task
 */
public void updateJob(Long taskId, String name, Long scheduledTime) throws InvalidJobsInDbException, InvalidUpdateRequestException {
    long now = System.currentTimeMillis();
    if (now > scheduledTime) {
        String errMessage = "Current time: " + now + " > request time: " + scheduledTime;
        throw new InvalidUpdateRequestException(errMessage);
    }
    boolean immediate = scheduledTime <= now + immediateInterval;
    boolean nearfuture = !immediate && scheduledTime <= now + nearFutureInterval;
    Long jobId = getConnection().updateJob(taskId, name, immediate, nearfuture, nodeId, scheduledTime);
    if (jobId > -1) {
        // one job is found
        todo.dequeue(new Job(jobId));
        // We ignore if the job is not in the Map outstandingJobs
        outstandingJobs.remove(jobId);
        // Loading/Refresh the job here, in-order to update the job for the latest changes.
        // Otherwise when the next immediate load task runs, it still fetch the job with the
        // old updates.
        ParameterizedType genericSuperClass = (ParameterizedType) getConnection().getClass().getGenericSuperclass();
        Class entityClass = (Class) genericSuperClass.getActualTypeArguments()[0];
        HumanTaskJobDAO updatedJob = (HumanTaskJobDAO) getEntityManager().find(entityClass, jobId);
        getEntityManager().refresh(updatedJob);
        if (immediate) {
            // Immediate scheduling means we add the job immediately to the todo list and
            // we put it in the DB for safe keeping
            addTodoList(new Job(updatedJob));
        } else if (nearfuture) {
            // Re-schedule load-immediate job task
            todo.clearTasks(LoadImmediateTask.class);
            todo.dequeue(new Job(jobId));
            // We ignore if the job is not in the Map outstandingJobs
            outstandingJobs.remove(jobId);
            todo.enqueue(new LoadImmediateTask(System.currentTimeMillis() + 1000));
        } else {
            todo.clearTasks(UpgradeJobsTask.class);
            todo.enqueue(new UpgradeJobsTask(System.currentTimeMillis() + 1000));
        }
    }
}
Also used : ParameterizedType(java.lang.reflect.ParameterizedType) AtomicLong(java.util.concurrent.atomic.AtomicLong) InvalidUpdateRequestException(org.wso2.carbon.humantask.core.api.scheduler.InvalidUpdateRequestException) HumanTaskJobDAO(org.wso2.carbon.humantask.core.dao.HumanTaskJobDAO)

Example 2 with HumanTaskJobDAO

use of org.wso2.carbon.humantask.core.dao.HumanTaskJobDAO in project carbon-business-process by wso2.

the class SimpleScheduler method doLoadImmediate.

boolean doLoadImmediate() {
    if (log.isDebugEnabled()) {
        log.debug("LOAD IMMEDIATE started");
    }
    // don't load anything if we're already half-full;  we've got plenty to do already
    if (outstandingJobs.size() > todoLimit / 2) {
        return true;
    }
    List<Job> jobs = new ArrayList<Job>();
    try {
        // don't load more than we can chew
        int tps = 100;
        final int batch = Math.min((int) (immediateInterval * tps / 1000), todoLimit - outstandingJobs.size());
        // jobs might have been enqueued by #addTodoList meanwhile
        if (batch <= 0) {
            if (log.isDebugEnabled()) {
                log.debug("Max capacity reached: " + outstandingJobs.size() + " jobs dispacthed i.e. queued or being executed");
            }
            return true;
        }
        if (log.isDebugEnabled()) {
            log.debug("Started loading " + batch + " jobs from db");
        }
        // jobs = _db.dequeueImmediate(_nodeId, System.currentTimeMillis() + _immediateInterval, batch);
        List<HumanTaskJobDAO> htJobs = execTransaction(new Callable<List<HumanTaskJobDAO>>() {

            public List<HumanTaskJobDAO> call() throws Exception {
                return getConnection().dequeueImmediate(nodeId, System.currentTimeMillis() + immediateInterval, batch);
            }
        });
        for (HumanTaskJobDAO htJob : htJobs) {
            jobs.add(new Job(htJob));
        }
        if (log.isDebugEnabled()) {
            log.debug("loaded " + jobs.size() + " jobs from db");
        }
        long warningDelay = 0;
        long delayedTime = System.currentTimeMillis() - warningDelay;
        int delayedCount = 0;
        boolean runningLate;
        AbsoluteTimeDateFormat f = new AbsoluteTimeDateFormat();
        for (Job j : jobs) {
            // jobs might have been enqueued by #addTodoList meanwhile
            if (outstandingJobs.size() >= todoLimit) {
                if (log.isDebugEnabled()) {
                    log.debug("Max capacity reached: " + outstandingJobs.size() + " jobs dispacthed i.e. queued or being executed");
                }
                break;
            }
            runningLate = j.schedDate <= delayedTime;
            if (runningLate) {
                // TODO run the job here
                delayedCount++;
            }
            if (log.isDebugEnabled()) {
                log.debug("todo.enqueue job from db: " + j.getJobID() + " for " + j.schedDate + "(" + f.format(j.schedDate) + ") " + (runningLate ? " delayed=true" : ""));
            }
            enqueue(j);
        }
        if (delayedCount > 0) {
            log.warn("Dispatching jobs with more than " + (warningDelay / 60000) + " minutes delay. Either the server was down for some time or the job " + "load is greater than available capacity");
        }
        // clear only if the batch succeeded
        processedSinceLastLoadTask.clear();
        return true;
    } catch (Exception ex) {
        log.error("Error loading immediate jobs from database.", ex);
        return false;
    } finally {
        if (log.isDebugEnabled()) {
            log.debug("LOAD IMMEDIATE complete");
        }
    }
}
Also used : AbsoluteTimeDateFormat(org.apache.log4j.helpers.AbsoluteTimeDateFormat) HumanTaskJobDAO(org.wso2.carbon.humantask.core.dao.HumanTaskJobDAO) HumanTaskException(org.wso2.carbon.humantask.core.engine.HumanTaskException) InvalidJobsInDbException(org.wso2.carbon.humantask.core.api.scheduler.InvalidJobsInDbException) InvalidUpdateRequestException(org.wso2.carbon.humantask.core.api.scheduler.InvalidUpdateRequestException)

Example 3 with HumanTaskJobDAO

use of org.wso2.carbon.humantask.core.dao.HumanTaskJobDAO in project carbon-business-process by wso2.

the class SimpleScheduler method scheduleJob.

public long scheduleJob(long now, long scheduledTime, JobType type, String details, long taskId, String name) {
    boolean immediate = scheduledTime <= now + immediateInterval;
    boolean nearfuture = !immediate && scheduledTime <= now + nearFutureInterval;
    HumanTaskJobDAO tempJob = HumanTaskServiceComponent.getHumanTaskServer().getDaoConnectionFactory().getConnection().createHumanTaskJobDao();
    tempJob.setTime(scheduledTime);
    tempJob.setTransacted(false);
    tempJob.setDetails(details);
    tempJob.setTaskId(taskId);
    tempJob.setName(name);
    tempJob.setType(type.toString());
    if (immediate) {
        // Immediate scheduling means we put it in the DB for safe keeping
        // _db.insertJob(job, _nodeId, true);
        tempJob.setNodeId(nodeId);
        tempJob.setScheduled(true);
        getEntityManager().persist(tempJob);
        // And add it to our todo list .
        if (outstandingJobs.size() < todoLimit) {
            addTodoList(new Job(tempJob));
        }
        if (log.isDebugEnabled()) {
            log.debug("scheduled immediate job: " + tempJob.getId());
        }
    } else if (nearfuture) {
        // Near future, assign the job to ourselves (why? -- this makes it very unlikely that we
        // would get two nodes trying to process the same instance, which causes unsightly rollbacks).
        // _db.insertJob(job, _nodeId, false);
        tempJob.setNodeId(nodeId);
        tempJob.setScheduled(false);
        getEntityManager().persist(tempJob);
        if (log.isDebugEnabled()) {
            log.debug("scheduled near-future job: " + tempJob.getId());
        }
    } else /* far future */
    {
        // Not the near future, we don't assign a node-id, we'll assign it later.
        // _db.insertJob(job, null, false);
        tempJob.setNodeId(null);
        tempJob.setScheduled(false);
        getEntityManager().persist(tempJob);
        if (log.isDebugEnabled()) {
            log.debug("scheduled far-future job: " + tempJob.getId());
        }
    }
    return tempJob.getId();
}
Also used : HumanTaskJobDAO(org.wso2.carbon.humantask.core.dao.HumanTaskJobDAO)

Aggregations

HumanTaskJobDAO (org.wso2.carbon.humantask.core.dao.HumanTaskJobDAO)3 InvalidUpdateRequestException (org.wso2.carbon.humantask.core.api.scheduler.InvalidUpdateRequestException)2 ParameterizedType (java.lang.reflect.ParameterizedType)1 AtomicLong (java.util.concurrent.atomic.AtomicLong)1 AbsoluteTimeDateFormat (org.apache.log4j.helpers.AbsoluteTimeDateFormat)1 InvalidJobsInDbException (org.wso2.carbon.humantask.core.api.scheduler.InvalidJobsInDbException)1 HumanTaskException (org.wso2.carbon.humantask.core.engine.HumanTaskException)1