0

I have PC running in Kiosk mode (self-service kiosk). It has weak internet connection, so I use local MongoDB server for all data. But sometimes (ex. daily) I want to send statistics and other stuff to remote database. When I start PC, while internet connection is down, i get following message:

error: A hook (`orm`) failed to load!
error: Error: Failed to connect to MongoDB.  Are you sure your configured Mongo instance is running?
  Error details: { MongoError: connection 1 to ds241489.mlab.com:41489 timed out
    at Function.MongoError.create (/home/nevada/v4/node_modules/mongodb-core/lib/error.js:29:11)
    at Socket.<anonymous> (/home/nevada/v4/node_modules/mongodb-core/lib/connection/connection.js:186:20)
    ...

Then Sails crashes and Browser shows error message to user

connections.js:

mlab: {
  adapter: 'sails-mongo',
  host: 'ds241489.mlab.com',
  port: *****,
  user: '******',
  password: '************',
  database: '******'
}

models/Remote.js:

module.exports = {
  connection: 'mlab',
  attributes: {
    // Some stuff here
  }
};

Everything works OK while internet is available, but when PC is offline, server refuses to start. How can I skip automatic connection to (remote) DB and then (try to) connect manually, by request from user?

NevaDA
  • 128
  • 6
  • Here is second kind of error: `[nevada@lm v4]$ sails l info: Starting app... error: Error: The hook orm is taking too long to load. Make sure it is triggering its initialize() callback, or else set sails.config.orm._hookTimeout to a higher value (currently 20000) at Timeout.tooLong [as _onTimeout] (/home/nevada/v4/node_modules/sails/lib/app/private/loadHooks.js:85:21) at ontimeout (timers.js:498:11) at tryOnTimeout (timers.js:323:5) at Timer.listOnTimeout (timers.js:290:5)` – NevaDA Jan 27 '19 at 13:13

1 Answers1

0

I haven't implemented this myself, but I would do one of two things here:

  1. Set up a separate cron or other type of scheduled task to run every hour or so. This would then check for internet, and if internet connects successfully, run the DB-sync script. Of course inside a big try/catch to gracefully handle internet disconnect half way through.

  2. Set up an event-listener on the host/kiosk system, that listens for an event indicating that the OS has internet connection again. When that happens, try to run the script mentioned above.

Number one is the most likely candidate in my book, and I'd even throw in a Sails module to keep all logic in the controllers and config. sails-hook-cron (GitHub) is a very popular hook you can use to configure your cron-job in the config/cron.js file, and use it in a controller of your choosing.

Set up your application to NOT connect to Mlab automatically. Only do that in the sync-script, and the app should start without a hitch, regardless of your internet connection.

Canis
  • 4,130
  • 1
  • 23
  • 27
  • Cron is great idea! But how do I connect to Mlab from script? Is there sails-based solution? I can check for connection from sails, but I don't know any way to connect to unspecified before server in runtime – NevaDA Jan 30 '19 at 20:01
  • I've tried to find solution and there is the only link I found relevant: https://stackoverflow.com/questions/29454176/sails-js-i-want-to-add-db-connection-dynamically-after-sails-lift Still nothing... – NevaDA Jan 30 '19 at 20:09
  • @NevaDA You can use a script that calls simple mongo-tools. Mongodump and mongorestore might get the job done for you. The local DB is then the only DB sails talks to. You run frequent backups to remote dumping local data, and restoring it into the remote. I saw something about point-in-time and oplogs in these tools, so you might be able to backup only the changes since last backup, to avoid full dumps every time. See here: [dump and restore](https://docs.mlab.com/migrating/#dump-and-restore) plus [details](https://docs.mongodb.com/v3.6/tutorial/backup-and-restore-tools/#binary-bson-dumps) – Canis Jan 30 '19 at 20:25