killbill-uncached

Fix jruby initialization to start synchronously during Kill

5/20/2013 7:38:55 PM

Details

diff --git a/beatrix/src/test/java/com/ning/billing/beatrix/integration/osgi/TestJrubyNotificationPlugin.java b/beatrix/src/test/java/com/ning/billing/beatrix/integration/osgi/TestJrubyNotificationPlugin.java
index 5512ccc..cb5f808 100644
--- a/beatrix/src/test/java/com/ning/billing/beatrix/integration/osgi/TestJrubyNotificationPlugin.java
+++ b/beatrix/src/test/java/com/ning/billing/beatrix/integration/osgi/TestJrubyNotificationPlugin.java
@@ -50,9 +50,11 @@ public class TestJrubyNotificationPlugin extends TestOSGIBase {
     @Test(groups = "slow", enabled = true)
     public void testOnEventForAccountCreation() throws Exception {
 
-        final Account account = createAccountWithNonOsgiPaymentMethod(getAccountData(4));
+        // Once we create the account we give the hand to the jruby notification plugin
+        // which will handle the ExtBusEvent and start updating the account, create tag definition and finally create a tag.
+        // We wait for all that to occur and declare victory if we see the TagDefiniton/Tag creation.
         busHandler.pushExpectedEvents(NextEvent.TAG_DEFINITION, NextEvent.TAG);
-        // notification will do a bunch of things and also create a tag
+        final Account account = createAccountWithNonOsgiPaymentMethod(getAccountData(4));
         assertTrue(busHandler.isCompleted(2 * DELAY));
 
         final List<Tag> tags = tagUserApi.getTagsForAccount(account.getId(), callContext);
diff --git a/osgi-bundles/bundles/jruby/src/main/java/com/ning/billing/osgi/bundles/jruby/JRubyActivator.java b/osgi-bundles/bundles/jruby/src/main/java/com/ning/billing/osgi/bundles/jruby/JRubyActivator.java
index e98b1ab..5d7dc0d 100644
--- a/osgi-bundles/bundles/jruby/src/main/java/com/ning/billing/osgi/bundles/jruby/JRubyActivator.java
+++ b/osgi-bundles/bundles/jruby/src/main/java/com/ning/billing/osgi/bundles/jruby/JRubyActivator.java
@@ -100,21 +100,15 @@ public class JRubyActivator extends KillbillActivatorBase {
             logService.log(LogService.LOG_WARNING, tmpDirPath + " is not a directory, the restart mechanism is disabled");
             return;
         }
+        // Start the plugin synchronously and schedule the restart logic
+        doStartPlugin(pluginMain, context, killbillServices);
 
-        final AtomicBoolean firstStart = new AtomicBoolean(true);
         restartFuture = Executors.newSingleThreadScheduledExecutor("jruby-restarter-" + pluginMain)
                                  .scheduleWithFixedDelay(new Runnable() {
             long lastRestartMillis = System.currentTimeMillis();
 
             @Override
             public void run() {
-                if (firstStart.get()) {
-                    // Initial start
-                    logService.log(LogService.LOG_INFO, "Starting JRuby plugin " + rubyConfig.getRubyMainClass());
-                    doStartPlugin(pluginMain, context, killbillServices);
-                    firstStart.set(false);
-                    return;
-                }
 
                 final File restartFile = new File(tmpDirPath + "/" + RESTART_FILE_NAME);
                 if (!restartFile.isFile()) {
@@ -130,7 +124,7 @@ public class JRubyActivator extends KillbillActivatorBase {
                     lastRestartMillis = restartFile.lastModified();
                 }
             }
-        }, 0, JRUBY_PLUGINS_RESTART_DELAY_SECS, TimeUnit.SECONDS);
+        }, JRUBY_PLUGINS_RESTART_DELAY_SECS, JRUBY_PLUGINS_RESTART_DELAY_SECS, TimeUnit.SECONDS);
     }
 
     private PluginRubyConfig retrievePluginRubyConfig(final BundleContext context) {
@@ -151,14 +145,18 @@ public class JRubyActivator extends KillbillActivatorBase {
         }, this.getClass().getClassLoader());
     }
 
-    private void doStartPlugin(final String pluginMain, final BundleContext context, final Map<String, Object> killbillServices) {
+    private void  doStartPlugin(final String pluginMain, final BundleContext context, final Map<String, Object> killbillServices) {
+        logService.log(LogService.LOG_INFO, "Starting JRuby plugin " + pluginMain);
         plugin.instantiatePlugin(killbillServices, pluginMain);
         plugin.startPlugin(context);
+        logService.log(LogService.LOG_INFO, "JRuby plugin " + pluginMain + " started");
     }
 
     private void doStopPlugin(final BundleContext context) {
+        logService.log(LogService.LOG_INFO, "Stopping JRuby plugin " + context.getBundle().getSymbolicName());
         plugin.stopPlugin(context);
         plugin.unInstantiatePlugin();
+        logService.log(LogService.LOG_INFO, "Stopped JRuby plugin " + context.getBundle().getSymbolicName());
     }
 
     // We make the explicit registration in the start method by hand as this would be called too early
diff --git a/osgi-bundles/bundles/jruby/src/main/java/com/ning/billing/osgi/bundles/jruby/JRubyPlugin.java b/osgi-bundles/bundles/jruby/src/main/java/com/ning/billing/osgi/bundles/jruby/JRubyPlugin.java
index d3685eb..050a469 100644
--- a/osgi-bundles/bundles/jruby/src/main/java/com/ning/billing/osgi/bundles/jruby/JRubyPlugin.java
+++ b/osgi-bundles/bundles/jruby/src/main/java/com/ning/billing/osgi/bundles/jruby/JRubyPlugin.java
@@ -97,14 +97,14 @@ public abstract class JRubyPlugin {
         pluginInstance = (RubyObject) container.runScriptlet(pluginMain + ".new(" + KILLBILL_PLUGIN_CLASS_NAME + "," + KILLBILL_SERVICES + ")");
     }
 
-    public void startPlugin(final BundleContext context) {
+    public synchronized void startPlugin(final BundleContext context) {
         checkPluginIsStopped();
         pluginInstance.callMethod(START_PLUGIN_RUBY_METHOD_NAME);
         checkPluginIsRunning();
         registerHttpServlet();
     }
 
-    public void stopPlugin(final BundleContext context) {
+    public synchronized void stopPlugin(final BundleContext context) {
         checkPluginIsRunning();
         unregisterHttpServlet();
         pluginInstance.callMethod(STOP_PLUGIN_RUBY_METHOD_NAME);