How to enable maintenance mode only when there are no active users? When there are updates available for Drupal, I enable the maintenance mode, update, and disable the maintenance mode again. 

As far as I understand it, users that were right in the middle of editing a node or checking out their shopping cart would lose their content/progress because of the maintenance break.

One solution could be to notify all online users that they should save their content, however, this would not work for users that are checking out right now (e.g., filling in the shipping data or being in the payment process).

Ideally, when trying to enable the maintenance mode, Drupal should warn me that users are currently active on the site.

Is this possible? Can Drupal detect that someone (be it a registered or an anonymous user) is "active", even if the user might have been afk/inactive for some time (e.g., searching for the credit card)? Possible indications could be: opened a node edit form but no submission or further request detected; started but not finished the checkout; added products to the cart without checking out; etc.

If yes, how could I achieve this? I’m not experienced enough to write this from scratch, so hopefully this doesn’t involve creating a totally new module. Does there exist a module for this purpose?
Solution 1 - Going Down:

Using the Going Down module (credits: J. Reynolds) could be a valid compromise (and maybe help to create a custom work around). Some more details about this module (from its project page):


  ... warns site visitors that the web site will be going down for maintenance at a specified time. The default configuration displays a fixed position red overlay message at the bottom of the browser window with text in the format 'Drupal is going offline for maintenance at Tue Feb 21 14:41:08 2012 -0800'. Module configuration is added to the Site Maintenance administration page (admin/settings/site-maintenance).


Here is a screenprint about it (also from its project page):



Only using this module will only reduce the risk of active users when you want to enable maintenance mode (though not completely reduce it).

However, by using that module, you may be able to use the Rules module to build some custom rule(s), in which you add your own custom Rules Conditions to go check if the time left for scheduled maintenance date/time is still sufficient.

Such Rules Condition would be to perform some custom PHP code, to retrieve the date/time set via the Going Down module. And then add this Rules Condition when visiting pages where you want to avoid your issue.

Solution 2 - Rules (and Message) is your friend:

Use the Rules module to create a custom rule that uses Rules Event "Drupal is initializing". Add whatever Rules Conditions you want, if any (eg: not for users with role admin). And add the appropriate Rules Action(s) to perform some type of special logging, e.g. using the Message module to create custom log data. Such log data should include things like timestamp, user id, current path, etc. If you'd use the Message module, you could create a (rather basic) view (using the Views module) to list the relevant log records that got created by this custom rule.

Enable this custom rule only X minutes (pick whatever value that fits for X) before you plan to enable maintenance mode. From then on just browse the views results to get an idea about how long ago which user/visitor has been visiting which path. For any events (log records) longer then Y minutes ago, just assume that user/session is not really doing anything on the site anymore at the moment you're browsing your Views result.