Skip to content

Commit

Permalink
Merge pull request #84 from thinkonmay/dev
Browse files Browse the repository at this point in the history
Dev
  • Loading branch information
epitchi authored Sep 5, 2024
2 parents d2e3b17 + 73d0f07 commit 57275ea
Show file tree
Hide file tree
Showing 7 changed files with 50 additions and 96 deletions.
2 changes: 1 addition & 1 deletion src-tauri
Submodule src-tauri updated 2 files
+1 −1 core
+1 −1 tauri.conf.json
2 changes: 1 addition & 1 deletion src/backend/actions/background.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
};
8 changes: 6 additions & 2 deletions src/backend/reducers/fetch/analytics.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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);
}
5 changes: 1 addition & 4 deletions src/backend/reducers/fetch/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
};
41 changes: 17 additions & 24 deletions src/backend/reducers/fetch/local.ts
Original file line number Diff line number Diff line change
Expand Up @@ -152,33 +152,27 @@ export type StartRequest = {
vm?: Computer;
};

export async function KeepaliveVolume(
type callback = () => Promise<void>;
export function KeepaliveVolume(
computer: Computer,
volume_id: string,
total_time_callback?: (time_in_second: number) => Promise<void>,
cancel_fun?: () => boolean
): Promise<void> {
total_time_callback?: (time_in_second: number) => Promise<void>
): 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(
Expand Down Expand Up @@ -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));
Expand Down
61 changes: 15 additions & 46 deletions src/backend/reducers/remote.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<void>) => {
pinger = fun;
};

export const ready = async () => {
appDispatch(
popup_open({
Expand Down Expand Up @@ -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({
Expand All @@ -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;
Expand Down
27 changes: 9 additions & 18 deletions src/backend/reducers/worker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ import {
StartVirtdaemon
} from './fetch/local';
import { BuilderHelper } from './helper';
import { set_pinger } from './remote';

type WorkerType = {
data: any;
Expand Down Expand Up @@ -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;
Expand All @@ -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;
Expand Down

0 comments on commit 57275ea

Please sign in to comment.