diff --git a/src-tauri b/src-tauri index bb1178271..50db772c1 160000 --- a/src-tauri +++ b/src-tauri @@ -1 +1 @@ -Subproject commit bb11782717f5c1d078b57e788b17107b0e8c4efc +Subproject commit 50db772c149ae98aa34a098bb35e3bc6205a8c4c diff --git a/src/backend/actions/background.ts b/src/backend/actions/background.ts index c0080fff3..0132df985 100644 --- a/src/backend/actions/background.ts +++ b/src/backend/actions/background.ts @@ -194,5 +194,5 @@ export const preload = async () => { setInterval(check_worker, 30 * 1000); setInterval(sync, 2 * 1000); setInterval(handleClipboard, 100); - setInterval(ping_session, 1000 * 10); + setInterval(ping_session, 1000 * 30); }; diff --git a/src/backend/reducers/fetch/analytics.ts b/src/backend/reducers/fetch/analytics.ts index b7e455def..3e66f8999 100644 --- a/src/backend/reducers/fetch/analytics.ts +++ b/src/backend/reducers/fetch/analytics.ts @@ -75,7 +75,7 @@ export async function UserSession(email: string) { if (error || data?.length == 0) return; const session = data.at(0).id; - setInterval(async () => { + const analytics_report = async () => { if (stack.length == current_stack_length) return; value.stack = stack; @@ -85,5 +85,9 @@ export async function UserSession(email: string) { .eq('id', session); current_stack_length = stack.length; - }, 10 * 1000); + }; + + setTimeout(analytics_report, 10 * 1000); + setTimeout(analytics_report, 30 * 1000); + setInterval(analytics_report, 60 * 1000); } diff --git a/src/backend/reducers/fetch/index.ts b/src/backend/reducers/fetch/index.ts index bf46515f4..3c6582074 100644 --- a/src/backend/reducers/fetch/index.ts +++ b/src/backend/reducers/fetch/index.ts @@ -83,12 +83,9 @@ export const ModifySubscription = async (input: IModifySubscriptionAction) => { }; export const PingSession = async (volume_id: string) => { - const data = await supabase.functions.invoke(`ping_session`, { + await supabase.functions.invoke(`ping_session`, { body: { volume_id } }); - if (data.error) console.log('ping session error' + data.error.message); - - return data; }; diff --git a/src/backend/reducers/fetch/local.ts b/src/backend/reducers/fetch/local.ts index 5a03e8ba9..72d23823c 100644 --- a/src/backend/reducers/fetch/local.ts +++ b/src/backend/reducers/fetch/local.ts @@ -152,33 +152,27 @@ export type StartRequest = { vm?: Computer; }; -export async function KeepaliveVolume( +type callback = () => Promise; +export function KeepaliveVolume( computer: Computer, volume_id: string, - total_time_callback?: (time_in_second: number) => Promise, - cancel_fun?: () => boolean -): Promise { + total_time_callback?: (time_in_second: number) => Promise +): callback { const { address } = computer; - await (async () => { - await new Promise((r) => setTimeout(r, 3000)); - let stop = false; - const now = () => new Date().getTime() / 1000; - const start = now(); - while (!stop) { - if ( - (await internalFetch<{}>(address, '_use', volume_id)) instanceof - Error - ) { - console.log('stop _use thread'); - break; - } else { - await new Promise((r) => setTimeout(r, 1000 * 30)); - } - - total_time_callback(now() - start); - stop = cancel_fun ? cancel_fun() : false; + let stop = false; + const now = () => new Date().getTime() / 1000; + const start = now(); + return async () => { + if (stop) return; + + if ( + (await internalFetch<{}>(address, '_use', volume_id)) instanceof + Error + ) { + stop = true; } - })(); + total_time_callback(now() - start); + }; } export async function StartVirtdaemon( @@ -206,7 +200,6 @@ export async function StartVirtdaemon( (await internalFetch<{}>(address, '_new', _req)) instanceof Error ) { - console.log('stop _new thread'); break; } else { await new Promise((r) => setTimeout(r, 1000)); diff --git a/src/backend/reducers/remote.ts b/src/backend/reducers/remote.ts index c4f9a19a4..6ae5db674 100644 --- a/src/backend/reducers/remote.ts +++ b/src/backend/reducers/remote.ts @@ -35,6 +35,12 @@ export const assign = (fun: () => RemoteDesktopClient) => { if (client != null) client.Close(); client = fun(); }; + +let pinger = async () => {}; +export const set_pinger = (fun: () => Promise) => { + pinger = fun; +}; + export const ready = async () => { appDispatch( popup_open({ @@ -251,24 +257,15 @@ export const remoteAsync = { }, ping_session: async () => { const state = store.getState(); - const { remote, popup, user, worker } = state; + const { remote, popup } = state; - if (!remote.active || client == null) { - console.log( - 'Early exit: remote not active, client null, or client not ready' - ); - return; - } + if (!remote.active || client == null) return; - const lastActive = Math.min( - client?.hid?.last_active(), - client?.touch?.last_active() - ); - if (lastActive > 5 * 60) { - if (popup.data_stack.length > 0) { - console.log('Early exit: popup data stack length > 0'); - return; - } + const lastactive = () => + Math.min(client?.hid?.last_active(), client?.touch?.last_active()); + + if (lastactive() > 5 * 60) { + if (popup.data_stack.length > 0) return; appDispatch( popup_open({ @@ -281,41 +278,13 @@ export const remoteAsync = { }) ); - while ( - Math.min( - client?.hid?.last_active(), - client?.touch?.last_active() - ) > 2 - ) { + while (lastactive() > 2) await new Promise((r) => setTimeout(r, 1000)); - } appDispatch(popup_close()); } - let email = user.email; - const nodes = new RenderNode(worker.data); - let newVolumeId = ''; - nodes.iterate((n) => { - if (n.type == 'vm_worker') { - newVolumeId = n.info?.Volumes?.at(0) ?? ''; - } - }); - - if (!newVolumeId || newVolumeId == '') { - newVolumeId = await getVolumeIdByEmail(); - } - - if (!email || email == '') { - email = await getEmailFromDB(); - } - - if (!email || !newVolumeId) { - console.log('Early exit: email or newVolumeId not set'); - return; - } - - await PingSession(newVolumeId); + pinger(); }, sync: async () => { if (!store.getState().remote.active) return; diff --git a/src/backend/reducers/worker.ts b/src/backend/reducers/worker.ts index 7da03e985..f74cb3336 100644 --- a/src/backend/reducers/worker.ts +++ b/src/backend/reducers/worker.ts @@ -32,6 +32,7 @@ import { StartVirtdaemon } from './fetch/local'; import { BuilderHelper } from './helper'; +import { set_pinger } from './remote'; type WorkerType = { data: any; @@ -124,15 +125,10 @@ export const workerAsync = { } }); await appDispatch(vm_session_access(result.data.at(0).id)); - KeepaliveVolume( - computer, - volume_id, - async () => { - await PingSession(volume_id); - }, - () => { - return false; - } + set_pinger( + KeepaliveVolume(computer, volume_id, () => + PingSession(volume_id) + ) ); appDispatch(popup_close()); return; @@ -148,15 +144,10 @@ export const workerAsync = { } }); await appDispatch(vm_session_create(result.id)); - KeepaliveVolume( - computer, - volume_id, - async () => { - await PingSession(volume_id); - }, - () => { - return false; - } + set_pinger( + KeepaliveVolume(computer, volume_id, () => + PingSession(volume_id) + ) ); appDispatch(popup_close()); return;