Skip to content

Commit

Permalink
async the sync task
Browse files Browse the repository at this point in the history
  • Loading branch information
GedasFX committed Mar 8, 2023
1 parent 1051edf commit 5ab47ed
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 8 deletions.
14 changes: 9 additions & 5 deletions main.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
import logging
import os
import re
import subprocess
from pathlib import Path

plugin_dir = Path(os.path.dirname(os.path.realpath(__file__)))
Expand Down Expand Up @@ -44,6 +43,7 @@ async def _kill_previous_spawn(process: asyncio.subprocess.Process):

class Plugin:
current_spawn = None
current_sync = None

async def spawn(self, backend_type: str):
logger.debug("Executing: spawn(%s)", backend_type)
Expand Down Expand Up @@ -82,9 +82,14 @@ async def get_backend_type(self):

async def sync_now(self):
logger.debug("Executing: sync_now()")
subprocess.run([rclone_bin, "copy", "--include-from",
cfg_syncpath_file, "/", "backend:decky-cloud-save", "--copy-links"])
self.current_sync = await asyncio.subprocess.create_subprocess_exec(rclone_bin, *["copy", "--include-from", cfg_syncpath_file, "/", "backend:decky-cloud-save", "--copy-links"])

async def sync_now_probe(self):
logger.debug("Executing: sync_now_probe()")
if not self.current_sync:
return 0

return self.current_sync.returncode

#

Expand Down Expand Up @@ -164,7 +169,6 @@ async def set_config(self, key: str, value: str):

# Asyncio-compatible long-running code, executed in a task when the plugin is loaded


async def _main(self):
logger.debug("rclone exe path: %s", rclone_bin)
logger.debug("rclone cfg path: %s", rclone_cfg)
Expand All @@ -179,4 +183,4 @@ async def _main(self):

# Function called first during the unload process, utilize this to handle your plugin being removed
async def _unload(self):
await _kill_previous_spawn(self.current_spawn) # Kills only if exists
await _kill_previous_spawn(self.current_spawn) # Kills only if exists
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "decky-cloud-save",
"version": "1.1.0",
"version": "1.1.1",
"description": "Manage cloud saves for games that do not support it in [current year].",
"scripts": {
"build": "shx rm -rf dist && rollup -c",
Expand Down
28 changes: 26 additions & 2 deletions src/apiClient.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,37 @@
import { sleep } from "decky-frontend-lib";
import { getServerApi, setAppState } from "./state";

export async function syncNow(): Promise<void> {
const start = new Date();

setAppState("syncing", "true");
await getServerApi().callPluginMethod("sync_now", {});
setAppState("syncing", "false");

getServerApi().toaster.toast({ title: "Decky Cloud Save", body: `Sync completed in ${(new Date().getTime() - start.getTime()) / 1000}s.` });
let exitCode = 0;
while (true) {
const status = await getServerApi().callPluginMethod<{}, number | undefined>("sync_now_probe", {});

if (status.success && status.result != null) {
exitCode = status.result;
break;
}

await sleep(360);
}

let body;
switch (exitCode) {
case 0:
case 6:
body = `Sync completed in ${(new Date().getTime() - start.getTime()) / 1000}s.`;
break;
default:
body = `Sync failed. Check journalctl for errors.`;
break;
}

setAppState("syncing", "false");
getServerApi().toaster.toast({ title: "Decky Cloud Save", body });
}

export async function getCloudBackend(): Promise<string | undefined> {
Expand Down

0 comments on commit 5ab47ed

Please sign in to comment.