Minor improvements #31

Merged
merged 1 commit into from Dec 14, 2016

Conversation

Projects
None yet
3 participants
Member

johnsca commented Dec 9, 2016

  • Sleep a small amount in block_until to avoid CPU-heavy busy-wait
  • Better repr of ModelEntities for debugging
  • Better errors when statuses accessed on dead units and applications
juju/application.py
@@ -41,14 +41,14 @@ def status(self):
"""Get the application status, as set by the charm's leader.
"""
- return self.data['status']['current']
+ return self.__getattr__('status')['current']
@tvansteenburgh

tvansteenburgh Dec 9, 2016

Member

Forgive my density, can you explain the benefit of this change in more detail?

@johnsca

johnsca Dec 9, 2016

Member

np, probably should have commented that. The intention is to have it run though the checks in ModelEntity.__getattr__ so that we get a more useful error if accessed on a dead entity (rather than NoneType is not subscriptable).

@tvansteenburgh

tvansteenburgh Dec 12, 2016

Member

Wouldn't self.status['current'] do the same thing?

@petevg

petevg Dec 12, 2016

Collaborator

It's a little odd to me to see you call self.__getattr__ rather than getattr(self, ..., but it's not that big of a deal. If the class is always guaranteed to have a status, I can see dropping it, per @tvansteenburgh

@tvansteenburgh

tvansteenburgh Dec 12, 2016

Member

self.status is the same as self.data['status'] (because of the __getattr__ that we have on ModelEntity. But that won't work for hyphenated names, like 'agent-status'.

My main complaints with this is that 1) it makes my eyes bleed 2) it's hard to understand why it's there unless you already know (difficult to maintain) and 3) we (or someone else) will forget to use this technique in the future.

I'd like to see us come up with a cleaner way to achieve the same result if possible.

@johnsca

johnsca Dec 12, 2016

Member

There was a naming conflict with self.status in one of the methods (which also caused an issue with using getattr()), and the others included hyphenation. I'll make something cleaner because I agree that it sucks.

Collaborator

petevg commented Dec 14, 2016

This looks a lot nicer :-) LGTM.

Member

tvansteenburgh commented Dec 14, 2016

This addresses two of my issues nicely. But...

  1. Wouldn't it be nicer to only have to call _check_alive() in one place?
  2. _check_alive() is not the best name, because ModelEntity already has alive and dead properties, and alive doesn't mean the same thing as _check_alive(). For example, say you have a ModelEntity with self.data == None. So, it's dead. In that case, entity.previous().alive == False (but the entity is actually dead in the real Juju model), but entity.previous()._check_alive() == True (because the entity at it's previous state still has data).

I would prefer to just do the None check in the data property itself, but there are some bits of code that actually want to know if that returns None, and handle it in some way.

I wonder if we should add a safe_data property that returns data, or raises DeadEntityException if it's None.

Sorry, I'm really not trying to be difficult here, I just don't feel like this is quite right yet. Definitely open to more discussion/ideas.

Member

johnsca commented Dec 14, 2016

@tvansteenburgh Updated.

juju/unit.py
@property
def agent_status_since(self):
"""Get the time when the `agent_status` was last updated.
"""
- return parse_date(self.data['agent-status']['since'])
+ return parse_date(self.self_data['agent-status']['since'])
@tvansteenburgh

tvansteenburgh Dec 14, 2016

Member

s/self_data/safe_data/

Minor improvements
* Sleep a small amount in block_until to avoid CPU-heavy busy-wait
* Better repr of ModelEntities for debugging
* Better errors when statuses accessed on dead units and applications

@tvansteenburgh tvansteenburgh merged commit ea3d50f into master Dec 14, 2016

2 checks passed

continuous-integration/travis-ci/pr The Travis CI build passed
Details
continuous-integration/travis-ci/push The Travis CI build passed
Details

@tvansteenburgh tvansteenburgh deleted the tweaks branch Dec 14, 2016

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment