logs added

This commit is contained in:
Lucio Lelii 2022-06-30 12:33:55 +02:00
parent 1b79f8e813
commit 7e00459e9a
2 changed files with 73 additions and 54 deletions

View File

@ -18,6 +18,7 @@ import org.gcube.common.events.Observes;
import org.gcube.common.events.Observes.Kind;
import org.gcube.smartgears.Constants;
import org.gcube.smartgears.configuration.Mode;
import org.gcube.smartgears.context.Property;
import org.gcube.smartgears.context.application.ApplicationContext;
import org.gcube.smartgears.handlers.application.ApplicationLifecycleEvent;
import org.gcube.smartgears.handlers.application.ApplicationLifecycleHandler;
@ -46,8 +47,6 @@ import org.slf4j.LoggerFactory;
* </ul>
*
* @author Fabio Simeoni
* @see ProfileBuilder
* @see ProfilePublisherImpl
*/
public class ApplicationProfileManager extends ApplicationLifecycleHandler {
@ -55,7 +54,7 @@ public class ApplicationProfileManager extends ApplicationLifecycleHandler {
private ApplicationContext context;
private ScheduledFuture<?> periodicUpdates;
private static final String PUBLISHED_PROP = "published";
private List<Publisher> publishers = ProviderFactory.provider().publishers();
@ -84,8 +83,8 @@ public class ApplicationProfileManager extends ApplicationLifecycleHandler {
publishers = context.container().configuration().mode()!=Mode.offline?
ProviderFactory.provider().publishers():
Collections.emptyList();
registerObservers();
schedulePeriodicUpdates();
registerObservers();
schedulePeriodicUpdates();
}
// helpers
@ -118,19 +117,28 @@ public class ApplicationProfileManager extends ApplicationLifecycleHandler {
//if we've failed before first publication do not try to publish
//(we may well have failed there)
for (Publisher publisher: publishers)
try {
publisher.update(context);
}catch (Exception e) {
log.error("cannot publish {} with publisher type {} (see details)",context.name(), publisher.getClass().getCanonicalName(), e);
// since we've failed no published event is fired and profile
// will not be stored.
// we do it manually to ensure we leave some local trace of the
// changed profile.
//TODO: CHECK --- store(profile);
if (context.properties().contains(PUBLISHED_PROP)) {
log.info("publishing application for the first time");
context.properties().add(new Property(PUBLISHED_PROP, true));
if (context.lifecycle().state() != ApplicationState.failed) {
publishers.forEach(p -> {
try {
p.create(context,
context.container().configuration().authorizationProvider().getContexts());
}catch (Exception e) {
log.error("cannot publish {} for first time with publisher type {} (see details)",context.name(), p.getClass().getCanonicalName(), e);
}
});
}
}
else
publishers.forEach(p -> {
try {
p.update(context);
}catch (Exception e) {
log.error("cannot publish {} with publisher type {} (see details)",context.name(), p.getClass().getCanonicalName(), e);
}
});
}
@ -158,20 +166,20 @@ public class ApplicationProfileManager extends ApplicationLifecycleHandler {
@Observes(value = removeFromContext)
void removeFrom(String scope) {
for (Publisher publisher: publishers)
try {
log.debug("unpublishing application from scope {}", scope);
publisher.remove(context,
Collections.singleton(scope));
}catch (Exception e) {
try {
log.debug("unpublishing application from scope {}", scope);
publisher.remove(context,
Collections.singleton(scope));
}catch (Exception e) {
log.error("cannot remove scope {} with publisher type {} (see details)",scope, publisher.getClass().getCanonicalName(), e);
log.error("cannot remove scope {} with publisher type {} (see details)",scope, publisher.getClass().getCanonicalName(), e);
// since we've failed no published event is fired and profile
// will not be stored.
// we do it manually to ensure we leave some local trace of the
// changed profile.
//TODO: CHECK --- store(profile);
}
// since we've failed no published event is fired and profile
// will not be stored.
// we do it manually to ensure we leave some local trace of the
// changed profile.
//TODO: CHECK --- store(profile);
}
}
});

View File

@ -20,10 +20,12 @@ import java.util.concurrent.ScheduledFuture;
import org.gcube.common.events.Observes;
import org.gcube.smartgears.configuration.Mode;
import org.gcube.smartgears.context.Property;
import org.gcube.smartgears.context.container.ContainerContext;
import org.gcube.smartgears.handlers.container.ContainerHandler;
import org.gcube.smartgears.handlers.container.ContainerLifecycleEvent;
import org.gcube.smartgears.lifecycle.container.ContainerLifecycle;
import org.gcube.smartgears.lifecycle.container.ContainerState;
import org.gcube.smartgears.provider.ProviderFactory;
import org.gcube.smartgears.publishing.Publisher;
import org.gcube.smartgears.utils.Utils;
@ -55,7 +57,7 @@ public class ContainerProfileManager extends ContainerHandler {
private ContainerContext context;
private ScheduledFuture<?> periodicUpdates;
private static final String PUBLISHED_PROP = "published";
private List<Publisher> publishers;
@ -83,8 +85,8 @@ public class ContainerProfileManager extends ContainerHandler {
publishers = context.configuration().mode()!=Mode.offline?
ProviderFactory.provider().publishers():
Collections.emptyList();
registerObservers();
schedulePeriodicUpdates();
registerObservers();
schedulePeriodicUpdates();
}
private void registerObservers() {
@ -102,19 +104,28 @@ public class ContainerProfileManager extends ContainerHandler {
log.info("Publish after profile Change event called");
//if we've failed before first publication do not try to publish
//(we may well have failed there)
for (Publisher publisher: publishers)
try {
publisher.update(context);
}catch (Exception e) {
log.error("cannot publish containar with publisher type {} (see details)", publisher.getClass().getCanonicalName(), e);
// since we've failed no published event is fired and profile
// will not be stored.
// we do it manually to ensure we leave some local trace of the
// changed profile.
//TODO: CHECK --- store(profile);
if (context.properties().contains(PUBLISHED_PROP)) {
context.properties().add(new Property(PUBLISHED_PROP, true));
log.info("publishing container for the first time");
if (context.lifecycle().state() != ContainerState.failed) {
publishers.forEach(p -> {
try {
p.create(context,
context.configuration().authorizationProvider().getContexts());
}catch (Exception e) {
log.error("cannot publish container for first time with publisher type {} (see details)", p.getClass().getCanonicalName(), e);
}
});
}
}
else
publishers.forEach(p -> {
try {
p.update(context);
}catch (Exception e) {
log.error("cannot publish container with publisher type {} (see details)", p.getClass().getCanonicalName(), e);
}
});
}
@Observes(value = addToContext)