#294 Update time_to_expire when compose is finished
Merged 4 years ago by jkaluza. Opened 4 years ago by lsedlar.
lsedlar/odcs update-expiry  into  master

@@ -392,6 +392,9 @@ 

              self.time_removed = happen_on or datetime.utcnow()

          elif to_state == COMPOSE_STATES['done']:

              self.time_done = happen_on or datetime.utcnow()

+         if to_state in (COMPOSE_STATES["done"], COMPOSE_STATES["failed"]):

+             ttl = self.time_to_expire - self.time_submitted

+             self.time_to_expire = (happen_on or datetime.utcnow()) + ttl

          db.session.commit()

  

  

@@ -29,6 +29,8 @@ 

  from odcs.server.models import User

  from odcs.server.pungi import PungiSourceType

  

+ import pytest

+ 

  from .utils import ModelsBaseTest

  

  
@@ -212,3 +214,20 @@ 

          self.assertTrue(self.c1 in composes)

          self.assertTrue(self.c2 in composes)

          self.assertTrue(self.c3 not in composes)

+ 

+     def test_transition_to_done_updates_time_to_expire(self):

+         in_five_minutes = datetime.utcnow() + timedelta(seconds=300)

+         self.c1.transition(COMPOSE_STATES["done"], "it's finished", in_five_minutes)

+         # The compose should expire about 60 seconds (give or take a tenth of a

+         # second) after the time it was finished.

+         expires_in = self.c1.time_to_expire - self.c1.time_done

+         assert expires_in.total_seconds() == pytest.approx(60, abs=0.1)

+ 

+     def test_transition_to_failed_updates_time_to_expire(self):

+         now = datetime.utcnow()

+         in_five_minutes = now + timedelta(seconds=300)

+         # Fail the compose in five minutes...

+         self.c1.transition(COMPOSE_STATES["failed"], "it's finished", in_five_minutes)

+         # so that it expires in about 6 minutes

+         expires_in = self.c1.time_to_expire - now

+         assert expires_in.total_seconds() == pytest.approx(360, abs=0.1)

When a compose transitions to "done" or "failed", update the expiration time. Since we don't store the time to live for the compose, we have to compute it based on start time and current expiration time.

This should prevent composes that took long time to finish from being expired right away after finishing.

rebased onto 14cc541

4 years ago

Not sure which test is more readable. They should probably look the same though.

Pull-Request has been merged by jkaluza

4 years ago