From 8c03c1ddcf6373ac72e8a95684060927aa855e18 Mon Sep 17 00:00:00 2001 From: David Huynh Date: Sun, 9 May 2010 04:34:36 +0000 Subject: [PATCH] Prevent autosave timer events from bunching up when the computer is put into sleep mode. Don't autosave while creating or importing projects, exporting rows, or uploading data to Freebase. Those are potentially intensive operations. git-svn-id: http://google-refine.googlecode.com/svn/trunk@627 7d457c2a-affb-35e4-300a-418c747d4874 --- .../metaweb/gridworks/GridworksServlet.java | 24 ++++++++++++------- .../com/metaweb/gridworks/ProjectManager.java | 23 +++++++++++++++--- .../commands/edit/CreateProjectCommand.java | 3 +++ .../commands/edit/ImportProjectCommand.java | 3 +++ .../commands/edit/UploadDataCommand.java | 4 ++++ .../commands/info/ExportRowsCommand.java | 4 ++++ 6 files changed, 49 insertions(+), 12 deletions(-) diff --git a/src/main/java/com/metaweb/gridworks/GridworksServlet.java b/src/main/java/com/metaweb/gridworks/GridworksServlet.java index 725baffb4..acf9615ef 100644 --- a/src/main/java/com/metaweb/gridworks/GridworksServlet.java +++ b/src/main/java/com/metaweb/gridworks/GridworksServlet.java @@ -155,6 +155,19 @@ public class GridworksServlet extends HttpServlet { _commands.put("mqlread", new MQLReadCommand()); _commands.put("mqlwrite", new MQLWriteCommand()); } + + final static protected long s_autoSavePeriod = 1000 * 60 * 1; // minutes + static protected class AutoSaveTimerTask extends TimerTask { + public void run() { + try { + ProjectManager.singleton.save(false); // quick, potentially incomplete save + } finally { + _timer.schedule(new AutoSaveTimerTask(), s_autoSavePeriod); + // we don't use scheduleAtFixedRate because that might result in + // bunched up events when the computer is put in sleep mode + } + } + } @Override public void init() throws ServletException { @@ -164,17 +177,10 @@ public class GridworksServlet extends HttpServlet { ProjectManager.initialize(); if (_timer == null) { - _timer = new Timer(); + _timer = new Timer("autosave"); + _timer.schedule(new AutoSaveTimerTask(), s_autoSavePeriod); } - long period = 1000 * 60 * 5; // 5 minutes - _timer.scheduleAtFixedRate(new TimerTask() { - @Override - public void run() { - ProjectManager.singleton.save(false); // quick, potentially incomplete save - } - }, period, period); - logger.trace("< initialize"); } diff --git a/src/main/java/com/metaweb/gridworks/ProjectManager.java b/src/main/java/com/metaweb/gridworks/ProjectManager.java index da41fbb2c..a714c1514 100644 --- a/src/main/java/com/metaweb/gridworks/ProjectManager.java +++ b/src/main/java/com/metaweb/gridworks/ProjectManager.java @@ -49,7 +49,12 @@ public class ProjectManager { /** * What caches the joins between projects. */ - transient protected InterProjectModel _interProjectModel = new InterProjectModel(); + transient protected InterProjectModel _interProjectModel = new InterProjectModel(); + + /** + * Flags + */ + transient protected int _busy = 0; // heavy operations like creating or importing projects are going on static public ProjectManager singleton; @@ -260,6 +265,16 @@ public class ProjectManager { } } + public void setBusy(boolean busy) { + synchronized (this) { + if (busy) { + _busy++; + } else { + _busy--; + } + } + } + public void addLatestExpression(String s) { synchronized (this) { _expressions.remove(s); @@ -275,8 +290,10 @@ public class ProjectManager { } public void save(boolean allModified) { - saveProjects(allModified); - saveWorkspace(); + if (allModified || _busy == 0) { + saveProjects(allModified); + saveWorkspace(); + } } /** diff --git a/src/main/java/com/metaweb/gridworks/commands/edit/CreateProjectCommand.java b/src/main/java/com/metaweb/gridworks/commands/edit/CreateProjectCommand.java index 487da5c3f..3eba2b4c6 100644 --- a/src/main/java/com/metaweb/gridworks/commands/edit/CreateProjectCommand.java +++ b/src/main/java/com/metaweb/gridworks/commands/edit/CreateProjectCommand.java @@ -62,6 +62,7 @@ public class CreateProjectCommand extends Command { public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { + ProjectManager.singleton.setBusy(true); try { /* * The uploaded file is in the POST body as a "file part". If @@ -97,6 +98,8 @@ public class CreateProjectCommand extends Command { ParsingUtilities.encode("Failed to import file: " + e.getLocalizedMessage()) ); e.printStackTrace(); + } finally { + ProjectManager.singleton.setBusy(false); } } diff --git a/src/main/java/com/metaweb/gridworks/commands/edit/ImportProjectCommand.java b/src/main/java/com/metaweb/gridworks/commands/edit/ImportProjectCommand.java index 9e11d55da..fb6670687 100644 --- a/src/main/java/com/metaweb/gridworks/commands/edit/ImportProjectCommand.java +++ b/src/main/java/com/metaweb/gridworks/commands/edit/ImportProjectCommand.java @@ -36,6 +36,7 @@ public class ImportProjectCommand extends Command { public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { + ProjectManager.singleton.setBusy(true); try { Properties options = ParsingUtilities.parseUrlParameters(request); @@ -63,6 +64,8 @@ public class ImportProjectCommand extends Command { } } catch (Exception e) { e.printStackTrace(); + } finally { + ProjectManager.singleton.setBusy(false); } } diff --git a/src/main/java/com/metaweb/gridworks/commands/edit/UploadDataCommand.java b/src/main/java/com/metaweb/gridworks/commands/edit/UploadDataCommand.java index a25ffcd5c..53274f78b 100644 --- a/src/main/java/com/metaweb/gridworks/commands/edit/UploadDataCommand.java +++ b/src/main/java/com/metaweb/gridworks/commands/edit/UploadDataCommand.java @@ -11,6 +11,7 @@ import javax.servlet.http.HttpServletResponse; import org.json.JSONException; import org.json.JSONObject; +import com.metaweb.gridworks.ProjectManager; import com.metaweb.gridworks.browsing.Engine; import com.metaweb.gridworks.commands.Command; import com.metaweb.gridworks.exporters.TripleloaderExporter; @@ -23,6 +24,7 @@ public class UploadDataCommand extends Command { public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { + ProjectManager.singleton.setBusy(true); try { Project project = getProject(request); Engine engine = getEngine(request, project); @@ -48,6 +50,8 @@ public class UploadDataCommand extends Command { } catch (Exception e) { respondException(response, e); + } finally { + ProjectManager.singleton.setBusy(false); } } } diff --git a/src/main/java/com/metaweb/gridworks/commands/info/ExportRowsCommand.java b/src/main/java/com/metaweb/gridworks/commands/info/ExportRowsCommand.java index 6d355b0bb..593329c1c 100644 --- a/src/main/java/com/metaweb/gridworks/commands/info/ExportRowsCommand.java +++ b/src/main/java/com/metaweb/gridworks/commands/info/ExportRowsCommand.java @@ -10,6 +10,7 @@ import javax.servlet.ServletException; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; +import com.metaweb.gridworks.ProjectManager; import com.metaweb.gridworks.browsing.Engine; import com.metaweb.gridworks.commands.Command; import com.metaweb.gridworks.exporters.Exporter; @@ -32,6 +33,7 @@ public class ExportRowsCommand extends Command { public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { + ProjectManager.singleton.setBusy(true); try { Project project = getProject(request); Engine engine = getEngine(request, project); @@ -53,6 +55,8 @@ public class ExportRowsCommand extends Command { } } catch (Exception e) { respondException(response, e); + } finally { + ProjectManager.singleton.setBusy(false); } } }