use of org.apache.sling.event.impl.jobs.config.QueueConfigurationManager in project sling by apache.
the class UpgradeTask method upgradeBridgedJobs.
/**
* Upgrade bridged jobs
* @param rootResource The root resource (topic resource)
*/
private void upgradeBridgedJobs(final Resource topicResource) {
final String topicName = topicResource.getName().replace('.', '/');
final QueueConfigurationManager qcm = configuration.getQueueConfigurationManager();
if (qcm == null) {
return;
}
final QueueInfo info = qcm.getQueueInfo(topicName);
JobTopicTraverser.traverse(logger, topicResource, new JobTopicTraverser.ResourceCallback() {
@Override
public boolean handle(final Resource rsrc) {
try {
final ValueMap vm = ResourceHelper.getValueMap(rsrc);
final String targetId = caps.detectTarget(topicName, vm, info);
final Map<String, Object> props = new HashMap<>(vm);
final String newPath;
if (targetId != null) {
newPath = configuration.getAssginedJobsPath() + '/' + targetId + '/' + topicResource.getName() + rsrc.getPath().substring(topicResource.getPath().length());
props.put(Job.PROPERTY_JOB_QUEUE_NAME, info.queueName);
props.put(Job.PROPERTY_JOB_TARGET_INSTANCE, targetId);
} else {
newPath = configuration.getUnassignedJobsPath() + '/' + topicResource.getName() + rsrc.getPath().substring(topicResource.getPath().length());
props.remove(Job.PROPERTY_JOB_QUEUE_NAME);
props.remove(Job.PROPERTY_JOB_TARGET_INSTANCE);
}
props.remove(Job.PROPERTY_JOB_STARTED_TIME);
try {
ResourceHelper.getOrCreateResource(topicResource.getResourceResolver(), newPath, props);
topicResource.getResourceResolver().delete(rsrc);
topicResource.getResourceResolver().commit();
} catch (final PersistenceException pe) {
logger.warn("Unable to move job from previous version " + rsrc.getPath(), pe);
topicResource.getResourceResolver().refresh();
topicResource.getResourceResolver().revert();
}
} catch (final InstantiationException ie) {
logger.warn("Unable to move job from previous version " + rsrc.getPath(), ie);
topicResource.getResourceResolver().refresh();
topicResource.getResourceResolver().revert();
}
return caps.isActive();
}
});
}
use of org.apache.sling.event.impl.jobs.config.QueueConfigurationManager in project sling by apache.
the class CheckTopologyTask method assignJobs.
/**
* Try to assign all jobs from the jobs root.
* The jobs are stored by topic
* @param jobsRoot The root of the jobs
* @param unassign Whether to unassign the job if no instance is found.
*/
private void assignJobs(final Resource jobsRoot, final boolean unassign) {
final ResourceResolver resolver = jobsRoot.getResourceResolver();
final Iterator<Resource> topicIter = jobsRoot.listChildren();
while (caps.isActive() && topicIter.hasNext()) {
final Resource topicResource = topicIter.next();
final String topicName = topicResource.getName().replace('.', '/');
logger.debug("Found topic {}", topicName);
// first check if there is an instance for these topics
final List<InstanceDescription> potentialTargets = caps.getPotentialTargets(topicName);
if (potentialTargets != null && potentialTargets.size() > 0) {
final QueueConfigurationManager qcm = this.configuration.getQueueConfigurationManager();
if (qcm == null) {
break;
}
final QueueInfo info = qcm.getQueueInfo(topicName);
logger.debug("Found queue {} for {}", info.queueConfiguration, topicName);
JobTopicTraverser.traverse(this.logger, topicResource, new JobTopicTraverser.ResourceCallback() {
@Override
public boolean handle(final Resource rsrc) {
try {
final ValueMap vm = ResourceHelper.getValueMap(rsrc);
final String targetId = caps.detectTarget(topicName, vm, info);
if (targetId != null) {
final String newPath = configuration.getAssginedJobsPath() + '/' + targetId + '/' + topicResource.getName() + rsrc.getPath().substring(topicResource.getPath().length());
final Map<String, Object> props = new HashMap<>(vm);
props.put(Job.PROPERTY_JOB_QUEUE_NAME, info.queueName);
props.put(Job.PROPERTY_JOB_TARGET_INSTANCE, targetId);
props.remove(Job.PROPERTY_JOB_STARTED_TIME);
try {
ResourceHelper.getOrCreateResource(resolver, newPath, props);
resolver.delete(rsrc);
resolver.commit();
final String jobId = vm.get(ResourceHelper.PROPERTY_JOB_ID, String.class);
configuration.getAuditLogger().debug("REASSIGN OK {} : {}", targetId, jobId);
} catch (final PersistenceException pe) {
logger.warn("Unable to move unassigned job from " + rsrc.getPath() + " to " + newPath, pe);
resolver.refresh();
resolver.revert();
}
}
} catch (final InstantiationException ie) {
// something happened with the resource in the meantime
logger.warn("Unable to move unassigned job from " + rsrc.getPath(), ie);
resolver.refresh();
resolver.revert();
}
return caps.isActive();
}
});
}
// now unassign if there are still jobs
if (caps.isActive() && unassign) {
// we have to move everything to the unassigned area
JobTopicTraverser.traverse(this.logger, topicResource, new JobTopicTraverser.ResourceCallback() {
@Override
public boolean handle(final Resource rsrc) {
try {
final ValueMap vm = ResourceHelper.getValueMap(rsrc);
final String newPath = configuration.getUnassignedJobsPath() + '/' + topicResource.getName() + rsrc.getPath().substring(topicResource.getPath().length());
final Map<String, Object> props = new HashMap<>(vm);
props.remove(Job.PROPERTY_JOB_QUEUE_NAME);
props.remove(Job.PROPERTY_JOB_TARGET_INSTANCE);
props.remove(Job.PROPERTY_JOB_STARTED_TIME);
try {
ResourceHelper.getOrCreateResource(resolver, newPath, props);
resolver.delete(rsrc);
resolver.commit();
final String jobId = vm.get(ResourceHelper.PROPERTY_JOB_ID, String.class);
configuration.getAuditLogger().debug("REUNASSIGN OK : {}", jobId);
} catch (final PersistenceException pe) {
logger.warn("Unable to unassigned job from " + rsrc.getPath() + " to " + newPath, pe);
resolver.refresh();
resolver.revert();
}
} catch (final InstantiationException ie) {
// something happened with the resource in the meantime
logger.warn("Unable to unassigned job from " + rsrc.getPath(), ie);
resolver.refresh();
resolver.revert();
}
return caps.isActive();
}
});
}
}
}
use of org.apache.sling.event.impl.jobs.config.QueueConfigurationManager in project sling by apache.
the class UpgradeTask method moveJobFromPreviousVersion.
/**
* Move a single job
*/
private void moveJobFromPreviousVersion(final Resource jobResource) throws PersistenceException {
final ResourceResolver resolver = jobResource.getResourceResolver();
try {
final ValueMap vm = ResourceHelper.getValueMap(jobResource);
// check for binary properties
Map<String, Object> binaryProperties = new HashMap<>();
final ObjectInputStream ois = vm.get("slingevent:properties", ObjectInputStream.class);
if (ois != null) {
try {
int length = ois.readInt();
for (int i = 0; i < length; i++) {
final String key = (String) ois.readObject();
final Object value = ois.readObject();
binaryProperties.put(key, value);
}
} catch (final ClassNotFoundException cnfe) {
throw new PersistenceException("Class not found.", cnfe);
} catch (final java.io.InvalidClassException ice) {
throw new PersistenceException("Invalid class.", ice);
} catch (final IOException ioe) {
throw new PersistenceException("Unable to deserialize job properties.", ioe);
} finally {
try {
ois.close();
} catch (final IOException ioe) {
throw new PersistenceException("Unable to deserialize job properties.", ioe);
}
}
}
final Map<String, Object> properties = ResourceHelper.cloneValueMap(vm);
final String topic = (String) properties.remove("slingevent:topic");
properties.put(ResourceHelper.PROPERTY_JOB_TOPIC, topic);
properties.remove(Job.PROPERTY_JOB_QUEUE_NAME);
properties.remove(Job.PROPERTY_JOB_TARGET_INSTANCE);
// and binary properties
properties.putAll(binaryProperties);
properties.remove("slingevent:properties");
if (!properties.containsKey(Job.PROPERTY_JOB_RETRIES)) {
// we put a dummy value here; this gets updated by the queue
properties.put(Job.PROPERTY_JOB_RETRIES, 10);
}
if (!properties.containsKey(Job.PROPERTY_JOB_RETRY_COUNT)) {
properties.put(Job.PROPERTY_JOB_RETRY_COUNT, 0);
}
final List<InstanceDescription> potentialTargets = caps.getPotentialTargets(topic);
String targetId = null;
if (potentialTargets != null && potentialTargets.size() > 0) {
final QueueConfigurationManager qcm = configuration.getQueueConfigurationManager();
if (qcm == null) {
resolver.revert();
return;
}
final QueueInfo info = qcm.getQueueInfo(topic);
logger.debug("Found queue {} for {}", info.queueConfiguration, topic);
targetId = caps.detectTarget(topic, vm, info);
if (targetId != null) {
properties.put(Job.PROPERTY_JOB_QUEUE_NAME, info.queueName);
properties.put(Job.PROPERTY_JOB_TARGET_INSTANCE, targetId);
properties.put(Job.PROPERTY_JOB_RETRIES, info.queueConfiguration.getMaxRetries());
}
}
properties.put(Job.PROPERTY_JOB_CREATED_INSTANCE, "old:" + Environment.APPLICATION_ID);
properties.put(ResourceResolver.PROPERTY_RESOURCE_TYPE, ResourceHelper.RESOURCE_TYPE_JOB);
final String jobId = configuration.getUniqueId(topic);
properties.put(ResourceHelper.PROPERTY_JOB_ID, jobId);
properties.remove(Job.PROPERTY_JOB_STARTED_TIME);
final String newPath = configuration.getUniquePath(targetId, topic, jobId, vm);
this.logger.debug("Moving 'old' job from {} to {}", jobResource.getPath(), newPath);
ResourceHelper.getOrCreateResource(resolver, newPath, properties);
resolver.delete(jobResource);
resolver.commit();
} catch (final InstantiationException ie) {
throw new PersistenceException("Exception while reading reasource: " + ie.getMessage(), ie.getCause());
}
}
use of org.apache.sling.event.impl.jobs.config.QueueConfigurationManager in project sling by apache.
the class CheckTopologyTask method reassignStaleJobs.
/**
* Reassign stale jobs from this instance
*/
private void reassignStaleJobs() {
if (caps.isActive()) {
this.logger.debug("Checking for stale jobs...");
final ResourceResolver resolver = this.configuration.createResourceResolver();
if (resolver != null) {
try {
final Resource jobsRoot = resolver.getResource(this.configuration.getLocalJobsPath());
// this resource should exist, but we check anyway
if (jobsRoot != null) {
final Iterator<Resource> topicIter = jobsRoot.listChildren();
while (caps.isActive() && topicIter.hasNext()) {
final Resource topicResource = topicIter.next();
final String topicName = topicResource.getName().replace('.', '/');
this.logger.debug("Checking topic {}...", topicName);
final List<InstanceDescription> potentialTargets = caps.getPotentialTargets(topicName);
boolean reassign = true;
for (final InstanceDescription desc : potentialTargets) {
if (desc.isLocal()) {
reassign = false;
break;
}
}
if (reassign) {
final QueueConfigurationManager qcm = this.configuration.getQueueConfigurationManager();
if (qcm == null) {
break;
}
final QueueInfo info = qcm.getQueueInfo(topicName);
logger.info("Start reassigning stale jobs");
JobTopicTraverser.traverse(this.logger, topicResource, new JobTopicTraverser.ResourceCallback() {
@Override
public boolean handle(final Resource rsrc) {
try {
final ValueMap vm = ResourceHelper.getValueMap(rsrc);
final String targetId = caps.detectTarget(topicName, vm, info);
final Map<String, Object> props = new HashMap<>(vm);
props.remove(Job.PROPERTY_JOB_STARTED_TIME);
final String newPath;
if (targetId != null) {
newPath = configuration.getAssginedJobsPath() + '/' + targetId + '/' + topicResource.getName() + rsrc.getPath().substring(topicResource.getPath().length());
props.put(Job.PROPERTY_JOB_QUEUE_NAME, info.queueName);
props.put(Job.PROPERTY_JOB_TARGET_INSTANCE, targetId);
} else {
newPath = configuration.getUnassignedJobsPath() + '/' + topicResource.getName() + rsrc.getPath().substring(topicResource.getPath().length());
props.remove(Job.PROPERTY_JOB_QUEUE_NAME);
props.remove(Job.PROPERTY_JOB_TARGET_INSTANCE);
}
try {
ResourceHelper.getOrCreateResource(resolver, newPath, props);
resolver.delete(rsrc);
resolver.commit();
final String jobId = vm.get(ResourceHelper.PROPERTY_JOB_ID, String.class);
if (targetId != null) {
configuration.getAuditLogger().debug("REASSIGN OK {} : {}", targetId, jobId);
} else {
configuration.getAuditLogger().debug("REUNASSIGN OK : {}", jobId);
}
} catch (final PersistenceException pe) {
logger.warn("Unable to move stale job from " + rsrc.getPath() + " to " + newPath, pe);
resolver.refresh();
resolver.revert();
}
} catch (final InstantiationException ie) {
// something happened with the resource in the meantime
logger.warn("Unable to move stale job from " + rsrc.getPath(), ie);
resolver.refresh();
resolver.revert();
}
return caps.isActive();
}
});
}
}
}
} finally {
resolver.close();
}
}
}
}
Aggregations