completed sample implementation (without Twisted), with simple logging

git-svn-id: svn://svn.cy55.de/Zope3/src/cybertools/trunk@2420 fd906abe-77d9-0310-91a1-e0d9ade77398
This commit is contained in:
helmutm 2008-02-25 14:12:37 +00:00
parent 9650935184
commit 7c8489f170
7 changed files with 33 additions and 11 deletions

View file

@ -107,7 +107,7 @@ the path to the configuration file.
>>> master.config
controller.name = 'sample'
logger.name = 'default'
logger.standard = 20
logger.standard = 30
scheduler.name = 'sample'
Controllers
@ -158,9 +158,9 @@ We schedule a sample job by taking the role of the controller and simply
call the master agent's callback method for entering jobs.
>>> from cybertools.agent.base.control import JobSpecification
>>> jobSpec = JobSpecification('sample', agent='sample01')
>>> jobSpec = JobSpecification('sample', '00001', agent='sample01')
>>> master.setupJobs([jobSpec])
Job <...Job object ...> on agent <...SampleAgent object ...> has been executed.
Job 00001 on agent sample01 has been executed.
Logging
-------
@ -169,3 +169,15 @@ Logging
<cybertools.agent.base.log.Logger object ...>
>>> agent01.logger is master.logger
True
>>> master.config.logger.standard = 20
>>> master.logger.setup()
>>> jobSpec = JobSpecification('sample', '00002', agent='sample01')
>>> master.setupJobs([jobSpec])
Job 00002 on agent sample01 has been executed.
2... agent:sample01 job:00002 message:job execution
>>> for r in master.logger.records:
... print r
2... agent:sample01 job:00001 message:job execution
2... agent:sample01 job:00002 message:job execution

View file

@ -34,6 +34,7 @@ class Agent(object):
implements(IAgent)
name = '???'
master = None
config = None
logger = None
@ -49,6 +50,7 @@ class Agent(object):
class Master(Agent):
name = 'master'
scheduler = None
def __init__(self, configuration=None):
@ -69,24 +71,26 @@ class Master(Agent):
def setupAgents(self, agentSpecs):
for spec in agentSpecs:
agent = agents(self, spec.type)
agent.name = spec.name
self.children[spec.name] = agent
def setupJobs(self, jobSpecs):
for spec in jobSpecs:
job = jobs(self.scheduler, spec.type)
job.agent = self.children[spec.agent]
job.identifier = spec.identifier
self.scheduler.schedule(job, spec.startTime)
class SampleAgent(Agent):
def execute(self, job, params=None):
msg = 'Job %s on agent %s has been executed.' % (job, self)
print msg
self.log(msg)
print 'Job %s on agent %s has been executed.' % (job.identifier, self.name)
self.log(job)
def log(self, msg):
self.logger.log(dict(msg=msg))
def log(self, job):
self.logger.log(dict(message='job execution', job=job.identifier,
agent=self.name))
agents.register(SampleAgent, Master, name='sample')

View file

@ -68,8 +68,9 @@ class JobSpecification(object):
startTime = None
def __init__(self, type, **kw):
def __init__(self, type, identifier, **kw):
self.type = type
self.identifier = identifier
for k, v in kw.items():
setattr(self, k, v)

View file

@ -33,6 +33,7 @@ class Job(object):
implements(IScheduledJob)
identifier = '???'
agent = None
startTime = None
repeat = 0

View file

@ -59,11 +59,12 @@ class Logger(object):
def __init__(self, agent):
self.agent = agent
self.records = []
self.externalLoggers = []
self.setup()
self.externalLoggers = []
def setup(self):
conf = self.agent.config.logger
self.externalLoggers = []
if conf.standard:
logger = logging.getLogger()
logger.level = conf.standard

View file

@ -6,4 +6,4 @@
controller(name='sample')
scheduler(name='sample')
logger(name='default', standard=20)
logger(name='default', standard=30)

View file

@ -31,6 +31,7 @@ class IAgent(Interface):
""" An agent waits for jobs to execute.
"""
name = Attribute('A name identifying the agent.')
master = Attribute('IMaster instance.')
config = Attribute('Configuration settings.')
logger = Attribute('Logger instance to be used for recording '
@ -154,6 +155,8 @@ class IScheduledJob(Interface):
a predefined date and time - this is the basic job interface.
"""
identifier = Attribute('A name/ID unique within the realm of the '
'controller.')
scheduler = Attribute('Scheduler that controls this job.')
agent = Attribute('Agent responsible for executing the job.')
startTime = Attribute('Date/time at which the job should be executed.')