logs added
This commit is contained in:
parent
1b79f8e813
commit
7e00459e9a
|
@ -18,6 +18,7 @@ import org.gcube.common.events.Observes;
|
||||||
import org.gcube.common.events.Observes.Kind;
|
import org.gcube.common.events.Observes.Kind;
|
||||||
import org.gcube.smartgears.Constants;
|
import org.gcube.smartgears.Constants;
|
||||||
import org.gcube.smartgears.configuration.Mode;
|
import org.gcube.smartgears.configuration.Mode;
|
||||||
|
import org.gcube.smartgears.context.Property;
|
||||||
import org.gcube.smartgears.context.application.ApplicationContext;
|
import org.gcube.smartgears.context.application.ApplicationContext;
|
||||||
import org.gcube.smartgears.handlers.application.ApplicationLifecycleEvent;
|
import org.gcube.smartgears.handlers.application.ApplicationLifecycleEvent;
|
||||||
import org.gcube.smartgears.handlers.application.ApplicationLifecycleHandler;
|
import org.gcube.smartgears.handlers.application.ApplicationLifecycleHandler;
|
||||||
|
@ -46,8 +47,6 @@ import org.slf4j.LoggerFactory;
|
||||||
* </ul>
|
* </ul>
|
||||||
*
|
*
|
||||||
* @author Fabio Simeoni
|
* @author Fabio Simeoni
|
||||||
* @see ProfileBuilder
|
|
||||||
* @see ProfilePublisherImpl
|
|
||||||
*/
|
*/
|
||||||
public class ApplicationProfileManager extends ApplicationLifecycleHandler {
|
public class ApplicationProfileManager extends ApplicationLifecycleHandler {
|
||||||
|
|
||||||
|
@ -55,7 +54,7 @@ public class ApplicationProfileManager extends ApplicationLifecycleHandler {
|
||||||
|
|
||||||
private ApplicationContext context;
|
private ApplicationContext context;
|
||||||
private ScheduledFuture<?> periodicUpdates;
|
private ScheduledFuture<?> periodicUpdates;
|
||||||
|
private static final String PUBLISHED_PROP = "published";
|
||||||
private List<Publisher> publishers = ProviderFactory.provider().publishers();
|
private List<Publisher> publishers = ProviderFactory.provider().publishers();
|
||||||
|
|
||||||
|
|
||||||
|
@ -80,12 +79,12 @@ public class ApplicationProfileManager extends ApplicationLifecycleHandler {
|
||||||
|
|
||||||
|
|
||||||
private void activated(){
|
private void activated(){
|
||||||
|
|
||||||
publishers = context.container().configuration().mode()!=Mode.offline?
|
publishers = context.container().configuration().mode()!=Mode.offline?
|
||||||
ProviderFactory.provider().publishers():
|
ProviderFactory.provider().publishers():
|
||||||
Collections.emptyList();
|
Collections.emptyList();
|
||||||
registerObservers();
|
registerObservers();
|
||||||
schedulePeriodicUpdates();
|
schedulePeriodicUpdates();
|
||||||
}
|
}
|
||||||
|
|
||||||
// helpers
|
// helpers
|
||||||
|
@ -118,19 +117,28 @@ public class ApplicationProfileManager extends ApplicationLifecycleHandler {
|
||||||
|
|
||||||
//if we've failed before first publication do not try to publish
|
//if we've failed before first publication do not try to publish
|
||||||
//(we may well have failed there)
|
//(we may well have failed there)
|
||||||
for (Publisher publisher: publishers)
|
if (context.properties().contains(PUBLISHED_PROP)) {
|
||||||
try {
|
log.info("publishing application for the first time");
|
||||||
publisher.update(context);
|
context.properties().add(new Property(PUBLISHED_PROP, true));
|
||||||
}catch (Exception e) {
|
if (context.lifecycle().state() != ApplicationState.failed) {
|
||||||
|
publishers.forEach(p -> {
|
||||||
log.error("cannot publish {} with publisher type {} (see details)",context.name(), publisher.getClass().getCanonicalName(), e);
|
try {
|
||||||
|
p.create(context,
|
||||||
// since we've failed no published event is fired and profile
|
context.container().configuration().authorizationProvider().getContexts());
|
||||||
// will not be stored.
|
}catch (Exception e) {
|
||||||
// we do it manually to ensure we leave some local trace of the
|
log.error("cannot publish {} for first time with publisher type {} (see details)",context.name(), p.getClass().getCanonicalName(), e);
|
||||||
// changed profile.
|
}
|
||||||
//TODO: CHECK --- store(profile);
|
});
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
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)
|
@Observes(value = removeFromContext)
|
||||||
void removeFrom(String scope) {
|
void removeFrom(String scope) {
|
||||||
for (Publisher publisher: publishers)
|
for (Publisher publisher: publishers)
|
||||||
try {
|
try {
|
||||||
log.debug("unpublishing application from scope {}", scope);
|
log.debug("unpublishing application from scope {}", scope);
|
||||||
publisher.remove(context,
|
publisher.remove(context,
|
||||||
Collections.singleton(scope));
|
Collections.singleton(scope));
|
||||||
}catch (Exception e) {
|
}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
|
// since we've failed no published event is fired and profile
|
||||||
// will not be stored.
|
// will not be stored.
|
||||||
// we do it manually to ensure we leave some local trace of the
|
// we do it manually to ensure we leave some local trace of the
|
||||||
// changed profile.
|
// changed profile.
|
||||||
//TODO: CHECK --- store(profile);
|
//TODO: CHECK --- store(profile);
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
@ -206,7 +214,7 @@ public class ApplicationProfileManager extends ApplicationLifecycleHandler {
|
||||||
context.events().fire(context,changed);
|
context.events().fire(context,changed);
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
periodicUpdates = Utils.scheduledServicePool.scheduleAtFixedRate(updateTask,
|
periodicUpdates = Utils.scheduledServicePool.scheduleAtFixedRate(updateTask,
|
||||||
Constants.application_republish_frequency_in_minutes,
|
Constants.application_republish_frequency_in_minutes,
|
||||||
Constants.application_republish_frequency_in_minutes , TimeUnit.MINUTES);
|
Constants.application_republish_frequency_in_minutes , TimeUnit.MINUTES);
|
||||||
|
|
|
@ -20,10 +20,12 @@ import java.util.concurrent.ScheduledFuture;
|
||||||
|
|
||||||
import org.gcube.common.events.Observes;
|
import org.gcube.common.events.Observes;
|
||||||
import org.gcube.smartgears.configuration.Mode;
|
import org.gcube.smartgears.configuration.Mode;
|
||||||
|
import org.gcube.smartgears.context.Property;
|
||||||
import org.gcube.smartgears.context.container.ContainerContext;
|
import org.gcube.smartgears.context.container.ContainerContext;
|
||||||
import org.gcube.smartgears.handlers.container.ContainerHandler;
|
import org.gcube.smartgears.handlers.container.ContainerHandler;
|
||||||
import org.gcube.smartgears.handlers.container.ContainerLifecycleEvent;
|
import org.gcube.smartgears.handlers.container.ContainerLifecycleEvent;
|
||||||
import org.gcube.smartgears.lifecycle.container.ContainerLifecycle;
|
import org.gcube.smartgears.lifecycle.container.ContainerLifecycle;
|
||||||
|
import org.gcube.smartgears.lifecycle.container.ContainerState;
|
||||||
import org.gcube.smartgears.provider.ProviderFactory;
|
import org.gcube.smartgears.provider.ProviderFactory;
|
||||||
import org.gcube.smartgears.publishing.Publisher;
|
import org.gcube.smartgears.publishing.Publisher;
|
||||||
import org.gcube.smartgears.utils.Utils;
|
import org.gcube.smartgears.utils.Utils;
|
||||||
|
@ -55,7 +57,7 @@ public class ContainerProfileManager extends ContainerHandler {
|
||||||
|
|
||||||
private ContainerContext context;
|
private ContainerContext context;
|
||||||
private ScheduledFuture<?> periodicUpdates;
|
private ScheduledFuture<?> periodicUpdates;
|
||||||
|
private static final String PUBLISHED_PROP = "published";
|
||||||
private List<Publisher> publishers;
|
private List<Publisher> publishers;
|
||||||
|
|
||||||
|
|
||||||
|
@ -83,40 +85,49 @@ public class ContainerProfileManager extends ContainerHandler {
|
||||||
publishers = context.configuration().mode()!=Mode.offline?
|
publishers = context.configuration().mode()!=Mode.offline?
|
||||||
ProviderFactory.provider().publishers():
|
ProviderFactory.provider().publishers():
|
||||||
Collections.emptyList();
|
Collections.emptyList();
|
||||||
registerObservers();
|
registerObservers();
|
||||||
schedulePeriodicUpdates();
|
schedulePeriodicUpdates();
|
||||||
}
|
}
|
||||||
|
|
||||||
private void registerObservers() {
|
private void registerObservers() {
|
||||||
context.events().subscribe(new Object() {
|
context.events().subscribe(new Object() {
|
||||||
@Observes({ activation, part_activation, shutdown, stop, failure })
|
@Observes({ activation, part_activation, shutdown, stop, failure })
|
||||||
void onChanged(ContainerLifecycle lc) {
|
void onChanged(ContainerLifecycle lc) {
|
||||||
|
|
||||||
// since we do not know the observers, they will deal with failures and their consequences
|
// 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
|
// any that comes back will be logged in this event thread
|
||||||
context.events().fire(context, changed);
|
context.events().fire(context, changed);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Observes(value = changed, kind = critical)
|
@Observes(value = changed, kind = critical)
|
||||||
void publishAfterChange(ContainerContext context) {
|
void publishAfterChange(ContainerContext context) {
|
||||||
log.info("Publish after profile Change event called");
|
log.info("Publish after profile Change event called");
|
||||||
//if we've failed before first publication do not try to publish
|
//if we've failed before first publication do not try to publish
|
||||||
//(we may well have failed there)
|
//(we may well have failed there)
|
||||||
for (Publisher publisher: publishers)
|
if (context.properties().contains(PUBLISHED_PROP)) {
|
||||||
try {
|
context.properties().add(new Property(PUBLISHED_PROP, true));
|
||||||
publisher.update(context);
|
log.info("publishing container for the first time");
|
||||||
}catch (Exception e) {
|
if (context.lifecycle().state() != ContainerState.failed) {
|
||||||
|
publishers.forEach(p -> {
|
||||||
log.error("cannot publish containar with publisher type {} (see details)", publisher.getClass().getCanonicalName(), e);
|
try {
|
||||||
|
p.create(context,
|
||||||
// since we've failed no published event is fired and profile
|
context.configuration().authorizationProvider().getContexts());
|
||||||
// will not be stored.
|
}catch (Exception e) {
|
||||||
// we do it manually to ensure we leave some local trace of the
|
log.error("cannot publish container for first time with publisher type {} (see details)", p.getClass().getCanonicalName(), e);
|
||||||
// changed profile.
|
}
|
||||||
//TODO: CHECK --- store(profile);
|
});
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
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)
|
@Observes(value = addToContext)
|
||||||
void addTo(String scope) {
|
void addTo(String scope) {
|
||||||
for (Publisher publisher: publishers)
|
for (Publisher publisher: publishers)
|
||||||
|
@ -135,9 +146,9 @@ public class ContainerProfileManager extends ContainerHandler {
|
||||||
// changed profile.
|
// changed profile.
|
||||||
//TODO: CHECK --- store(profile);
|
//TODO: CHECK --- store(profile);
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Observes(value = removeFromContext)
|
@Observes(value = removeFromContext)
|
||||||
void removeFrom(String scope) {
|
void removeFrom(String scope) {
|
||||||
for (Publisher publisher: publishers)
|
for (Publisher publisher: publishers)
|
||||||
|
@ -159,8 +170,8 @@ public class ContainerProfileManager extends ContainerHandler {
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
private void schedulePeriodicUpdates() {
|
private void schedulePeriodicUpdates() {
|
||||||
// register to cancel updates
|
// register to cancel updates
|
||||||
context.events().subscribe(
|
context.events().subscribe(
|
||||||
|
|
Loading…
Reference in New Issue