jkevan commented on code in PR #432:
URL: https://github.com/apache/unomi/pull/432#discussion_r888697267


##########
tools/shell-commands/src/main/java/org/apache/unomi/shell/services/internal/UnomiManagementServiceImpl.java:
##########
@@ -31,13 +34,19 @@
  */
 public class UnomiManagementServiceImpl implements UnomiManagementService {
 
+    private static final Logger logger = 
LoggerFactory.getLogger(UnomiManagementServiceImpl.class);
+
     private BundleContext bundleContext;
+    private FeaturesService featuresService;
     private List<String> bundleSymbolicNames;
     private List<String> reversedBundleSymbolicNames;
 
+    private static final String CDP_GRAPHQL_FEATURE = "cdp-graphql-feature";

Review Comment:
   Seem's not used anymore ?



##########
lifecycle-watcher/src/main/java/org/apache/unomi/lifecycle/BundleWatcherImpl.java:
##########
@@ -223,50 +266,93 @@ private void displayLogsForInactiveServices() {
         });
     }
 
+    private void fillFeaturesToInstall() {
+        String installGraphQLFeature = 
bundleContext.getProperty("org.apache.unomi.graphql.feature.activated");
+        boolean graphQLToInstall = 
StringUtils.isNotBlank(installGraphQLFeature) && 
installGraphQLFeature.equals("true");
+        if (graphQLToInstall) {
+            featuresToInstall.add(CDP_GRAPHQL_FEATURE);
+            
requiredBundlesFromFeatures.put("org.apache.unomi.cdp-graphql-api-impl", false);
+            
requiredBundlesFromFeatures.put("org.apache.unomi.graphql-playground", false);
+        }

Review Comment:
   Could we try to generify this using Service property and properties ?
   If not possible it's ok to keep it in the code directly, but may be found a 
name more meainingful that it's related to GraphQL feature.



##########
lifecycle-watcher/src/main/java/org/apache/unomi/lifecycle/BundleWatcherImpl.java:
##########
@@ -223,50 +266,93 @@ private void displayLogsForInactiveServices() {
         });
     }
 
+    private void fillFeaturesToInstall() {
+        String installGraphQLFeature = 
bundleContext.getProperty("org.apache.unomi.graphql.feature.activated");
+        boolean graphQLToInstall = 
StringUtils.isNotBlank(installGraphQLFeature) && 
installGraphQLFeature.equals("true");
+        if (graphQLToInstall) {
+            featuresToInstall.add(CDP_GRAPHQL_FEATURE);
+            
requiredBundlesFromFeatures.put("org.apache.unomi.cdp-graphql-api-impl", false);
+            
requiredBundlesFromFeatures.put("org.apache.unomi.graphql-playground", false);
+        }
+    }
+
+    public boolean shouldInstallAdditionalFeatures() {
+        return !featuresToInstall.isEmpty();
+    }

Review Comment:
   To be removed, this check need to be inside the: isStartupComplete directly.



##########
tools/shell-commands/src/main/resources/OSGI-INF/blueprint/blueprint.xml:
##########
@@ -20,12 +20,14 @@
            xmlns="http://www.osgi.org/xmlns/blueprint/v1.0.0";
            xsi:schemaLocation="http://www.osgi.org/xmlns/blueprint/v1.0.0 
http://www.osgi.org/xmlns/blueprint/v1.0.0/blueprint.xsd";>
 
+    <reference id="featuresService" 
interface="org.apache.karaf.features.FeaturesService"/>
+

Review Comment:
   can be cleaned, removed.



##########
tools/shell-commands/src/main/java/org/apache/unomi/shell/services/internal/UnomiManagementServiceImpl.java:
##########
@@ -31,13 +34,19 @@
  */
 public class UnomiManagementServiceImpl implements UnomiManagementService {
 
+    private static final Logger logger = 
LoggerFactory.getLogger(UnomiManagementServiceImpl.class);
+
     private BundleContext bundleContext;
+    private FeaturesService featuresService;

Review Comment:
   Seem's not used anymore ?



##########
tools/shell-commands/src/main/resources/OSGI-INF/blueprint/blueprint.xml:
##########
@@ -59,13 +61,14 @@
                 <value>org.apache.unomi.router-rest</value>
                 <value>org.apache.unomi.shell-dev-commands</value>
                 <value>org.apache.unomi.web-tracker-wab</value>
-                <value>org.apache.unomi.cdp-graphql-api-impl</value>
                 <value>org.apache.unomi.groovy-actions-services</value>
                 <value>org.apache.unomi.groovy-actions-rest</value>
             </list>
         </property>
         <property name="bundleContext" ref="blueprintBundleContext"/>
+        <property name="featuresService" ref="featuresService"/>

Review Comment:
   can be cleaned, removed.



##########
lifecycle-watcher/src/main/java/org/apache/unomi/lifecycle/BundleWatcherImpl.java:
##########
@@ -223,50 +266,93 @@ private void displayLogsForInactiveServices() {
         });
     }
 
