1
0
mirror of http://aero2k.de/t/repos/urlbot-native.git synced 2017-09-06 15:25:38 +02:00

patch by braph: use decorator for config lock, drop IPC lock

This commit is contained in:
Thorsten
2016-04-03 21:20:14 +02:00
parent 89db85ad59
commit fca53d869f
5 changed files with 27 additions and 33 deletions

View File

@@ -7,6 +7,7 @@ import time
import requests import requests
from collections import namedtuple from collections import namedtuple
from urllib.error import URLError from urllib.error import URLError
import threading
RATE_NO_LIMIT = 0x00 RATE_NO_LIMIT = 0x00
RATE_GLOBAL = 0x01 RATE_GLOBAL = 0x01
@@ -43,6 +44,8 @@ buckets = {
rate_limit_classes = buckets.keys() rate_limit_classes = buckets.keys()
plugin_lock = threading.Lock()
def rate_limit(rate_class=RATE_GLOBAL): def rate_limit(rate_class=RATE_GLOBAL):
""" """
@@ -182,6 +185,23 @@ def giphy(subject, api_key):
return giphy_url return giphy_url
def config_locked(f):
"""A decorator that makes access to the config thread-safe"""
def decorate(*args, **kwargs):
plugin_lock.acquire()
try:
return f(*args, **kwargs)
except:
raise
finally:
plugin_lock.release()
return decorate
def pluginfunction(name, desc, plugin_type, ratelimit_class=RATE_GLOBAL, enabled=True): def pluginfunction(name, desc, plugin_type, ratelimit_class=RATE_GLOBAL, enabled=True):
"""A decorator to make a plugin out of a function """A decorator to make a plugin out of a function
:param enabled: :param enabled:

View File

@@ -14,7 +14,6 @@ hist_max_count = integer(default=5)
hist_max_time = integer(default=10*60) hist_max_time = integer(default=10*60)
persistent_storage = string(default='urlbot.persistent') persistent_storage = string(default='urlbot.persistent')
persistent_locked = boolean(default=false)
# the "dice" feature will use more efficient random data (0) for given users # the "dice" feature will use more efficient random data (0) for given users
enhanced-random-user = string_list(default=list()) enhanced-random-user = string_list(default=list())

View File

@@ -5,7 +5,7 @@ import traceback
import types import types
import config import config
from common import RATE_NO_LIMIT, pluginfunction, ptypes_PARSE, ptypes_COMMAND, ptypes_MUC_ONLINE, ptypes from common import RATE_NO_LIMIT, pluginfunction, config_locked, ptypes_PARSE, ptypes_COMMAND, ptypes_MUC_ONLINE, ptypes
from plugins import commands, parsers, muc_online from plugins import commands, parsers, muc_online
joblist = [] joblist = []
@@ -21,18 +21,14 @@ def plugin_enabled_get(urlbot_plugin):
return urlbot_plugin.is_enabled return urlbot_plugin.is_enabled
@config_locked
def plugin_enabled_set(plugin, enabled): def plugin_enabled_set(plugin, enabled):
if config.conf_get('persistent_locked'):
log.warn("couldn't get exclusive lock")
config.conf_set('persistent_locked', True)
if plugin.plugin_name not in config.runtime_config_store['plugins']: if plugin.plugin_name not in config.runtime_config_store['plugins']:
config.runtime_config_store['plugins'][plugin.plugin_name] = {} config.runtime_config_store['plugins'][plugin.plugin_name] = {}
config.runtime_config_store['plugins'][plugin.plugin_name]['enabled'] = enabled config.runtime_config_store['plugins'][plugin.plugin_name]['enabled'] = enabled
config.runtimeconf_persist() config.runtimeconf_persist()
config.conf_set('persistent_locked', False)
def register_active_event(t, callback, args, action_runner, plugin, msg_obj): def register_active_event(t, callback, args, action_runner, plugin, msg_obj):

View File

@@ -13,7 +13,7 @@ from lxml import etree
import config import config
from common import ( from common import (
VERSION, RATE_FUN, RATE_GLOBAL, RATE_INTERACTIVE, RATE_NO_LIMIT, VERSION, RATE_FUN, RATE_GLOBAL, RATE_INTERACTIVE, RATE_NO_LIMIT,
giphy, pluginfunction, giphy, pluginfunction, config_locked,
ptypes_COMMAND, ptypes_COMMAND,
RATE_NO_SILENCE, RATE_NO_SILENCE,
get_nick_from_object get_nick_from_object
@@ -340,6 +340,7 @@ def usersetting_get(argv, args):
@pluginfunction('set', 'modify a user setting', ptypes_COMMAND, ratelimit_class=RATE_NO_LIMIT) @pluginfunction('set', 'modify a user setting', ptypes_COMMAND, ratelimit_class=RATE_NO_LIMIT)
@config_locked
def command_usersetting(argv, **args): def command_usersetting(argv, **args):
settings = ['spoiler'] settings = ['spoiler']
arg_user = args['reply_user'] arg_user = args['reply_user']
@@ -360,20 +361,12 @@ def command_usersetting(argv, **args):
# display current value # display current value
return usersetting_get(argv, args) return usersetting_get(argv, args)
if config.conf_get('persistent_locked'):
return {
'msg': args['reply_user'] + ''': couldn't get exclusive lock'''
}
config.conf_set('persistent_locked', True)
if arg_user not in config.runtime_config_store['user_pref']: if arg_user not in config.runtime_config_store['user_pref']:
config.runtime_config_store['user_pref'][arg_user] = {} config.runtime_config_store['user_pref'][arg_user] = {}
config.runtime_config_store['user_pref'][arg_user][arg_key] = 'on' == arg_val config.runtime_config_store['user_pref'][arg_user][arg_key] = 'on' == arg_val
config.runtimeconf_persist() config.runtimeconf_persist()
config.conf_set('persistent_locked', False)
# display value written to db # display value written to db
return usersetting_get(argv, args) return usersetting_get(argv, args)
@@ -482,6 +475,7 @@ def command_show_moinlist(argv, **args):
@pluginfunction( @pluginfunction(
'record', 'record a message for a now offline user (usage: record {user} {some message};' 'record', 'record a message for a now offline user (usage: record {user} {some message};'
' {some message} == "previous" to use the last channel message)', ptypes_COMMAND) ' {some message} == "previous" to use the last channel message)', ptypes_COMMAND)
@config_locked
def command_record(argv, **args): def command_record(argv, **args):
if len(argv) < 2: if len(argv) < 2:
return { return {
@@ -497,20 +491,12 @@ def command_record(argv, **args):
else: else:
message += ' '.join(argv[1:]) message += ' '.join(argv[1:])
if config.conf_get('persistent_locked'):
return {
'msg': "%s: couldn't get exclusive lock" % args['reply_user']
}
config.conf_set('persistent_locked', True)
if target_user not in config.runtime_config_store['user_records']: if target_user not in config.runtime_config_store['user_records']:
config.runtime_config_store['user_records'][target_user] = [] config.runtime_config_store['user_records'][target_user] = []
config.runtime_config_store['user_records'][target_user].append(message) config.runtime_config_store['user_records'][target_user].append(message)
config.runtimeconf_persist() config.runtimeconf_persist()
config.conf_set('persistent_locked', False)
return { return {
'msg': '%s: message saved for %s' % (args['reply_user'], target_user) 'msg': '%s: message saved for %s' % (args['reply_user'], target_user)

View File

@@ -2,7 +2,7 @@ import logging
import config import config
from common import ( from common import (
pluginfunction, pluginfunction, config_locked,
ptypes_MUC_ONLINE ptypes_MUC_ONLINE
) )
@@ -10,6 +10,7 @@ log = logging.getLogger(__name__)
@pluginfunction('send_record', 'delivers previously saved message to user', ptypes_MUC_ONLINE) @pluginfunction('send_record', 'delivers previously saved message to user', ptypes_MUC_ONLINE)
@config_locked
def send_record(**args): def send_record(**args):
arg_user = args['reply_user'] arg_user = args['reply_user']
arg_user_key = arg_user.lower() arg_user_key = arg_user.lower()
@@ -31,15 +32,7 @@ def send_record(**args):
) )
} }
if config.conf_get('persistent_locked'):
log.warning("couldn't get exclusive lock")
return None
config.conf_set('persistent_locked', True)
user_records.pop(arg_user_key) user_records.pop(arg_user_key)
config.runtimeconf_persist() config.runtimeconf_persist()
config.conf_set('persistent_locked', False)
return response return response