use of org.apache.sling.api.resource.PersistenceException in project sling by apache.
the class JobHandler method reschedule.
/**
* Reschedule the job
* Update the retry count and remove the started time.
* @return <code>true</code> if rescheduling was successful, <code>false</code> otherwise.
*/
public boolean reschedule() {
final ResourceResolver resolver = this.configuration.createResourceResolver();
try {
final Resource jobResource = resolver.getResource(job.getResourcePath());
if (jobResource != null) {
final ModifiableValueMap mvm = jobResource.adaptTo(ModifiableValueMap.class);
mvm.put(Job.PROPERTY_JOB_RETRY_COUNT, job.getProperty(Job.PROPERTY_JOB_RETRY_COUNT, Integer.class));
if (job.getProperty(Job.PROPERTY_RESULT_MESSAGE) != null) {
mvm.put(Job.PROPERTY_RESULT_MESSAGE, job.getProperty(Job.PROPERTY_RESULT_MESSAGE));
}
mvm.remove(Job.PROPERTY_JOB_STARTED_TIME);
mvm.put(JobImpl.PROPERTY_JOB_QUEUED, Calendar.getInstance());
try {
resolver.commit();
return true;
} catch (final PersistenceException pe) {
this.configuration.getMainLogger().debug("Unable to update reschedule properties for job " + job.getId(), pe);
}
}
} finally {
resolver.close();
}
return false;
}
use of org.apache.sling.api.resource.PersistenceException in project sling by apache.
the class JobHandler method persistJobProperties.
/**
* Update the property of a job in the resource tree
* @param propNames the property names to update
* @return {@code true} if the update was successful.
*/
public boolean persistJobProperties(final String... propNames) {
if (propNames != null) {
final ResourceResolver resolver = this.configuration.createResourceResolver();
try {
final Resource jobResource = resolver.getResource(job.getResourcePath());
if (jobResource != null) {
final ModifiableValueMap mvm = jobResource.adaptTo(ModifiableValueMap.class);
for (final String propName : propNames) {
final Object val = job.getProperty(propName);
if (val != null) {
if (val.getClass().isEnum()) {
mvm.put(propName, val.toString());
} else {
mvm.put(propName, val);
}
} else {
mvm.remove(propName);
}
}
resolver.commit();
return true;
} else {
this.configuration.getMainLogger().debug("No job resource found at {}", job.getResourcePath());
}
} catch (final PersistenceException ignore) {
this.configuration.getMainLogger().debug("Unable to persist properties", ignore);
} finally {
resolver.close();
}
return false;
}
return true;
}
use of org.apache.sling.api.resource.PersistenceException in project sling by apache.
the class AbstractJobHandlingTest method cleanup.
public void cleanup() {
// clean job area
final ServiceReference<ResourceResolverFactory> ref = this.bc.getServiceReference(ResourceResolverFactory.class);
final ResourceResolverFactory factory = this.bc.getService(ref);
ResourceResolver resolver = null;
try {
resolver = factory.getAdministrativeResourceResolver(null);
final Resource rsrc = resolver.getResource("/var/eventing");
if (rsrc != null) {
delete(rsrc);
resolver.commit();
}
} catch (final LoginException le) {
// ignore
} catch (final PersistenceException e) {
// ignore
} catch (final Exception e) {
// sometimes an NPE is thrown from the repository, as we
// are in the cleanup, we can ignore this
} finally {
if (resolver != null) {
resolver.close();
}
}
// unregister all services
for (final ServiceRegistration<?> reg : this.registrations) {
reg.unregister();
}
this.registrations.clear();
// remove all configurations
try {
final org.osgi.service.cm.Configuration[] cfgs = this.configAdmin.listConfigurations(null);
if (cfgs != null) {
for (final org.osgi.service.cm.Configuration c : cfgs) {
try {
c.delete();
} catch (final IOException io) {
// ignore
}
}
}
} catch (final IOException io) {
// ignore
} catch (final InvalidSyntaxException e) {
// ignore
}
this.sleep(1000);
}
use of org.apache.sling.api.resource.PersistenceException in project sling by apache.
the class JobQueueImpl method removeAll.
/**
* @see org.apache.sling.event.jobs.Queue#removeAll()
*/
@Override
public synchronized void removeAll() {
final Set<String> topics = this.cache.getTopics();
logger.debug("Removing all jobs for queue {} : {}", queueName, topics);
if (!topics.isEmpty()) {
final ResourceResolver resolver = this.services.configuration.createResourceResolver();
try {
final Resource baseResource = resolver.getResource(this.services.configuration.getLocalJobsPath());
// sanity check - should never be null
if (baseResource != null) {
final BatchResourceRemover brr = new BatchResourceRemover();
for (final String t : topics) {
final Resource topicResource = baseResource.getChild(t.replace('/', '.'));
if (topicResource != null) {
JobTopicTraverser.traverse(logger, topicResource, new JobTopicTraverser.JobCallback() {
@Override
public boolean handle(final JobImpl job) {
final Resource jobResource = topicResource.getResourceResolver().getResource(job.getResourcePath());
// sanity check
if (jobResource != null) {
try {
brr.delete(jobResource);
} catch (final PersistenceException ignore) {
logger.error("Unable to remove job " + job, ignore);
topicResource.getResourceResolver().revert();
topicResource.getResourceResolver().refresh();
}
}
return true;
}
});
}
}
try {
resolver.commit();
} catch (final PersistenceException ignore) {
logger.error("Unable to remove jobs", ignore);
}
}
} finally {
resolver.close();
}
}
}
use of org.apache.sling.api.resource.PersistenceException in project sling by apache.
the class JobSchedulerImpl method setSuspended.
/**
* Change the suspended flag for a scheduled job
* @param info The schedule info
* @param flag The corresponding flag
*/
public void setSuspended(final ScheduledJobInfoImpl info, final boolean flag) {
final ResourceResolver resolver = configuration.createResourceResolver();
try {
final StringBuilder sb = new StringBuilder(this.configuration.getScheduledJobsPath(true));
sb.append(ResourceHelper.filterName(info.getName()));
final String path = sb.toString();
final Resource eventResource = resolver.getResource(path);
if (eventResource != null) {
final ModifiableValueMap mvm = eventResource.adaptTo(ModifiableValueMap.class);
if (flag) {
mvm.put(ResourceHelper.PROPERTY_SCHEDULE_SUSPENDED, Boolean.TRUE);
} else {
mvm.remove(ResourceHelper.PROPERTY_SCHEDULE_SUSPENDED);
}
resolver.commit();
}
if (flag) {
this.stopScheduledJob(info);
} else {
this.startScheduledJob(info);
}
} catch (final PersistenceException pe) {
// we ignore the exception if removing fails
ignoreException(pe);
} finally {
resolver.close();
}
}
Aggregations