+    private void fillFeaturesToInstall() {
+        String installGraphQLFeature = 
bundleContext.getProperty("org.apache.unomi.graphql.feature.activated");
+        boolean graphQLToInstall = 
StringUtils.isNotBlank(installGraphQLFeature) && 
installGraphQLFeature.equals("true");
+        if (graphQLToInstall) {
+            featuresToInstall.add(CDP_GRAPHQL_FEATURE);
+            
requiredBundlesFromFeatures.put("org.apache.unomi.cdp-graphql-api-impl", false);
+            
requiredBundlesFromFeatures.put("org.apache.unomi.graphql-playground", false);
+        }
+    }
+
+    public boolean shouldInstallAdditionalFeatures() {
+        return !featuresToInstall.isEmpty();
+    }
+
+    private void installFeatures() {
+        List<String> installedFeatures = new ArrayList<>();
+        featuresToInstall.forEach(value -> {
+            try {
+                long featureStartupTime = System.currentTimeMillis();
+                if 
(!featuresService.isInstalled(featuresService.getFeature(value))) {
+                    System.out.println("Installing feature " + value);
+                    featuresService.installFeature(value, 
EnumSet.of(FeaturesService.Option.NoAutoRefreshManagedBundles,
+                            
FeaturesService.Option.NoAutoRefreshUnmanagedBundles, 
FeaturesService.Option.NoAutoRefreshBundles));
+                    logger.info("Feature {} successfully installed in {} ms", 
value, System.currentTimeMillis() - featureStartupTime);
+                }
+                installedFeatures.add(value);
+            } catch (Exception e) {
+                logger.error("Error when installing {} feature", value, e);
+            }
+        });
+        installedFeatures.forEach(value -> featuresToInstall.remove(value));
+    }
+
+    private void startScheduler() {
+        if (scheduledFuture == null || scheduledFuture.isCancelled()) {
+            TimerTask task = new TimerTask() {
+                @Override
+                public void run() {
+                    displayLogsForInactiveBundles(requiredBundles);
+                    displayLogsForInactiveBundles(requiredBundlesFromFeatures);
+                    displayLogsForInactiveServices();
+                    checkStartupComplete();
+                }
+            };
+            scheduledFuture = scheduler
+                    .scheduleWithFixedDelay(task, 
checkStartupStateRefreshInterval, checkStartupStateRefreshInterval, 
TimeUnit.SECONDS);
+        }
+    }
+
     private void checkStartupComplete() {
         if (!isStartupComplete()) {
-            if (scheduledFuture == null || scheduledFuture.isCancelled()) {
-                TimerTask task = new TimerTask() {
-                    @Override
-                    public void run() {
-                        displayLogsForInactiveBundles();
-                        displayLogsForInactiveServices();
-                        checkStartupComplete();
-                    }
-                };
-                scheduledFuture = scheduler
-                        .scheduleWithFixedDelay(task, 
checkStartupStateRefreshInterval, checkStartupStateRefreshInterval, 
TimeUnit.SECONDS);
-            }
+            startScheduler();
             return;
         }
         if (scheduledFuture != null) {
             scheduledFuture.cancel(true);
             scheduledFuture = null;
         }
+        if (shouldInstallAdditionalFeatures() && !installingFeatureStarted) {
+            installingFeatureStarted = true;
+            installFeatures();
+            checkStartupComplete();
+            return;
+        }

Review Comment:
   Should this be checked by the schedule task instead ?



##########
lifecycle-watcher/src/main/java/org/apache/unomi/lifecycle/BundleWatcherImpl.java:
##########
@@ -142,25 +172,38 @@ public void checkExistingBundles() {
         checkStartupComplete();
     }
 
+    private void checkInBundlesList(Bundle bundle, Map<String, Boolean> 
bundles) {
+        if (bundle.getSymbolicName().startsWith("org.apache.unomi") && 
bundles.containsKey(bundle.getSymbolicName())) {
+            if (bundle.getState() == Bundle.ACTIVE) {
+                bundles.put(bundle.getSymbolicName(), true);
+            } else {
+                bundles.put(bundle.getSymbolicName(), false);
+            }
+        }
+    }
+
+    private void managedBundleEvent(Bundle bundle, Map<String, Boolean> 
bundles, Boolean start) {
+        if (bundle.getSymbolicName().startsWith("org.apache.unomi") && 
bundles.containsKey(bundle.getSymbolicName())) {
+            logger.info("Bundle {} was {}.", bundle.getSymbolicName(), start ? 
"started" : "stopped");
+            bundles.put(bundle.getSymbolicName(), start);
+            checkStartupComplete();

Review Comment:
   Careful here, it seem's this check on "checkStartupComplete" was only done 
in during bundle start, should we wrap it with if(start) ?



##########
tools/shell-commands/pom.xml:
##########
@@ -78,6 +79,12 @@
             <version>4.11</version>
             <scope>test</scope>
         </dependency>
+        <dependency>
+            <groupId>org.apache.karaf.features</groupId>
+            <artifactId>org.apache.karaf.features.core</artifactId>
+            <version>${version.karaf}</version>
+            <scope>provided</scope>
+        </dependency>

Review Comment:
   Not sure it is still necessary here if removed from 
tools/shell-commands/src/main/java/org/apache/unomi/shell/services/internal/UnomiManagementServiceImpl.java



##########
tools/shell-commands/pom.xml:
##########
@@ -88,6 +95,11 @@
                 <configuration>
                     <instructions>
                         <Karaf-Commands>*</Karaf-Commands>
+                        <Import-Package>
+                            
org.apache.karaf.util.tracker.annotation;resolution:=optional,
+                            
org.eclipse.osgi.service.resolver;resolution:=optional,
+                            *
+                        </Import-Package>

Review Comment:
   Same could be cleaned.



##########
lifecycle-watcher/src/main/java/org/apache/unomi/lifecycle/BundleWatcherImpl.java:
##########
@@ -223,50 +266,93 @@ private void displayLogsForInactiveServices() {
         });
     }
 
+    private void fillFeaturesToInstall() {
+        String installGraphQLFeature = 
bundleContext.getProperty("org.apache.unomi.graphql.feature.activated");
+        boolean graphQLToInstall = 
StringUtils.isNotBlank(installGraphQLFeature) && 
installGraphQLFeature.equals("true");
+        if (graphQLToInstall) {
+            featuresToInstall.add(CDP_GRAPHQL_FEATURE);
+            
requiredBundlesFromFeatures.put("org.apache.unomi.cdp-graphql-api-impl", false);
+            
requiredBundlesFromFeatures.put("org.apache.unomi.graphql-playground", false);
+        }
+    }
+
+    public boolean shouldInstallAdditionalFeatures() {
+        return !featuresToInstall.isEmpty();
+    }
+
+    private void installFeatures() {
+        List<String> installedFeatures = new ArrayList<>();
+        featuresToInstall.forEach(value -> {
+            try {
+                long featureStartupTime = System.currentTimeMillis();
+                if 
(!featuresService.isInstalled(featuresService.getFeature(value))) {
+                    System.out.println("Installing feature " + value);
+                    featuresService.installFeature(value, 
EnumSet.of(FeaturesService.Option.NoAutoRefreshManagedBundles,
+                            
FeaturesService.Option.NoAutoRefreshUnmanagedBundles, 
FeaturesService.Option.NoAutoRefreshBundles));
+                    logger.info("Feature {} successfully installed in {} ms", 
value, System.currentTimeMillis() - featureStartupTime);
+                }
+                installedFeatures.add(value);
+            } catch (Exception e) {
+                logger.error("Error when installing {} feature", value, e);
+            }
+        });
+        installedFeatures.forEach(value -> featuresToInstall.remove(value));
+    }
+
+    private void startScheduler() {
+        if (scheduledFuture == null || scheduledFuture.isCancelled()) {
+            TimerTask task = new TimerTask() {
+                @Override
+                public void run() {
+                    displayLogsForInactiveBundles(requiredBundles);
+                    displayLogsForInactiveBundles(requiredBundlesFromFeatures);
+                    displayLogsForInactiveServices();
+                    checkStartupComplete();
+                }
+            };
+            scheduledFuture = scheduler
+                    .scheduleWithFixedDelay(task, 
checkStartupStateRefreshInterval, checkStartupStateRefreshInterval, 
TimeUnit.SECONDS);
+        }
+    }
+
     private void checkStartupComplete() {
         if (!isStartupComplete()) {
-            if (scheduledFuture == null || scheduledFuture.isCancelled()) {
-                TimerTask task = new TimerTask() {
-                    @Override
-                    public void run() {
-                        displayLogsForInactiveBundles();
-                        displayLogsForInactiveServices();
-                        checkStartupComplete();
-                    }
-                };
-                scheduledFuture = scheduler
-                        .scheduleWithFixedDelay(task, 
checkStartupStateRefreshInterval, checkStartupStateRefreshInterval, 
TimeUnit.SECONDS);
-            }
+            startScheduler();
             return;
         }
         if (scheduledFuture != null) {
             scheduledFuture.cancel(true);
             scheduledFuture = null;
         }
+        if (shouldInstallAdditionalFeatures() && !installingFeatureStarted) {
+            installingFeatureStarted = true;
+            installFeatures();
+            checkStartupComplete();
+            return;
+        }
+        if (!allAdditionalBundleStarted()) {
+            startScheduler();
+            return;
+        }

Review Comment:
   This is a bit strange to have this check here since it is already done few 
lines before, could we update the first check instead of adding an other.
   I am a bit scared to introduce scenario with endless scheduled task or task 
being canceled without the installation really finished.



##########
lifecycle-watcher/src/main/java/org/apache/unomi/lifecycle/BundleWatcherImpl.java:
##########
@@ -223,50 +266,93 @@ private void displayLogsForInactiveServices() {
         });
     }
 
