Skip to content

A fully open source & end-to-end encrypted note taking alternative to Evernote.

server(themes): fix sync and install count edge cases

Abdullah Attad2ff43163744
diff --git a/servers/themes/src/api.ts b/servers/themes/src/api.ts
index 9a9a9d175..093e07178 100644
--- a/servers/themes/src/api.ts
+++ b/servers/themes/src/api.ts
@@ -41,10 +41,9 @@ export const ThemesAPI = router({
if (!theme) return;
if (userId) {
- updateTotalInstalls(
- theme,
- await InstallsCounter.increment(theme.id, userId)
- ).catch((e) => console.error("Failed to update total installs:", e));
+ InstallsCounter.increment(theme.id, userId)
+ .then((totalInstalls) => updateTotalInstalls(theme.id, totalInstalls))
+ .catch((e) => console.error("Failed to update total installs:", e));
}
return theme;
}),
diff --git a/servers/themes/src/orama.ts b/servers/themes/src/orama.ts
index b1c338b77..b4999b998 100644
--- a/servers/themes/src/orama.ts
+++ b/servers/themes/src/orama.ts
@@ -16,7 +16,14 @@ GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
-import { Orama, SearchParams, create, search, update } from "@orama/orama";
+import {
+ Orama,
+ SearchParams,
+ create,
+ getByID,
+ search,
+ update
+} from "@orama/orama";
import { CompiledThemeDefinition, ThemeMetadata } from "./sync";
import { ThemeQuerySchema } from "./schemas";
@@ -62,12 +69,12 @@ export async function findTheme(
return results.hits[0]?.document as CompiledThemeDefinition | undefined;
}
-export async function updateTotalInstalls(
- theme: CompiledThemeDefinition,
- totalInstalls: number
-) {
- if (!ThemesDatabase) await initializeDatabase();
- await update(ThemesDatabase!, theme.id, { ...theme, totalInstalls });
+export async function updateTotalInstalls(id: string, totalInstalls: number) {
+ const db = ThemesDatabase || (await initializeDatabase());
+ // re-read so a theme updated by a sync isn't overwritten with a stale copy
+ const theme = await getByID(db, id);
+ if (!theme) return;
+ await update(db, id, { ...theme, totalInstalls });
}
export async function getThemes(query: (typeof ThemeQuerySchema)["_type"]) {
diff --git a/servers/themes/src/sync.ts b/servers/themes/src/sync.ts
index d3592c756..564a9b128 100644
--- a/servers/themes/src/sync.ts
+++ b/servers/themes/src/sync.ts
@@ -19,7 +19,7 @@ along with this program. If not, see <http://www.gnu.org/licenses/>.
import { execFile } from "child_process";
import { promisify } from "util";
import fs from "node:fs";
-import { readdir, readFile } from "fs/promises";
+import { readdir, readFile, rm } from "fs/promises";
import path from "path";
import {
InstallsCounter,
@@ -51,11 +51,17 @@ const THEME_COMPATIBILITY_VERSIONS: ThemeCompatibilityVersion[] = [1];
const execFileAsync = promisify(execFile);
const GIT_TIMEOUT = 60 * 1000;
let currentSync: Promise<void> | undefined;
+let isCurrentSyncFull = false;
-export function syncThemes(onlyIfChanged = false) {
+export function syncThemes(onlyIfChanged = false): Promise<void> {
// concurrent git pulls fail on index.lock, so piggyback on the running sync
- if (!currentSync)
- currentSync = sync(onlyIfChanged).finally(() => (currentSync = undefined));
+ if (currentSync) {
+ if (onlyIfChanged || isCurrentSyncFull) return currentSync;
+ // the running check-only sync may skip the rebuild, so queue a full one
+ return currentSync.catch(() => {}).then(() => syncThemes());
+ }
+ isCurrentSyncFull = !onlyIfChanged;
+ currentSync = sync(onlyIfChanged).finally(() => (currentSync = undefined));
return currentSync;
}
@@ -75,13 +81,28 @@ async function hasUpstreamChanges() {
return remote.split(/\s/)[0] !== local;
}
+// a failed clone/pull can leave the repo unusable (e.g. index.lock), so re-clone next time
+async function resetOnFailure(promise: Promise<unknown>) {
+ try {
+ await promise;
+ } catch (e) {
+ await rm(THEME_REPO_DIR_PATH, { recursive: true, force: true });
+ throw e;
+ }
+}
+
async function sync(onlyIfChanged: boolean) {
if (!fs.existsSync(THEME_REPO_DIR_PATH)) {
- await git(["clone", THEMES_REPO_URL], path.dirname(THEME_REPO_DIR_PATH));
+ await resetOnFailure(
+ git(
+ ["clone", "--quiet", THEMES_REPO_URL],
+ path.dirname(THEME_REPO_DIR_PATH)
+ )
+ );
console.log(`Cloned github repo to path ${THEME_REPO_DIR_PATH}`);
} else {
if (onlyIfChanged && !(await hasUpstreamChanges())) return;
- console.log(await git(["pull"], THEME_REPO_DIR_PATH));
+ await resetOnFailure(git(["pull", "--quiet"], THEME_REPO_DIR_PATH));
console.log(`Synced github repo ${THEMES_REPO_URL}`);
}
await generateThemesMetadata();