Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: is_offline command & handle reload and offline #480

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 22 additions & 2 deletions src-tauri/src/controller_binaries.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,12 @@ use crate::{
download::Downloader,
err,
errors::{Context, Result},
logerr, Service, SharedState,
logerr, utils, LiveState, Service, SharedState,
};

use std::path::PathBuf;
use std::time::Duration;
use std::{collections::HashMap, sync::Arc};
use std::{ops::Deref, path::PathBuf};

use futures::future;

Expand Down Expand Up @@ -465,3 +465,23 @@ pub async fn add_service(service: Service, state: State<'_, Arc<SharedState>>) -
services_guard.insert(service.get_id()?, service);
Ok(())
}

#[tauri::command(async)]
pub async fn is_offline(live_state: State<'_, LiveState>) -> Result<bool> {
Ok(!live_state.lock().await.is_online)
}

#[tauri::command(async)]
pub async fn reload_services_manifest(
state: State<'_, Arc<SharedState>>,
live_state: State<'_, LiveState>,
) -> Result<()> {
let mut live_state = live_state.lock().await;
let url = live_state.url.clone();
let res = utils::fetch_services_manifests(&url, state.deref().clone()).await;
// if loaded set is_online to true
if res.is_ok() {
live_state.is_online = true;
}
res
}
26 changes: 20 additions & 6 deletions src-tauri/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,13 @@ struct ModelInfo {
streaming: Option<bool>,
}

#[derive(Debug, Deserialize, Serialize, Clone, Default)]
pub struct LiveStateInner {
pub is_online: bool,
pub url: String,
}
pub type LiveState = Arc<Mutex<LiveStateInner>>;

fn main() {
let client = sentry::init((
"https://b98405fd0e4cc275b505645d293d23a5@o4506111848808448.ingest.sentry.io/4506111925223424",
Expand Down Expand Up @@ -191,6 +198,7 @@ fn main() {
let app = tauri::Builder::default()
.plugin(sentry_tauri::plugin())
.manage(state.clone())
.manage(LiveState::default())
.invoke_handler(tauri::generate_handler![
controller_binaries::start_service,
controller_binaries::stop_service,
Expand Down Expand Up @@ -256,12 +264,18 @@ fn main() {
})
.setup(|app| {
tauri::async_runtime::block_on(async move {
utils::fetch_services_manifests(
"https://raw.githubusercontent.com/premAI-io/prem-registry/v1/manifests.json",
app.state::<Arc<SharedState>>().deref().clone(),
)
.await
.expect("Failed to fetch and save services manifests");
let live_state = app.state::<LiveState>();
let mut live_state = live_state.lock().await;
let url =
"https://raw.githubusercontent.com/premAI-io/prem-registry/v1/manifests.json";
live_state.url = url.to_string();
let state = app.state::<Arc<SharedState>>().deref().clone();
let res = utils::fetch_services_manifests(&url, state).await;
if res.is_err() {
// we are offline, or invalid manifest
log::error!("possibly no internet: running in offline mode");
live_state.is_online = false;
}
});
Ok(())
})
Expand Down
8 changes: 8 additions & 0 deletions src/controller/binariesController.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,14 @@ class BinariesController extends AbstractServiceController {
await invoke("delete_service", { serviceId });
}

async is_offline(): Promise<void> {
await invoke("is_offline");
}

async reload_services_manifest(): Promise<void> {
await invoke("reload_services_manifest");
}

async download({
serviceId,
binariesUrl,
Expand Down