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
This commit is contained in:
David Huynh 2010-05-09 04:34:36 +00:00
parent 164abc2057
commit 8c03c1ddcf
6 changed files with 49 additions and 12 deletions

View File

@ -156,6 +156,19 @@ public class GridworksServlet extends HttpServlet {
_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 {
super.init();
@ -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");
}

View File

@ -51,6 +51,11 @@ public class ProjectManager {
*/
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;
static public synchronized void initialize() {
@ -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,9 +290,11 @@ public class ProjectManager {
}
public void save(boolean allModified) {
if (allModified || _busy == 0) {
saveProjects(allModified);
saveWorkspace();
}
}
/**
* Save the workspace's data out to file in a safe way: save to a temporary file first

View File

@ -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);
}
}

View File

@ -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);
}
}

View File

@ -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);
}
}
}

View File

@ -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);
}
}
}