Skip to content

Commit

Permalink
log daemon events
Browse files Browse the repository at this point in the history
  • Loading branch information
zmsdev committed Jun 20, 2024
1 parent 644c09b commit 05ddbcf
Show file tree
Hide file tree
Showing 3 changed files with 65 additions and 24 deletions.
1 change: 1 addition & 0 deletions Products/zms/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -303,6 +303,7 @@ def initialize(context):
fileobj.close()

# start daemon
_daemon.interval = 5 # TODO from conf
_daemon.start()

except:
Expand Down
44 changes: 33 additions & 11 deletions Products/zms/_daemon.py
Original file line number Diff line number Diff line change
@@ -1,29 +1,51 @@
# import module
################################################################################
# _daemon.py
#
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License
# as published by the Free Software Foundation; either version 2
# of the License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
################################################################################

# Imports.
from threading import *
import requests
import time
import uuid
# Product Imports.
from Products.zms import standard

stack = []
interval = 5

def process():
while True:
while stack:
item = stack.pop()
url, kwargs = item[0], item[1]
print(url, kwargs)
uid, url, kwargs = item[0], item[1], item[2]
standard.writeInfo(None, '[_daemon.process.%s]: %s'%(uid, url+'?'+'&'.join(['%s=%s'%(x,str(kwargs[x])) for x in kwargs])))
response = requests.get(url, kwargs)
print(response)
time.sleep(5)

def foo(**kwargs):
print(kwargs)
standard.writeInfo(None, '[_daemon.process.%s]: ==> %s'%(uid, str(response)))
time.sleep(interval)

def push(self, name, **kwargs):
print(self, name, kwargs)
uid = None
instack = bool(self.getConfProperty('%s.%s.async'%(self.meta_type,name),1))
if instack:
stack.append(('%s/%s'%(self.absolute_url(),name),kwargs))
return instack
uid = str(uuid.uuid4())
url = '%s/%s'%(self.absolute_url(),name)
standard.writeInfo(None, '[_daemon.push.%s]: %s'%(uid, url))
stack.append((uid, url, kwargs))
return uid

def start():
thread = Thread(target=process)
Expand Down
44 changes: 31 additions & 13 deletions Products/zms/standard.py
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,8 @@

security = ModuleSecurityInfo('Products.zms.standard')

LOGGER = logging.getLogger("ZMS.standard")

security.declarePublic('pybool')
def pybool(v):
return v in [True,'true','True',1]
Expand Down Expand Up @@ -1080,14 +1082,22 @@ def getLog(context):
"""
Get zms_log.
"""
request = context.REQUEST
if 'ZMSLOG' in request:
zms_log = request.get('ZMSLOG')
else:
zms_log = getattr(context, 'zms_log', None)
if zms_log is None:
zms_log = getattr(context.getPortalMaster(), 'zms_log', None)
request.set('ZMSLOG', zms_log)
zms_log = None
if context is not None:
request = getattr(context, 'REQUEST', None)
if request and 'ZMSLOG' in request:
zms_log = request.get('ZMSLOG')
else:
zms_log = getattr(context, 'zms_log', None)
if zms_log is None:
zms_log = getattr(context.getPortalMaster(), 'zms_log', None)
if request:
request.set('ZMSLOG', zms_log)
if zms_log is None:
class DefaulLog(object):
def hasSeverity(self, severity): return True
def LOG(self, severity, message): LOGGER.log( severity, message)
zms_log = DefaulLog()
return zms_log

security.declarePublic('writeStdout')
Expand Down Expand Up @@ -1115,14 +1125,20 @@ def writeLog(context, info):
zms_log = getLog(context)
severity = logging.DEBUG
if zms_log.hasSeverity(severity):
info = "[%s@%s] "%(context.meta_id, '/'.join(context.getPhysicalPath())) + info
if context:
info = "[%s@%s] "%(context.meta_id, '/'.join(context.getPhysicalPath())) + info
zms_log.LOG( severity, info)
except:
pass
return info

security.declarePublic('writeBlock')
def writeBlock(context, info):
def writeBlock(context=None, info='?'):
return writeInfo(context, info)


security.declarePublic('writeInfo')
def writeInfo(context=None, info='?'):
"""
Log information.
@param info: Information
Expand All @@ -1135,14 +1151,15 @@ def writeBlock(context, info):
zms_log = getLog(context)
severity = logging.INFO
if zms_log.hasSeverity(severity):
info = "[%s@%s] "%(context.meta_id, '/'.join(context.getPhysicalPath())) + info
if context:
info = "[%s@%s] "%(context.meta_id, '/'.join(context.getPhysicalPath())) + info
zms_log.LOG( severity, info)
except:
pass
return info

security.declarePublic('writeError')
def writeError(context, info):
def writeError(context=None, info='?'):
"""
Log error.
@param info: Information
Expand All @@ -1154,7 +1171,8 @@ def writeError(context, info):
info = info.decode('utf-8')
info += '\n'.join(traceback.format_tb(tb))
try:
info = "[%s@%s] "%(context.meta_id, '/'.join(context.getPhysicalPath())) + info
if context:
info = "[%s@%s] "%(context.meta_id, '/'.join(context.getPhysicalPath())) + info
zms_log = getLog(context)
severity = logging.ERROR
if zms_log.hasSeverity(severity):
Expand Down

0 comments on commit 05ddbcf

Please sign in to comment.