How to keep Windows awake only while Madsonic in active use?

Need help? Post your questions here.
jwow313
Posts: 4
Joined: 20 Feb 2014, 01:42
Has thanked: 0
Been thanked: 0

How to keep Windows awake only while Madsonic in active use?

Unread post by jwow313 »

Hi,

I just repurposed an old Windows XP laptop as my permanent Madsonic server. I only use the server intermittently during the week, so don't want to leave the machine on 24/7 the rest of the time. I can easily set the power settings to suspend after X minutes and then wake up using wake on LAN, but I don't know how to keep the machine awake. The suspend setting is only tracking keyboard/mouse activity. Ideally, I'd like to have the machine stay awake for X minutes/hours after Madsonic has last seen activity.

I could see this being done either within Madsonic, using a free third party windows utility to trigger mouse movement in WIndows whenver Madsonic receives a request, or maybe on the windows side, keeping the machine awake whenever there is CPU activity on the Madsonic process.

Alternatively, you could go the other way and have Madsonic put the machine to sleep whenever it hasn't seen activity for X minutes/hours.

Anyone know of a way of achieving this?
madsub
Posts: 60
Joined: 04 Apr 2013, 10:35
Has thanked: 1 time
Been thanked: 19 times

Re: How to keep Windows awake only while Madsonic in active

Unread post by madsub »

I have the same situtation and had it in the past on different servers before I switched to subsonic/madsonic.
At the moment I have set the idletime to 1 h and as a workaround for longer sessions I start a parallel teamviewer-session. As long as the teamviewer-connection is acrive the server will not go to sleep.
In the past I had a look for the system of the different timers in windows and how windows detects idleless and gets to an away-mode.
Mabe these links give some hints to a madsonic how to manage this in windows, no idea how this can be done in java.

https://msdn.microsoft.com/en-us/librar ... S.85).aspx
https://msdn.microsoft.com/en-us/librar ... S.85).aspx
jwow313
Posts: 4
Joined: 20 Feb 2014, 01:42
Has thanked: 0
Been thanked: 0

Re: How to keep Windows awake only while Madsonic in active

Unread post by jwow313 »

I'm looking at custom coding a simple solution with Python.

1. Monitor CPU usage (either using Microsoft tool or 3rd party software - still investigating this) and trigger execution of a script when CPU reaches a certain level
2. Python script touches a temp file to update its modified timestamp
3. Schedule a separate Python script via task scheduler to run every 30 minutes. If the timestamp of the temp file is older than 1 hour old, put the computer into sleep mode.

I'm still investigating how to make all these moving pieces work, but if I solve it, I will share the code.

Step #1 may require paid software like https://bitsum.com/processlasso/
Step #3... I'm not sure if it's easy to put windows to sleep via Python.
jwow313
Posts: 4
Joined: 20 Feb 2014, 01:42
Has thanked: 0
Been thanked: 0

Re: How to keep Windows awake only while Madsonic in active

Unread post by jwow313 »

Well, it took several iterations, but I ended up with a pretty simple solution. I wrote a Python script that checks the Madsonic log for the last time a song was played. If it was more than 120 minutes ago, then it puts the computer to sleep.

I also added some logic with a "heartbeat" file that is touched to update the modified time every time the computer wakes up. The modified time must be at least 30 minutes ago for the computer to go to sleep. This ensures that the computer doesn't go to sleep less than 30 minutes after being woken up. This gives the user enough time to find and play at least one song.

I scheduled this to run every 30 min using Windows Scheduled Tasks.

This script was written for Windows XP. If it doesnt work on later version of Windows, it should very easily with some simple modifications to the code.

The script is written to have these files in the c:\music_scripts directory.

Hope this helps someone else out! I honestly have spent a silly amount of time on this. Maybe 4-6 hours. I had a whole other script that I wrote that was watching the Madsonic process for CPU usage, but I scrapped it after discovering the madsonic log files.

getting_sleepy.bat (this is what gets scheduled in scheduled tasks)

Code: Select all

c:\python27\python.exe c:\music_scripts\getting_sleepy_v2.py
getting_sleepy_v2.py

Code: Select all

import re
import time
import os
from time import mktime

####### GLOBAL OPTIONS

filename = 'c:\\madsonic\\madsonic.log' # location of the main madsonic log file
heartbeat_file =  'c:\music_scripts\heartbeat' # location of the heartbeat file
sleep_after = 120  # sleep if no songs played for longer than this amount of time

#######

last_play_time = ''

