Search in sources :

Example 6 with JobImpl

use of org.apache.sling.event.impl.jobs.JobImpl in project sling by apache.

the class JobQueueImpl method startJob.

private void startJob(final JobHandler handler) {
    try {
        this.closeMarker.set(false);
        try {
            final JobImpl job = handler.getJob();
            handler.started = System.currentTimeMillis();
            this.services.configuration.getAuditLogger().debug("START OK : {}", job.getId());
            // sanity check for the queued property
            Calendar queued = job.getProperty(JobImpl.PROPERTY_JOB_QUEUED, Calendar.class);
            if (queued == null) {
                // we simply use a date of ten seconds ago
                queued = Calendar.getInstance();
                queued.setTimeInMillis(System.currentTimeMillis() - 10000);
            }
            final long queueTime = handler.started - queued.getTimeInMillis();
            // update statistics
            this.services.statisticsManager.jobStarted(this.queueName, job.getTopic(), queueTime);
            // send notification
            NotificationUtility.sendNotification(this.services.eventAdmin, NotificationConstants.TOPIC_JOB_STARTED, job, queueTime);
            synchronized (this.processingJobsLists) {
                this.processingJobsLists.put(job.getId(), handler);
            }
            JobExecutionResultImpl result = JobExecutionResultImpl.CANCELLED;
            Job.JobState resultState = Job.JobState.ERROR;
            final JobExecutionContextImpl ctx = new JobExecutionContextImpl(handler, new JobExecutionContextImpl.ASyncHandler() {

                @Override
                public void finished(final JobState state) {
                    services.jobConsumerManager.unregisterListener(job.getId());
                    finishedJob(job.getId(), state, true);
                    asyncCounter.decrementAndGet();
                }
            });
            try {
                synchronized (ctx) {
                    result = (JobExecutionResultImpl) handler.getConsumer().process(job, ctx);
                    if (result == null) {
                        // ASYNC processing
                        services.jobConsumerManager.registerListener(job.getId(), handler.getConsumer(), ctx);
                        asyncCounter.incrementAndGet();
                        ctx.markAsync();
                    } else {
                        if (result.succeeded()) {
                            resultState = Job.JobState.SUCCEEDED;
                        } else if (result.failed()) {
                            resultState = Job.JobState.QUEUED;
                        } else if (result.cancelled()) {
                            if (handler.isStopped()) {
                                resultState = Job.JobState.STOPPED;
                            } else {
                                resultState = Job.JobState.ERROR;
                            }
                        }
                    }
                }
            } catch (final Throwable t) {
                //NOSONAR
                logger.error("Unhandled error occured in job processor " + t.getMessage() + " while processing job " + Utility.toString(job), t);
                // we don't reschedule if an exception occurs
                result = JobExecutionResultImpl.CANCELLED;
                resultState = Job.JobState.ERROR;
            } finally {
                if (result != null) {
                    if (result.getRetryDelayInMs() != null) {
                        job.setProperty(JobImpl.PROPERTY_DELAY_OVERRIDE, result.getRetryDelayInMs());
                    }
                    if (result.getMessage() != null) {
                        job.setProperty(Job.PROPERTY_RESULT_MESSAGE, result.getMessage());
                    }
                    this.finishedJob(job.getId(), resultState, false);
                }
            }
        } catch (final Exception re) {
            // if an exception occurs, we just log
            this.logger.error("Exception during job processing.", re);
        }
    } finally {
        this.available.release();
    }
}
Also used : JobImpl(org.apache.sling.event.impl.jobs.JobImpl) Calendar(java.util.Calendar) JobState(org.apache.sling.event.jobs.Job.JobState) InternalJobState(org.apache.sling.event.impl.jobs.InternalJobState) JobState(org.apache.sling.event.jobs.Job.JobState) Job(org.apache.sling.event.jobs.Job) PersistenceException(org.apache.sling.api.resource.PersistenceException)

Example 7 with JobImpl

use of org.apache.sling.event.impl.jobs.JobImpl in project sling by apache.

the class NotificationUtility method sendNotification.

/**
     * Helper method for sending the notification events.
     */
public static void sendNotification(final EventAdmin eventAdmin, final String eventTopic, final Job job, final Long time) {
    if (eventAdmin != null) {
        // create new copy of job object
        final Job jobCopy = new JobImpl(job.getTopic(), job.getId(), ((JobImpl) job).getProperties());
        sendNotificationInternal(eventAdmin, eventTopic, jobCopy, time);
    }
}
Also used : JobImpl(org.apache.sling.event.impl.jobs.JobImpl) Job(org.apache.sling.event.jobs.Job)

Example 8 with JobImpl

use of org.apache.sling.event.impl.jobs.JobImpl in project sling by apache.

the class HistoryCleanUpTaskTest method setUpJob.

private void setUpJob() {
    Map<String, Object> parameters = Maps.<String, Object>newHashMap();
    parameters.put("age", MAX_AGE_IN_DAYS * 24 * 60);
    job = new JobImpl("not-relevant", "not-relevant_123", parameters);
    Mockito.when(jobContext.isStopped()).thenReturn(false);
}
Also used : JobImpl(org.apache.sling.event.impl.jobs.JobImpl)

Aggregations

JobImpl (org.apache.sling.event.impl.jobs.JobImpl)8 PersistenceException (org.apache.sling.api.resource.PersistenceException)3 Resource (org.apache.sling.api.resource.Resource)3 JobTopicTraverser (org.apache.sling.event.impl.jobs.JobTopicTraverser)3 ArrayList (java.util.ArrayList)2 Calendar (java.util.Calendar)2 ResourceResolver (org.apache.sling.api.resource.ResourceResolver)2 JobHandler (org.apache.sling.event.impl.jobs.JobHandler)2 Job (org.apache.sling.event.jobs.Job)2 HashMap (java.util.HashMap)1 HashSet (java.util.HashSet)1 List (java.util.List)1 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)1 ModifiableValueMap (org.apache.sling.api.resource.ModifiableValueMap)1 InternalJobState (org.apache.sling.event.impl.jobs.InternalJobState)1 BatchResourceRemover (org.apache.sling.event.impl.support.BatchResourceRemover)1 JobState (org.apache.sling.event.jobs.Job.JobState)1 JobExecutor (org.apache.sling.event.jobs.consumer.JobExecutor)1