use of org.apache.sling.api.resource.ResourceResolver in project sling by apache.
the class CheckTopologyTask method reassignJobsFromStoppedInstances.
/**
* Reassign jobs from stopped instance.
*/
private void reassignJobsFromStoppedInstances() {
if (caps.isLeader() && caps.isActive()) {
this.logger.debug("Checking for stopped instances...");
final ResourceResolver resolver = this.configuration.createResourceResolver();
if (resolver != null) {
try {
final Resource jobsRoot = resolver.getResource(this.configuration.getAssginedJobsPath());
this.logger.debug("Got jobs root {}", jobsRoot);
// this resource should exist, but we check anyway
if (jobsRoot != null) {
final Iterator<Resource> instanceIter = jobsRoot.listChildren();
while (caps.isActive() && instanceIter.hasNext()) {
final Resource instanceResource = instanceIter.next();
final String instanceId = instanceResource.getName();
if (!caps.isActive(instanceId)) {
logger.debug("Found stopped instance {}", instanceId);
assignJobs(instanceResource, true);
}
}
}
} finally {
resolver.close();
}
}
}
}
use of org.apache.sling.api.resource.ResourceResolver in project sling by apache.
the class UpgradeTask method upgradeBridgedJobs.
/**
* Upgrade bridged jobs.
* In previous versions, bridged jobs were stored under a special topic.
* This has changed, the jobs are now stored with their real topic.
*/
private void upgradeBridgedJobs() {
final String path = configuration.getLocalJobsPath() + "/slingevent:eventadmin";
final ResourceResolver resolver = configuration.createResourceResolver();
if (resolver != null) {
try {
final Resource rootResource = resolver.getResource(path);
if (rootResource != null) {
upgradeBridgedJobs(rootResource);
}
if (caps.isLeader()) {
final Resource unassignedRoot = resolver.getResource(configuration.getUnassignedJobsPath() + "/slingevent:eventadmin");
if (unassignedRoot != null) {
upgradeBridgedJobs(unassignedRoot);
}
}
} finally {
resolver.close();
}
}
}
use of org.apache.sling.api.resource.ResourceResolver in project sling by apache.
the class UpgradeTask method processJobsFromPreviousVersions.
/**
* Handle jobs from previous versions (<= 3.1.4) by moving them to the unassigned area
*/
private void processJobsFromPreviousVersions() {
final ResourceResolver resolver = configuration.createResourceResolver();
if (resolver != null) {
try {
this.processJobsFromPreviousVersions(resolver.getResource(configuration.getPreviousVersionAnonPath()));
this.processJobsFromPreviousVersions(resolver.getResource(configuration.getPreviousVersionIdentifiedPath()));
} catch (final PersistenceException pe) {
this.logger.warn("Problems moving jobs from previous version.", pe);
} finally {
resolver.close();
}
}
}
use of org.apache.sling.api.resource.ResourceResolver 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.api.resource.ResourceResolver in project sling by apache.
the class DelegateRequestTest method setup.
@Before
public void setup() throws LoginException, PersistenceException {
ResourceResolver adminResolver = null;
try {
adminResolver = rrFactory.getAdministrativeResourceResolver(null);
Map<String, Object> properties = new HashMap<String, Object>();
properties.put("text", "baseTESTValue");
properties.put("other", "baseOther");
properties.put(SlingConstants.NAMESPACE_PREFIX + ":" + SlingConstants.PROPERTY_RESOURCE_TYPE, "sling/delegate/base");
ResourceUtil.getOrCreateResource(adminResolver, baseComponentPath, properties, null, false);
properties.clear();
properties.put("text", "extendedTESTValue");
properties.put("other", "extendedOther");
properties.put(SlingConstants.NAMESPACE_PREFIX + ":" + SlingConstants.PROPERTY_RESOURCE_TYPE, "sling/delegate/extended");
ResourceUtil.getOrCreateResource(adminResolver, extendedComponentPath, properties, null, false);
properties.clear();
properties.put(SlingConstants.NAMESPACE_PREFIX + ":" + SlingConstants.PROPERTY_RESOURCE_SUPER_TYPE, "sling/delegate/base");
ResourceUtil.getOrCreateResource(adminResolver, "/apps/sling/delegate/extended", properties, null, false);
properties.clear();
adminResolver.commit();
} finally {
if (adminResolver != null && adminResolver.isLive()) {
adminResolver.close();
}
}
}
Aggregations