From 7e00459e9a2996682b6c91129e12fa80878ba1f5 Mon Sep 17 00:00:00 2001 From: lucio lelii Date: Thu, 30 Jun 2022 12:33:55 +0200 Subject: [PATCH] logs added --- .../lifecycle/ApplicationProfileManager.java | 70 +++++++++++-------- .../lifecycle/ContainerProfileManager.java | 57 +++++++++------ 2 files changed, 73 insertions(+), 54 deletions(-) diff --git a/src/main/java/org/gcube/smartgears/handlers/application/lifecycle/ApplicationProfileManager.java b/src/main/java/org/gcube/smartgears/handlers/application/lifecycle/ApplicationProfileManager.java index ec0c9a2..2228dc7 100644 --- a/src/main/java/org/gcube/smartgears/handlers/application/lifecycle/ApplicationProfileManager.java +++ b/src/main/java/org/gcube/smartgears/handlers/application/lifecycle/ApplicationProfileManager.java @@ -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; * * * @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 publishers = ProviderFactory.provider().publishers(); @@ -80,12 +79,12 @@ public class ApplicationProfileManager extends ApplicationLifecycleHandler { private void activated(){ - + 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); + } } }); @@ -206,7 +214,7 @@ public class ApplicationProfileManager extends ApplicationLifecycleHandler { context.events().fire(context,changed); } }; - + periodicUpdates = Utils.scheduledServicePool.scheduleAtFixedRate(updateTask, Constants.application_republish_frequency_in_minutes, Constants.application_republish_frequency_in_minutes , TimeUnit.MINUTES); diff --git a/src/main/java/org/gcube/smartgears/handlers/container/lifecycle/ContainerProfileManager.java b/src/main/java/org/gcube/smartgears/handlers/container/lifecycle/ContainerProfileManager.java index a15ba64..3f16126 100644 --- a/src/main/java/org/gcube/smartgears/handlers/container/lifecycle/ContainerProfileManager.java +++ b/src/main/java/org/gcube/smartgears/handlers/container/lifecycle/ContainerProfileManager.java @@ -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 publishers; @@ -83,40 +85,49 @@ public class ContainerProfileManager extends ContainerHandler { publishers = context.configuration().mode()!=Mode.offline? ProviderFactory.provider().publishers(): Collections.emptyList(); - registerObservers(); - schedulePeriodicUpdates(); + registerObservers(); + schedulePeriodicUpdates(); } - + private void registerObservers() { context.events().subscribe(new Object() { @Observes({ activation, part_activation, shutdown, stop, failure }) void onChanged(ContainerLifecycle lc) { - + // since we do not know the observers, they will deal with failures and their consequences // any that comes back will be logged in this event thread context.events().fire(context, changed); } - + @Observes(value = changed, kind = critical) void publishAfterChange(ContainerContext context) { 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) void addTo(String scope) { for (Publisher publisher: publishers) @@ -135,9 +146,9 @@ public class ContainerProfileManager extends ContainerHandler { // changed profile. //TODO: CHECK --- store(profile); } - + } - + @Observes(value = removeFromContext) void removeFrom(String scope) { for (Publisher publisher: publishers) @@ -159,8 +170,8 @@ public class ContainerProfileManager extends ContainerHandler { } }); } - - + + private void schedulePeriodicUpdates() { // register to cancel updates context.events().subscribe(