hydrus/include/ServerDaemons.py

113 lines
3.5 KiB
Python
Raw Normal View History

2015-03-04 22:44:32 +00:00
import collections
import hashlib
import httplib
import HydrusConstants as HC
import HydrusExceptions
import HydrusFileHandling
import HydrusNATPunch
import HydrusServer
import itertools
import os
import Queue
import random
2015-03-18 21:46:29 +00:00
import ServerFiles
2015-03-04 22:44:32 +00:00
import shutil
import sqlite3
import sys
import threading
import time
import traceback
import yaml
2015-03-25 22:04:19 +00:00
import HydrusData
import HydrusGlobals
2015-03-04 22:44:32 +00:00
2015-08-26 21:18:39 +00:00
def DAEMONCheckDataUsage(): HydrusGlobals.controller.WriteSynchronous( 'check_data_usage' )
2015-03-04 22:44:32 +00:00
2015-08-26 21:18:39 +00:00
def DAEMONCheckMonthlyData(): HydrusGlobals.controller.WriteSynchronous( 'check_monthly_data' )
2015-03-04 22:44:32 +00:00
2015-08-26 21:18:39 +00:00
def DAEMONClearBans(): HydrusGlobals.controller.WriteSynchronous( 'clear_bans' )
2015-03-04 22:44:32 +00:00
2015-08-26 21:18:39 +00:00
def DAEMONDeleteOrphans(): HydrusGlobals.controller.WriteSynchronous( 'delete_orphans' )
2015-03-04 22:44:32 +00:00
2015-08-26 21:18:39 +00:00
def DAEMONFlushRequestsMade( all_requests ): HydrusGlobals.controller.WriteSynchronous( 'flush_requests_made', all_requests )
2015-03-04 22:44:32 +00:00
def DAEMONGenerateUpdates():
2015-08-26 21:18:39 +00:00
dirty_updates = HydrusGlobals.controller.Read( 'dirty_updates' )
2015-03-04 22:44:32 +00:00
for ( service_key, tuples ) in dirty_updates.items():
2015-06-03 21:05:13 +00:00
for ( begin, end ) in tuples:
2015-08-26 21:18:39 +00:00
HydrusGlobals.controller.WriteSynchronous( 'clean_update', service_key, begin, end )
2015-06-03 21:05:13 +00:00
2015-03-04 22:44:32 +00:00
2015-08-26 21:18:39 +00:00
update_ends = HydrusGlobals.controller.Read( 'update_ends' )
2015-03-04 22:44:32 +00:00
for ( service_key, biggest_end ) in update_ends.items():
2015-03-25 22:04:19 +00:00
now = HydrusData.GetNow()
2015-03-04 22:44:32 +00:00
next_begin = biggest_end + 1
next_end = biggest_end + HC.UPDATE_DURATION
while next_end < now:
2015-08-26 21:18:39 +00:00
HydrusGlobals.controller.WriteSynchronous( 'create_update', service_key, next_begin, next_end )
2015-03-04 22:44:32 +00:00
biggest_end = next_end
2015-03-25 22:04:19 +00:00
now = HydrusData.GetNow()
2015-03-04 22:44:32 +00:00
next_begin = biggest_end + 1
next_end = biggest_end + HC.UPDATE_DURATION
def DAEMONUPnP():
try:
local_ip = HydrusNATPunch.GetLocalIP()
current_mappings = HydrusNATPunch.GetUPnPMappings()
our_mappings = { ( internal_client, internal_port ) : external_port for ( description, internal_client, internal_port, external_ip_address, external_port, protocol, enabled ) in current_mappings }
except: return # This IGD probably doesn't support UPnP, so don't spam the user with errors they can't fix!
2015-08-26 21:18:39 +00:00
services_info = HydrusGlobals.controller.Read( 'services_info' )
2015-03-04 22:44:32 +00:00
for ( service_key, service_type, options ) in services_info:
internal_port = options[ 'port' ]
upnp = options[ 'upnp' ]
if ( local_ip, internal_port ) in our_mappings:
current_external_port = our_mappings[ ( local_ip, internal_port ) ]
if current_external_port != upnp: HydrusNATPunch.RemoveUPnPMapping( current_external_port, 'TCP' )
for ( service_key, service_type, options ) in services_info:
internal_port = options[ 'port' ]
upnp = options[ 'upnp' ]
if upnp is not None and ( local_ip, internal_port ) not in our_mappings:
external_port = upnp
protocol = 'TCP'
description = HC.service_string_lookup[ service_type ] + ' at ' + local_ip + ':' + str( internal_port )
duration = 3600
HydrusNATPunch.AddUPnPMapping( local_ip, internal_port, external_port, protocol, description, duration = duration )