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:
parent
9650935184
commit
7c8489f170
7 changed files with 33 additions and 11 deletions
|
@ -107,7 +107,7 @@ the path to the configuration file.
|
||||||
>>> master.config
|
>>> master.config
|
||||||
controller.name = 'sample'
|
controller.name = 'sample'
|
||||||
logger.name = 'default'
|
logger.name = 'default'
|
||||||
logger.standard = 20
|
logger.standard = 30
|
||||||
scheduler.name = 'sample'
|
scheduler.name = 'sample'
|
||||||
|
|
||||||
Controllers
|
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.
|
call the master agent's callback method for entering jobs.
|
||||||
|
|
||||||
>>> from cybertools.agent.base.control import JobSpecification
|
>>> from cybertools.agent.base.control import JobSpecification
|
||||||
>>> jobSpec = JobSpecification('sample', agent='sample01')
|
>>> jobSpec = JobSpecification('sample', '00001', agent='sample01')
|
||||||
>>> master.setupJobs([jobSpec])
|
>>> master.setupJobs([jobSpec])
|
||||||
Job <...Job object ...> on agent <...SampleAgent object ...> has been executed.
|
Job 00001 on agent sample01 has been executed.
|
||||||
|
|
||||||
Logging
|
Logging
|
||||||
-------
|
-------
|
||||||
|
@ -169,3 +169,15 @@ Logging
|
||||||
<cybertools.agent.base.log.Logger object ...>
|
<cybertools.agent.base.log.Logger object ...>
|
||||||
>>> agent01.logger is master.logger
|
>>> agent01.logger is master.logger
|
||||||
True
|
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
|
||||||
|
|
|
@ -34,6 +34,7 @@ class Agent(object):
|
||||||
|
|
||||||
implements(IAgent)
|
implements(IAgent)
|
||||||
|
|
||||||
|
name = '???'
|
||||||
master = None
|
master = None
|
||||||
config = None
|
config = None
|
||||||
logger = None
|
logger = None
|
||||||
|
@ -49,6 +50,7 @@ class Agent(object):
|
||||||
|
|
||||||
class Master(Agent):
|
class Master(Agent):
|
||||||
|
|
||||||
|
name = 'master'
|
||||||
scheduler = None
|
scheduler = None
|
||||||
|
|
||||||
def __init__(self, configuration=None):
|
def __init__(self, configuration=None):
|
||||||
|
@ -69,24 +71,26 @@ class Master(Agent):
|
||||||
def setupAgents(self, agentSpecs):
|
def setupAgents(self, agentSpecs):
|
||||||
for spec in agentSpecs:
|
for spec in agentSpecs:
|
||||||
agent = agents(self, spec.type)
|
agent = agents(self, spec.type)
|
||||||
|
agent.name = spec.name
|
||||||
self.children[spec.name] = agent
|
self.children[spec.name] = agent
|
||||||
|
|
||||||
def setupJobs(self, jobSpecs):
|
def setupJobs(self, jobSpecs):
|
||||||
for spec in jobSpecs:
|
for spec in jobSpecs:
|
||||||
job = jobs(self.scheduler, spec.type)
|
job = jobs(self.scheduler, spec.type)
|
||||||
job.agent = self.children[spec.agent]
|
job.agent = self.children[spec.agent]
|
||||||
|
job.identifier = spec.identifier
|
||||||
self.scheduler.schedule(job, spec.startTime)
|
self.scheduler.schedule(job, spec.startTime)
|
||||||
|
|
||||||
|
|
||||||
class SampleAgent(Agent):
|
class SampleAgent(Agent):
|
||||||
|
|
||||||
def execute(self, job, params=None):
|
def execute(self, job, params=None):
|
||||||
msg = 'Job %s on agent %s has been executed.' % (job, self)
|
print 'Job %s on agent %s has been executed.' % (job.identifier, self.name)
|
||||||
print msg
|
self.log(job)
|
||||||
self.log(msg)
|
|
||||||
|
|
||||||
def log(self, msg):
|
def log(self, job):
|
||||||
self.logger.log(dict(msg=msg))
|
self.logger.log(dict(message='job execution', job=job.identifier,
|
||||||
|
agent=self.name))
|
||||||
|
|
||||||
|
|
||||||
agents.register(SampleAgent, Master, name='sample')
|
agents.register(SampleAgent, Master, name='sample')
|
||||||
|
|
|
@ -68,8 +68,9 @@ class JobSpecification(object):
|
||||||
|
|
||||||
startTime = None
|
startTime = None
|
||||||
|
|
||||||
def __init__(self, type, **kw):
|
def __init__(self, type, identifier, **kw):
|
||||||
self.type = type
|
self.type = type
|
||||||
|
self.identifier = identifier
|
||||||
for k, v in kw.items():
|
for k, v in kw.items():
|
||||||
setattr(self, k, v)
|
setattr(self, k, v)
|
||||||
|
|
||||||
|
|
|
@ -33,6 +33,7 @@ class Job(object):
|
||||||
|
|
||||||
implements(IScheduledJob)
|
implements(IScheduledJob)
|
||||||
|
|
||||||
|
identifier = '???'
|
||||||
agent = None
|
agent = None
|
||||||
startTime = None
|
startTime = None
|
||||||
repeat = 0
|
repeat = 0
|
||||||
|
|
|
@ -59,11 +59,12 @@ class Logger(object):
|
||||||
def __init__(self, agent):
|
def __init__(self, agent):
|
||||||
self.agent = agent
|
self.agent = agent
|
||||||
self.records = []
|
self.records = []
|
||||||
self.externalLoggers = []
|
|
||||||
self.setup()
|
self.setup()
|
||||||
|
self.externalLoggers = []
|
||||||
|
|
||||||
def setup(self):
|
def setup(self):
|
||||||
conf = self.agent.config.logger
|
conf = self.agent.config.logger
|
||||||
|
self.externalLoggers = []
|
||||||
if conf.standard:
|
if conf.standard:
|
||||||
logger = logging.getLogger()
|
logger = logging.getLogger()
|
||||||
logger.level = conf.standard
|
logger.level = conf.standard
|
||||||
|
|
|
@ -6,4 +6,4 @@
|
||||||
|
|
||||||
controller(name='sample')
|
controller(name='sample')
|
||||||
scheduler(name='sample')
|
scheduler(name='sample')
|
||||||
logger(name='default', standard=20)
|
logger(name='default', standard=30)
|
||||||
|
|
|
@ -31,6 +31,7 @@ class IAgent(Interface):
|
||||||
""" An agent waits for jobs to execute.
|
""" An agent waits for jobs to execute.
|
||||||
"""
|
"""
|
||||||
|
|
||||||
|
name = Attribute('A name identifying the agent.')
|
||||||
master = Attribute('IMaster instance.')
|
master = Attribute('IMaster instance.')
|
||||||
config = Attribute('Configuration settings.')
|
config = Attribute('Configuration settings.')
|
||||||
logger = Attribute('Logger instance to be used for recording '
|
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.
|
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.')
|
scheduler = Attribute('Scheduler that controls this job.')
|
||||||
agent = Attribute('Agent responsible for executing the job.')
|
agent = Attribute('Agent responsible for executing the job.')
|
||||||
startTime = Attribute('Date/time at which the job should be executed.')
|
startTime = Attribute('Date/time at which the job should be executed.')
|
||||||
|
|
Loading…
Add table
Reference in a new issue