Scripted maintenance mode



  • Is there a way to put the NodeBB forum automatically via a script into maintenance mode? Basically I would like to enable maintenance mode before doing a backup of the MongoDB. And it would be the easiest if this could be automated.

    I had a look at the write api plugin, but it doesn't seem to support this. If there isn't any possible way for this at the moment, I'll just write my own plugin. Shouldn't be too hard. As far as I can see I only need to set the config maintenanceMode to 1 or 0.


  • Admin

    Since you're on MongoDB you can execute the mongodb command to set the maintenance mode value in config.

    I'm not sure on the specifics (@baris is more familiar with it), but you put in the commands in a file and then you can run it using the mongo keyword.

    The command, by the way is:

    use myDatabase
    db.objects.update({ _key: "config" }, { $set: { "maintenanceMode": 0 } });
    

    Then you can restart NodeBB (as the value is cached and you can't alter the value of the running memory via script), and take your backup.

    A maintenance mode route in the write-api could be handier though.



  • Ignore this post, continue reading

    Oh, right. That is actually a much better idea.

    Fast and dirty simple solution would be to write two JavaScript file.

    // start-maintenance.js
    db.objects.update({ _key: "config" }, { $set: { "maintenanceMode": 1 } })
    db.objects.update({ _key: "config" }, { $set: { "maintenanceModeMessage": "Backup in progress! We are back online in a few minutes!" } })
    
    // stop-maintenance.js
    db.objects.update({ _key: "config" }, { $set: { "maintenanceMode": 0 } })
    

    And then have the backup script:

    #!/bin/sh
    mongo -u user -p pass mongo-hostname/nodebb-db-name start-maintenance.js
    mongodump -h mongo-hostname -u user -p pass --db=nodebb-db-name
    mongo -u user -p pass mongo-hostname/nodebb-db-name stop-maintenance.js
    

    I probably might do it via Python. Just so I don't need to have the username and password in the command line of mongo and mongodump.

    Thanks!



  • While showering this morning I realized that his solution won't work. Why? Because of caching. NodeBB caches the configuration. It also has an LRU cache for all objects in which this probably will stay cached as well. Thus changing the value in the database directly won't ever be read by NodeBB.

    @julian You should be aware of this! 😄


  • Global Moderator

    @dravere that's why he said to restart NodeBB after making the changes.



  • @pitaj I'm an idiot. Thank you for pointing it out. 😲

    I'll have to think about it, if I actually want to restart NodeBB just for the backup. I kind of rather have it kept running.


  • Admin

    @Dravere you can write a script that uses the db.setObject method instead of using db.objects.update the db.setObject method will clear the cache.


  • Admin

    @baris I don't think he was talking about the mongo cache though... we save the config values into Meta.config. 😕

    Probably a new util route in write-api is best course of action here.


  • Admin


  • Admin

    v5.1.6 now contains the maintenance mode util endpoint.



  • Thank you. Will have a look at it. Probably will use that plugin for some other stuff anyway so it is a great additional use for it 🙂

    I think I'll mark this topic as answered.


 

Suggested Topics

| |