divmod-users team mailing list archive
-
divmod-users team
-
Mailing list archive
-
Message #00006
Improving my Axiom-using service
Hi :)
I have a twisted service for something that uses axiom. There's a few
things about this code that trigger my "can be done better, maybe"
sense. Hopefully some Mantissa lessons learned can be applied here :)
Here's the code:
---
class Configuration(item.Item):
apiEndpoint = attributes.reference()
webEndpoint = attributes.reference()
manholeEndpoint = attributes.reference()
mailerConfiguration = attributes.reference()
class Service(service.Service):
def __init__(self, configuration):
self.configuration = configuration
self.rootStore = configuration.store
def startService(self):
service.IService(self.rootStore).startService()
self._mailerPowerup()
return self._startListening()
def _mailerPowerup(self):
mailer = self.configuration.mailerConfiguration.instantiate()
self.rootStore.inMemoryPowerUp(mailer, itxeasymail.IMailer)
def _startListening(self):
listening = []
apiFactory = auth.Factory.fromStore(self.rootStore)
endpoint = self.configuration.apiEndpoint.instantiate()
listening.append(endpoint.listen(apiFactory))
site = web.makeSite(self.rootStore, apiFactory)
endpoint = self.configuration.webEndpoint.instantiate()
listening.append(endpoint.listen(site))
manholeFactory = manhole.makeFactory(self.rootStore)
endpoint = self.configuration.manholeEndpoint.instantiate()
listening.append(endpoint.listen(manholeFactory))
return defer.gatherResults(listening)
@classmethod
def fromStore(cls, store):
"""
Finds a unique configuration in the store, and creates a service from
it, then returns that service.
"""
return cls(store.findUnique(Configuration))
---
All of the references held by the Configuration object are public in
maxims. They're basically endpoints and something composed out of
endpoints + an IUsernamePassword (mailer configuration).
1. The configuration schema changes whenever a new endpoint or service
configuration is added. That doesn't have to be an issue, but maybe it
should be written a bit more extensibly?
2. _mailerPowerup causes an in-memory powerup. That works fine, but
I'm wondering if it wouldn't be nicer if it was an actual persisted
powerup. IIUC, that would mean that txeasymail.mailer.Mailer would
have to become an axiom item, though.
3. Perhaps related to the above two: should this be rewritten so that
instead of it calling IService(store).startService(), it should *be*
called by that expression?
One potential idea here that instead of having a central configuration
object that has a bunch of endpoints, I'd have a bunch of persistable
services that each have 1 or more endpoints. I suppose the non-service
equivalent of that for IMailer would be a genuine powerup, but see (2)
for the caveats there.
thanks in advance for any comments :)
lvh