2
* Copyright 2009 Google Inc.
4
* Licensed under the Apache License, Version 2.0 (the "License");
5
* you may not use this file except in compliance with the License.
6
* You may obtain a copy of the License at
8
* http://www.apache.org/licenses/LICENSE-2.0
10
* Unless required by applicable law or agreed to in writing, software
11
* distributed under the License is distributed on an "AS-IS" BASIS,
12
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13
* See the License for the specific language governing permissions and
14
* limitations under the License.
19
import("cache_utils.syncedWithCache");
21
import("etherpad.pad.padutils");
22
import("etherpad.pro.pro_padmeta");
23
import("etherpad.log");
25
var _DOMAIN_EDIT_WRITE_INTERVAL = 2000; // 2 seconds
27
function _withCache(name, fn) {
28
return syncedWithCache('pro-padmeta.'+name, fn);
31
function _withDomainCache(domainId, name, fn) {
32
return _withCache(name+"."+domainId, fn);
36
function onStartup() {
37
execution.initTaskThreadPool("pro-padmeta-edits", 1);
40
function onShutdown() {
41
var success = execution.shutdownAndWaitOnTaskThreadPool("pro-padmeta-edits", 4000);
43
log.warn("Warning: pro.padmeta failed to flush pad edits on shutdown.");
47
function notifyEdit(domainId, localPadId, editorId, editTime) {
52
_withDomainCache(domainId, "edits", function(c) {
55
lastEditorId: editorId,
56
lastEditTime: editTime,
60
var info = c[localPadId];
61
if (info.recentEditors.indexOf(editorId) < 0) {
62
info.recentEditors.push(editorId);
65
_flushPadEditsEventually(domainId);
69
function _flushPadEditsEventually(domainId) {
70
// Make sure there is a recurring edit-writer for this domain
71
_withDomainCache(domainId, "recurring-edit-writers", function(c) {
73
flushEditsNow(domainId);
79
function flushEditsNow(domainId) {
80
if (!appjet.cache.shutdownHandlerIsRunning) {
81
execution.scheduleTask("pro-padmeta-edits", "proPadmetaFlushEdits",
82
_DOMAIN_EDIT_WRITE_INTERVAL, [domainId]);
85
_withDomainCache(domainId, "edits", function(edits) {
86
var padIdList = keys(edits);
87
padIdList.forEach(function(localPadId) {
88
_writePadEditsToDbNow(domainId, localPadId, edits[localPadId]);
89
delete edits[localPadId];
94
function _writePadEditsToDbNow(domainId, localPadId, editInfo) {
95
var globalPadId = padutils.makeGlobalId(domainId, localPadId);
96
pro_padmeta.accessProPad(globalPadId, function(propad) {
97
propad.setLastEditedDate(editInfo.lastEditTime);
98
propad.setLastEditor(editInfo.lastEditorId);
99
editInfo.recentEditors.forEach(function(eid) {
100
propad.addEditor(eid);