diff --git a/admin/src/App.js b/admin/src/App.js index 4bf6ee1cb..996d2f744 100644 --- a/admin/src/App.js +++ b/admin/src/App.js @@ -42,10 +42,36 @@ class App extends React.Component { }); } - checkConfig() { + async checkConfig(setup = false) { this.setState({ configChecked: true, }); + try { + let res = await Api.checkConfig(); + if (res.error) { + this.props.msg({ + type: "error", + message: res.error, + }); + throw "Config error"; + } + if (setup && !res.ready) { + setTimeout(() => { + this.checkConfig(true); + }, 10000); + return; + } + this.setState({ + config: res.config, + loading: false, + }); + } catch { + this.setState({ + error: true, + loading: false, + }); + } + Api.checkConfig() .then((res) => { console.log(res); @@ -177,7 +203,7 @@ class App extends React.Component { })}
- +
); diff --git a/admin/src/page/Setup.js b/admin/src/page/Setup.js index 2368be6f5..dd60d0122 100644 --- a/admin/src/page/Setup.js +++ b/admin/src/page/Setup.js @@ -195,60 +195,93 @@ class Setup extends React.Component { db: this.state.mongoType + this.state.db, }; this.waitText(); - Api.saveConfig(config) - .then(() => { - setTimeout(() => { - this.props.checkConfig(); - }, 80000); - }) - .catch(() => { - alert( - "Setup failed, check API is running and no errors, if this persists please contact the dev team." - ); - window.location.reload(false); + try { + await Api.saveConfig(config); + this.props.checkConfig(true); + } catch { + this.props.msg({ + type: "error", + message: + "Setup failed, check API is running and no errors, if this persists please contact the dev team.", }); + this.setTimeout(() => { + location.reload(); + }, 3000); + } } - async waitText() { - await this.timeout(10000); - this.setState({ - finalText: "Creating configs...", - }); - await this.timeout(10000); - this.setState({ - finalText: "Connecting to the database...", - }); - await this.timeout(10000); - this.setState({ - finalText: "Loading your libraries...", - }); - await this.timeout(10000); - this.setState({ - finalText: "Finding your embarassing movies...", - }); - await this.timeout(10000); - this.setState({ - finalText: "Finding your embarassing movies... oh wow...", - }); - await this.timeout(10000); - this.setState({ - finalText: "Matching content against online sources...", - }); - await this.timeout(10000); - this.setState({ - finalText: "Getting your friends...", - }); - await this.timeout(10000); - this.setState({ - finalText: "Finishing up...", - }); - await this.timeout(50000); + async waitText(it = 1) { + if (this.state.exiting) return; + let text, + to = 0; + switch (it) { + case 1: + text = "Starting things up..."; + to = 1000; + break; + case 2: + text = "Creating configs..."; + to = 1000; + break; + case 3: + text = "Connecting to the database..."; + to = 1000; + break; + case 4: + text = "Getting your friends..."; + to = 1000; + break; + case 5: + text = "Loading your libraries..."; + to = 3000; + break; + case 6: + text = "Finding your embarassing movies..."; + to = 3000; + break; + case 7: + text = "Finding your embarassing movies... oh wow..."; + to = 1000; + break; + case 8: + text = "Building your Petio library..."; + to = 3000; + break; + case 9: + text = + "Building your Petio library... Grab a cup of tea this could take a few minutes..."; + to = 1000; + break; + case 10: + text = + "If you're library is extremely big or your server connection is remote this might take a little while..."; + to = 120000; + break; + case 11: + text = + "We're not done, but you can start using Petio now while the rest of your library scans. Just a second we'll let you in...."; + to = 120000; + break; + case 12: + text = + "We're not done, but you can start using Petio now while the rest of your library scans. Just a second we'll let you in...."; + to = 3000; + break; + case 13: + location.reload(); + break; + } + it++; + await this.timeout(to); this.setState({ - finalText: "This is taking a while...", + finalText: text, }); - await this.timeout(100000); + this.waitText(it); + } + + componentWillUnmount() { this.setState({ - finalText: "Should really be done by now...", + exiting: true, }); } @@ -373,7 +406,12 @@ class Setup extends React.Component { value={this.state.password} onChange={this.inputChange} /> - @@ -607,6 +645,7 @@ function SetupContainer(props) { api={props.api} user={props.user} checkConfig={props.checkConfig} + msg={props.msg} /> ); } diff --git a/api/app.js b/api/app.js index 036dafd45..17e46717c 100755 --- a/api/app.js +++ b/api/app.js @@ -14,6 +14,7 @@ const bcrypt = require("bcryptjs"); // Config const getConfig = require("./util/config"); +const setupReady = require("./util/setupReady"); const Worker = require("./worker"); // Plex @@ -80,13 +81,34 @@ class Main { logger.log("info", "API: Setting up routes"); this.e.get("/config", async (req, res) => { let config = getConfig(); + let ready = false; + if (config) { + try { + let setupCheck = await setupReady(); + if (setupCheck.ready) { + ready = true; + } + if (setupCheck.error) { + res.status(500).json({ + error: "An error has occured", + }); + return; + } + } catch { + res.status(500).json({ + error: "An error has occured", + }); + return; + } + } res.json( config ? { config: true, login_type: config.login_type ? config.login_type : 1, + ready: ready, } - : { config: false, login_type: 1 } + : { config: false, login_type: 1, ready: ready } ); }); this.setup(); diff --git a/api/discovery/build.js b/api/discovery/build.js index effdf2fad..b07a32432 100644 --- a/api/discovery/build.js +++ b/api/discovery/build.js @@ -84,7 +84,7 @@ async function create(id) { } } catch (err) { // - console.log(err); + // console.log(err); } return; } diff --git a/api/util/setupReady.js b/api/util/setupReady.js new file mode 100644 index 000000000..cef59db06 --- /dev/null +++ b/api/util/setupReady.js @@ -0,0 +1,33 @@ +const Movie = require("../models/movie"); +const Show = require("../models/show"); +const User = require("../models/user"); +const logger = require("./logger"); + +async function setupReady() { + try { + let [movie, show, user] = await Promise.all([ + Movie.findOne(), + Show.findOne(), + User.findOne(), + ]); + if (movie && show && user) { + return { + ready: true, + error: false, + }; + } else { + return { + ready: false, + error: false, + }; + } + } catch { + logger.error("CHK: Fatal Error unable to write Db to file!"); + return { + ready: false, + error: "Database write error", + }; + } +} + +module.exports = setupReady; diff --git a/api/worker.js b/api/worker.js index 6369fdcfc..56f2adcec 100644 --- a/api/worker.js +++ b/api/worker.js @@ -23,6 +23,7 @@ class Worker { } async startCrons() { + // return; // for debug local try { await this.connnectDb(); const libUpdate = new LibraryUpdate();