Browse Source

Improve OSX Mountain Lion "stay awake" support with native OS calls.

Also add Special option "keep_awake" for Windows and OSX.
tags/0.7.4Beta1
shypike 13 years ago
parent
commit
a40d2da2ab
  1. 2
      SABnzbd.py
  2. 35
      sabnzbd/__init__.py
  3. 1
      sabnzbd/cfg.py
  4. 2
      sabnzbd/interface.py
  5. 38
      sabnzbd/osxmenu.py

2
SABnzbd.py

@ -1574,7 +1574,7 @@ def main():
### 30 sec polling tasks
if timer > 9:
timer = 0
# Keep Windows awake (if needed)
# Keep OS awake (if needed)
sabnzbd.keep_awake()
# Restart scheduler (if needed)
scheduler.restart()

35
sabnzbd/__init__.py

@ -31,6 +31,10 @@ import subprocess
import time
import cherrypy
from threading import RLock, Lock, Condition, Thread
try:
import sleepless
except ImportError:
sleepless = None
#------------------------------------------------------------------------
# Determine platform flags
@ -345,11 +349,6 @@ def start():
logging.debug('Starting urlgrabber')
URLGrabber.do.start()
if DARWIN_ML:
logging.debug('Starting OSX keep-awake service')
osxmenu.OsxAwake()
osxmenu.OsxAwake.do.start()
@synchronized(INIT_LOCK)
def halt():
@ -409,9 +408,6 @@ def halt():
except:
logging.error('Fatal error at saving state', exc_info=True)
if DARWIN_ML:
osxmenu.OsxAwake.do.stop()
# The Scheduler cannot be stopped when the stop was scheduled.
# Since all warm-restarts have been removed, it's not longer
@ -764,16 +760,21 @@ def empty_queues():
def keep_awake():
""" If we still have work to do, keep Windows system awake
""" If we still have work to do, keep Windows/OSX system awake
"""
if KERNEL32 or DARWIN_ML:
if not sabnzbd.downloader.Downloader.do.paused:
if (not PostProcessor.do.empty()) or not NzbQueue.do.is_empty():
if KERNEL32:
# set ES_SYSTEM_REQUIRED
KERNEL32.SetThreadExecutionState(ctypes.c_int(0x00000001))
else:
osxmenu.OsxAwake.do.stay_awake = True
if KERNEL32 or sleepless:
if sabnzbd.cfg.keep_awake():
awake = False
if not sabnzbd.downloader.Downloader.do.paused:
if (not PostProcessor.do.empty()) or not NzbQueue.do.is_empty():
awake = True
if KERNEL32:
# set ES_SYSTEM_REQUIRED
KERNEL32.SetThreadExecutionState(ctypes.c_int(0x00000001))
else:
sleepless.keep_awake()
if not awake and sleepless:
sleepless.allow_sleep()
def CheckFreeSpace():

1
sabnzbd/cfg.py

@ -239,6 +239,7 @@ quota_period = OptionStr('misc', 'quota_period', 'm')
osx_menu = OptionBool('misc', 'osx_menu', True)
osx_speed = OptionBool('misc', 'osx_speed', True)
keep_awake = OptionBool('misc', 'keep_awake', True)
win_menu = OptionBool('misc', 'win_menu', True)
uniconfig = OptionBool('misc', 'uniconfig', True)
allow_incomplete_nzb = OptionBool('misc', 'allow_incomplete_nzb', False)

2
sabnzbd/interface.py

@ -1188,7 +1188,7 @@ SPECIAL_BOOL_LIST = \
'queue_complete_pers', 'api_warnings', 'allow_64bit_tools', 'par2_multicore',
'never_repair', 'allow_streaming', 'ignore_unrar_dates', 'rss_filenames',
'osx_menu', 'osx_speed', 'win_menu', 'uniconfig', 'use_pickle', 'allow_incomplete_nzb',
'randomize_server_ip', 'no_ipv6'
'randomize_server_ip', 'no_ipv6', 'keep_awake'
)
SPECIAL_VALUE_LIST = \
( 'size_limit', 'folder_max_length', 'fsys_type', 'movie_rename_limit', 'nomedia_marker',

38
sabnzbd/osxmenu.py

@ -787,41 +787,3 @@ def notify(notificationName, message):
nc = Foundation.NSDistributedNotificationCenter.defaultCenter()
nc.postNotificationName_object_(notificationName, message)
del pool
#------------------------------------------------------------------------------
class OsxAwake(Thread):
""" Keep running 'caffeinate' as long as the 'stay_awake' flag is set
"""
do = None
def __init__(self):
Thread.__init__(self)
self.stay_awake = False
self.__stop_now = False
self.__proc = None
logging.info('Starting caffeinate task')
OsxAwake.do = self
def run(self):
command = ['caffeinate', '-i', '-s', '-t300']
while not self.__stop_now:
if self.stay_awake:
self.stay_awake = False
logging.debug('Launching caffeinate')
self.__proc = subprocess.Popen(command, stdout=subprocess.PIPE, stderr=subprocess.PIPE, shell=False)
self.__proc.wait()
self.__proc = None
else:
time.sleep(30)
def stop(self):
logging.info('Stopping caffeinate task')
self.__stop_now = True
self.stay_awake = False
if self.__proc:
logging.debug('Stopping caffeinate')
try:
self.__proc.terminate()
except:
pass

Loading…
Cancel
Save