+    private void fillFeaturesToInstall() {
+        String installGraphQLFeature = 
bundleContext.getProperty("org.apache.unomi.graphql.feature.activated");
+        boolean graphQLToInstall = 
StringUtils.isNotBlank(installGraphQLFeature) && 
installGraphQLFeature.equals("true");
+        if (graphQLToInstall) {
+            featuresToInstall.add(CDP_GRAPHQL_FEATURE);
+            
requiredBundlesFromFeatures.put("org.apache.unomi.cdp-graphql-api-impl", false);
+            
requiredBundlesFromFeatures.put("org.apache.unomi.graphql-playground", false);
+        }
+    }
+
+    public boolean shouldInstallAdditionalFeatures() {
+        return !featuresToInstall.isEmpty();
+    }
+
+    private void installFeatures() {
+        List<String> installedFeatures = new ArrayList<>();
+        featuresToInstall.forEach(value -> {
+            try {
+                long featureStartupTime = System.currentTimeMillis();
+                if 
(!featuresService.isInstalled(featuresService.getFeature(value))) {
+                    System.out.println("Installing feature " + value);
+                    featuresService.installFeature(value, 
EnumSet.of(FeaturesService.Option.NoAutoRefreshManagedBundles,
+                            
FeaturesService.Option.NoAutoRefreshUnmanagedBundles, 
FeaturesService.Option.NoAutoRefreshBundles));
+                    logger.info("Feature {} successfully installed in {} ms", 
value, System.currentTimeMillis() - featureStartupTime);
+                }
+                installedFeatures.add(value);
+            } catch (Exception e) {
+                logger.error("Error when installing {} feature", value, e);
+            }
+        });
+        installedFeatures.forEach(value -> featuresToInstall.remove(value));
+    }
+
+    private void startScheduler() {
+        if (scheduledFuture == null || scheduledFuture.isCancelled()) {
+            TimerTask task = new TimerTask() {
+                @Override
+                public void run() {
+                    displayLogsForInactiveBundles(requiredBundles);
+                    displayLogsForInactiveBundles(requiredBundlesFromFeatures);
+                    displayLogsForInactiveServices();
+                    checkStartupComplete();
+                }
+            };
+            scheduledFuture = scheduler
+                    .scheduleWithFixedDelay(task, 
checkStartupStateRefreshInterval, checkStartupStateRefreshInterval, 
TimeUnit.SECONDS);
+        }
+    }
+
     private void checkStartupComplete() {
         if (!isStartupComplete()) {
-            if (scheduledFuture == null || scheduledFuture.isCancelled()) {
-                TimerTask task = new TimerTask() {
-                    @Override
-                    public void run() {
-                        displayLogsForInactiveBundles();
-                        displayLogsForInactiveServices();
-                        checkStartupComplete();
-                    }
-                };
-                scheduledFuture = scheduler
-                        .scheduleWithFixedDelay(task, 
checkStartupStateRefreshInterval, checkStartupStateRefreshInterval, 
TimeUnit.SECONDS);
-            }
+            startScheduler();
             return;
         }
         if (scheduledFuture != null) {
             scheduledFuture.cancel(true);
             scheduledFuture = null;
         }
+        if (shouldInstallAdditionalFeatures() && !installingFeatureStarted) {

Review Comment:
   We should add this new check inside the isStartupComplete directly



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]

Reply via email to