Title: [743] trunk/rails-integration/src/main/java/org/jruby/webapp: Extracted servlet to run periodical background tasks from Mingle.
- Revision
- 743
- Author
- tirsen
- Date
- 2007-09-25 03:40:30 -0400 (Tue, 25 Sep 2007)
Log Message
Extracted servlet to run periodical background tasks from Mingle. Removed the require of rubygems (Mingle does not include rubygems)
Modified Paths
Added Paths
Diff
Modified: trunk/rails-integration/src/main/java/org/jruby/webapp/RailsFactory.java (742 => 743)
--- trunk/rails-integration/src/main/java/org/jruby/webapp/RailsFactory.java 2007-09-21 19:29:39 UTC (rev 742)
+++ trunk/rails-integration/src/main/java/org/jruby/webapp/RailsFactory.java 2007-09-25 07:40:30 UTC (rev 743)
@@ -118,14 +118,6 @@
// This is done in CustomObjectPool by forcing all object creation though a single thread.
runtime.defineReadonlyVariable("$servlet_context", JavaEmbedUtils.javaToRuby(runtime, context));
- // test Gems
- try {
- runtime.getLoadService().require("rubygems");
- } catch (RaiseException e) {
- logRubyException("Failed to load rubygems", e);
- throw new ServletException("Failed to load rubygems. See the logs for more details.");
- }
-
// start Rails
try {
long startTime = System.currentTimeMillis();
Added: trunk/rails-integration/src/main/java/org/jruby/webapp/RailsPeriodicalTaskServlet.java (0 => 743)
--- trunk/rails-integration/src/main/java/org/jruby/webapp/RailsPeriodicalTaskServlet.java (rev 0)
+++ trunk/rails-integration/src/main/java/org/jruby/webapp/RailsPeriodicalTaskServlet.java 2007-09-25 07:40:30 UTC (rev 743)
@@ -0,0 +1,97 @@
+package org.jruby.webapp;
+
+import org.jruby.Ruby;
+import org.jruby.webapp.RailsServlet;
+
+import javax.servlet.ServletConfig;
+import javax.servlet.ServletException;
+import javax.servlet.http.HttpServletRequest;
+import javax.servlet.http.HttpServletResponse;
+import java.io.IOException;
+
+public class RailsPeriodicalTaskServlet extends RailsServlet {
+ private String command;
+ private int waitBeforeStartSeconds = 30; // wait 30 seconds
+ private boolean stop = false; // wait 30 seconds
+ private Thread thread = null;
+ private int interval = 60;
+ private int minIdle = 0;
+ private static final int FIFTEEN_MINUTES_IN_MILLIS = 1000 * 60 * 15;
+
+ public void init(final ServletConfig servletConfig) throws ServletException {
+ super.init(servletConfig);
+
+ command = servletConfig.getInitParameter("command");
+ String waitBeforeStartSecondsString = servletConfig.getInitParameter("waitBeforeStartSeconds");
+ if (waitBeforeStartSecondsString != null) {
+ waitBeforeStartSeconds = Integer.parseInt(waitBeforeStartSecondsString);
+ }
+ String minIdleString = servletConfig.getInitParameter("minIdle");
+ if (minIdleString != null) {
+ minIdle = Integer.parseInt(minIdleString);
+ }
+ String intervalSecondsString = servletConfig.getInitParameter("interval");
+ if (intervalSecondsString != null) {
+ interval = Integer.parseInt(intervalSecondsString);
+ }
+
+ thread = new Thread(new Runnable() {
+ public void run() {
+ try {
+ // wait for a little while before starting the task
+ // this allow the app server to start serving requests before initializing all tasks
+ Thread.sleep(waitBeforeStartSeconds * 1000);
+
+ while (!stop) {
+ runOnce();
+ Thread.sleep(interval * 1000); // wait for interval
+ }
+ } catch (InterruptedException e) {
+ // break out of loop
+ } catch (Exception e) {
+ servletConfig.getServletContext().log("Could not start " + command, e);
+ }
+ }
+ });
+ thread.start();
+ }
+
+ private void runOnce() throws Exception {
+ // run task only if idles are more than required
+ // default is 0 which means it will always run
+ // (potentially causing another instance to get started)
+ if (getRuntimePool().getNumIdle() <= minIdle) {
+ return;
+ }
+ Ruby runtime = null;
+ try {
+ runtime = (Ruby) getRuntimePool().borrowObject();
+ runtime.evalScript(command);
+ getRuntimePool().returnObject(runtime);
+ } catch (Exception e) {
+ getServletContext().log("Could not execute: " + command, e);
+ getRuntimePool().invalidateObject(runtime);
+ getServletContext().log(command + " returning JRuby runtime to pool and will restart in 15 minutes.");
+ try {
+ Thread.sleep(FIFTEEN_MINUTES_IN_MILLIS);
+ } catch (InterruptedException ex) {
+ // can't do much here ...
+ }
+ }
+ }
+
+ public void destroy() {
+ stop = true;
+ thread.interrupt();
+ try {
+ thread.join();
+ } catch (InterruptedException e) {
+ }
+
+ super.destroy();
+ }
+
+ protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
+ throw new ServletException("Not supported");
+ }
+}
_______________________________________________
Jruby-extras-devel mailing list
[email protected]
http://rubyforge.org/mailman/listinfo/jruby-extras-devel