Warning: session_start(): open(/var/lib/php/sessions/sess_kb8ctrcnjcdfmbge30gvcdmami, O_RDWR) failed: No space left on device (28) in /var/www/tb-usercake/models/config.php on line 51
Warning: session_start(): Failed to read session data: files (path: /var/lib/php/sessions) in /var/www/tb-usercake/models/config.php on line 51
!7
static double updateInterval = 60;
sbool infoBoxes, fullUpdateAtStart = true;
sbool snippetsDB_inited;
sbool autoDownloadText = true;
concept CSnippet {
long snippetID;
S type;
S title, md5, transpilationMD5;
File file() { ret snippetDB_textFile(snippetID); }
S text() { ret loadTextFile(file()); }
bool hasText() { ret fileExists(file()); }
void setText(S text) { saveTextFile(file(), text); }
}
static Thread downloadThread;
p {
//restartWith384MBHeap(); // for xfullgrab
autoRestart10Mins();
snippetsDB_init();
}
svoid snippetsDB_init() {
lock programLock();
if (snippetsDB_inited) ret;
set snippetsDB_inited;
mainConcepts.autoSaveInterval = 1000*60; // every minute
mainConcepts.noXFullGrab = true;
dbIndexing(CSnippet, 'snippetID);
botWithCommandList("Snippets.");
if (fullUpdateAtStart) fullDownload();
doEvery(0, updateInterval, f update);
}
svoid fullDownload { fullDownload(true); }
svoid fullDownload(bool withText) {
L l = listAllSnippets();
printWithTime(n(l, "snippet") + " on server");
slurp(l);
if (withText) downloadSomeText();
}
static int slurp(L l) {
lock dbLock();
int changedSnippets = 0;
for (Snippet s : l) {
CSnippet cs = uniq(CSnippet, snippetID := parseSnippetID(s.id));
int changes = 0;
changes += cset(cs, title := s.title, type := s.type);
if (neq(cs.md5, s.md5)) {
changes += cset(cs, md5 := s.md5);
if (cs.hasText()) { cs.setText(null); ++changes; }
}
if (changes > 0) ++changedSnippets;
}
S text = n2(changedSnippets, "changed snippet");
if (infoBoxes && changedSnippets != 0) infoBox(text);
printWithTime(text);
ret changedSnippets;
}
// with text if autoDownloadText is true
svoid update {
L l = listRecentlyChangedSnippets();
printWithTime(n(l, "snippet") + " on server");
int changedSnippets = slurp(l);
if (changedSnippets >= l(l))
fullDownload(autoDownloadText);
else if (autoDownloadText)
downloadSomeText();
}
answer {
lock dbLock();
if "full download" {
fullDownload();
ret "OK, have " + n(countConcepts(CSnippet), "snippet");
}
if "update" {
update();
ret "OK";
}
if "download some text" {
if (downloadThread != null) ret "Already downloading";
downloadSomeText();
ret "Yo";
}
if "stop downloading" {
cancelThread(downloadThread);
downloadThread = null;
ret "OK";
}
if "get snippet text *" {
CSnippet c = findConcept(CSnippet, snippetID := parseSnippetID($1));
if (c == null) ret "not found";
downloadText(c);
ret "OK " + quote(c.text());
}
if "count" ret n2(countConcepts(CSnippet), "snippet");
if "all snippet ids"
ret struct(map fsI(sorted(collect(CSnippet, 'snippetID))));
if "kill" { cleanKillIn1(); ret "OK"; }
}
svoid downloadText(CSnippet s) {
if (s.hasText()) ret;
s.setText(eq(s.md5, emptyMD5()) ? "" : loadSnippet(s.snippetID));
}
svoid downloadSomeText {
lock dbLock();
if (downloadThread != null) ret;
downloadThread = startThread(r {
try {
int n = 0; for (CSnippet s) if (s.hasText()) ++n;
printWithTime("Have text for " + n + "/" + n(countConcepts(CSnippet), "concept");
new ConcurrentEvaluator e;
e.coresToUse = 10;
for (final CSnippet s) {
ping();
if (!s.hasText()) e.add(r {
ping();
downloadText(s);
/*++n;
if ((n % 100) == 0)
printWithTime("Have text for " + n + "/" + n(countConcepts(CSnippet), "concept");*/
});
}
e.startWaitCleanUp();
} finally {
downloadThread = null;
}
});
}