with open(filename, 'r') as f:
    for line in f:
        try:
            match = re.match('\[(\d{4}-\d{2}-\d{2} \d{2}:\d{2}:\d{2}).+PlayQueueInputStream', line).group(1)
            if match:
                last_play_time = time.strptime(match, '%Y-%m-%d %H:%M:%S')
        except:
            pass

curtime = time.time()
diff_min = (curtime - mktime(last_play_time)) / 60.0

print "last song played %d minutes ago." % diff_min

mtime = os.path.getmtime(heartbeat_file)
last_heartbeat = (curtime - mtime) / 60.0
print "computer was woken up %d minutes ago" % last_heartbeat

if last_heartbeat > 30 and diff_min > sleep_after:
    print "going to sleep"
    os.system('rundll32.exe powrprof.dll,SetSuspendState 0,1,0')
    time.sleep(15)
    os.system('c:\music_scripts\defibrillator.bat')
defibrillator.bat

Code: Select all

echo $null > c:\music_scripts\heartbeat
User avatar
DoCC
Contributor
Contributor
Posts: 211
Joined: 25 Feb 2014, 14:41
Has thanked: 40 times
Been thanked: 65 times

Re: How to keep Windows awake only while Madsonic in active

Unread post by DoCC »

erm in order to keep your DB in a valid state, please don'T just put the system to sleep / hibernate or whatever.
ensure a proper shutdown of the madsonic deamon and then send your system to sleep.
once your db gets bigger you will face a lot of fails and errors with a currupted db structure.
madsub
Posts: 60
Joined: 04 Apr 2013, 10:35
Has thanked: 1 time
Been thanked: 19 times

Re: How to keep Windows awake only while Madsonic in active

Unread post by madsub »

DoCC wrote:erm in order to keep your DB in a valid state, please don'T just put the system to sleep / hibernate or whatever.
ensure a proper shutdown of the madsonic deamon and then send your system to sleep.
once your db gets bigger you will face a lot of fails and errors with a currupted db structure.
Why should this happen?
I send my system to sleep and use Wake on LAN to wake it up again since I first started to use subsonic a few years ago and never noticed a corruption of the database.

By the way:
Using a system with Wake on LAN dows not not make any sense if there would not be a server running to immediately react to a call after waking up.
User avatar
DoCC
Contributor
Contributor
Posts: 211
Joined: 25 Feb 2014, 14:41
Has thanked: 40 times
Been thanked: 65 times

Re: How to keep Windows awake only while Madsonic in active

Unread post by DoCC »

i just recommend this step !
if the DB is big enough you can get in trouble. since hsqldb is only in RAM .
not every sleep standby hibernate S7 state is the same.
i just can report DB fail issues with hsqldb on 13xx GB media stuff -> DB apprx 350MB
everytime a shutdown, reboot or sleep was triggered without stopping the madsonic deamon the db crashed.
jwow313
Posts: 4
Joined: 20 Feb 2014, 01:42
Has thanked: 0
Been thanked: 0

Re: How to keep Windows awake only while Madsonic in active

Unread post by jwow313 »

hmm, interesting. this is my first time seeing any sort of thing like this as well. My experience with sleep is that threads of execution just pause and then turn back on. However, who knows exactly how Windows XP handles sleep or all of the other systems, for that matter. I would be a bit suspicious of Linux, given all the different types of kernels and such.

Thanks for your suggestion. My script is pretty easy to modify, since I just need a blocking command to shut down the service and then run the command to start the service back up on wake. I'll dig around to see if I can control the service easily from command line.
madsub
Posts: 60
Joined: 04 Apr 2013, 10:35
Has thanked: 1 time
Been thanked: 19 times

Re: How to keep Windows awake only while Madsonic in active

Unread post by madsub »

DoCC wrote:if the DB is big enough you can get in trouble. since hsqldb is only in RAM .
As long as you don't cut off power supply why should it be a problem?
i just can report DB fail issues with hsqldb on 13xx GB media stuff -> DB apprx 350MB
everytime a shutdown, reboot or sleep was triggered without stopping the madsonic deamon the db crashed.
My database is about the same and my server is automatically set to standby several times a day. So far I had no crash of the database.
User avatar
DoCC
Contributor
Contributor
Posts: 211
Joined: 25 Feb 2014, 14:41
Has thanked: 40 times
Been thanked: 65 times

Re: How to keep Windows awake only while Madsonic in active

Unread post by DoCC »

Ok. Well played xd
My servers are running 24/7 .
No MS stuff maybe the handling with RAM data onforced shutdown is different on *ix systems
Post Reply