Skip to content

Commit

Permalink
Merge pull request #422 from wazuh/3.7.1-7.2.1-mutex-fix
Browse files Browse the repository at this point in the history
Fix mutex
  • Loading branch information
Jesús Ángel authored Dec 14, 2018
2 parents 70dc715 + 7a0b776 commit 5ad2a8a
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 22 deletions.
71 changes: 50 additions & 21 deletions SplunkAppForWazuh/bin/db.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
from tinydb import TinyDB, Query
# sys.path.insert(0, os.path.join(os.path.dirname(__file__), "."))
from log import log
import threading


class database():
Expand All @@ -24,33 +25,61 @@ def __init__(self):
self.origin = TinyDB(os.path.dirname(os.path.abspath(__file__))+'/apilist.json')
self.db = self.origin.table('apis',cache_size=0)
self.Api = Query()
self.mutex = threading.Lock()

def insert(self, obj):
try:
result = self.db.insert(obj)
parsed_result = json.dumps({'data': result})
except Exception as e:
self.logger.error("Error inserting in DB module: %s" % (e))
raise e
return parsed_result
"""Insert a new API.
Parameters
----------
obj : dict
The new API
"""
with self.mutex:
try:
result = self.db.insert(obj)
parsed_result = json.dumps({'data': result})
except Exception as e:
self.logger.error("Error inserting in DB module: %s" % (e))
raise e
return parsed_result

def update(self, obj):
try:
self.db.update(obj, self.Api.id == obj['id'])
parsed_result = json.dumps({'data': 'success'})
except Exception as e:
self.logger.error("Error updating in DB module: %s" % (e))
raise e
return parsed_result
"""Update an already inserted API.
Parameters
----------
obj : dict
The API to edit.
"""
with self.mutex:
try:
self.db.update(obj, self.Api.id == obj['id'])
parsed_result = json.dumps({'data': 'success'})
except Exception as e:
self.logger.error("Error updating in DB module: %s" % (e))
raise e
return parsed_result

def remove(self, id):
try:
self.db.remove(self.Api.id == id)
parsed_result = json.dumps({'data': 'success'})
except Exception as e:
self.logger.error("Error removing in DB module: %s" % (e))
raise e
return parsed_result
"""Remove an API.
Parameters
----------
obj : dict
The API to be removed.
"""
with self.mutex:
try:
self.db.remove(self.Api.id == id)
parsed_result = json.dumps({'data': 'success'})
except Exception as e:
self.logger.error("Error removing in DB module: %s" % (e))
raise e
return parsed_result

def all(self):
try:
Expand Down
3 changes: 2 additions & 1 deletion SplunkAppForWazuh/bin/log.py
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,8 @@ def __init__(self):
raise e

def error(self, msg):
self.logger.error(msg)
"""Error log message."""
self.logger.error(msg, exc_info=True)

def info(self, msg):
self.logger.info(msg)
Expand Down

0 comments on commit 5ad2a8a

Please sign in to comment.