Skip to content

Commit

Permalink
Job status calculation
Browse files Browse the repository at this point in the history
  • Loading branch information
coagulant committed Oct 16, 2013
1 parent a0c9525 commit 067d33d
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 12 deletions.
14 changes: 8 additions & 6 deletions gopython3/core/models.py
Expand Up @@ -69,16 +69,18 @@ class Job(TimeFrameStampedModel):
* VCS dependencies are ignored
"""
requirements = models.TextField()
status = StatusField()
specs = models.ManyToManyField('Spec', through='Line', blank=True, null=True)

objects = JobManager()

def get_status(self):
if (self.status == 'completed' and
self.specs.filter(status__in=['pending', 'running']).count()):
return 'running'
return self.status
@property
def status(self):
statuses = set(self.specs.values_list('status', flat=True))
if TASK_STATUS.running in statuses:
return TASK_STATUS.running
if not statuses or TASK_STATUS.pending in statuses:
return TASK_STATUS.pending
return TASK_STATUS.completed

def start(self):
from .tasks import process_requirement
Expand Down
4 changes: 2 additions & 2 deletions gopython3/core/serializers.py
Expand Up @@ -15,7 +15,7 @@ class Meta:

class JobSerializer(serializers.HyperlinkedModelSerializer):
""" Custom job serializer to rename some fields"""
status = serializers.Field(source='get_status')
status = serializers.Field(source='status')

class Meta:
model = Job
Expand All @@ -25,7 +25,7 @@ class Meta:

class JobDetailSerialzier(JobSerializer):
packages = SpecSetSerializer(source='specs', many=True)
status = serializers.Field(source='get_status')
status = serializers.Field(source='status')

class Meta:
model = Job
Expand Down
7 changes: 3 additions & 4 deletions gopython3/core/tests/test_rest.py
Expand Up @@ -9,8 +9,7 @@ class TestApi(APITestCase):
maxDiff = None

def setUp(self):
self.job = JobFactory(specs=['django-model-utils==1.5.0', 'jsonfield==0.9.19'],
status='running')
self.job = JobFactory(specs=['django-model-utils==1.5.0', 'jsonfield==0.9.19'])

def test_api_root(self):
response = self.client.get('/api/v1/')
Expand All @@ -31,7 +30,7 @@ def test_jobs_list(self):
'results': [{
'id': 1,
'url': 'http://testserver/api/v1/jobs/1/',
'status': 'running',
'status': 'pending',
'created_at': self.job.created_at,
'updated_at': self.job.updated_at,
'started_at': None,
Expand All @@ -44,7 +43,7 @@ def test_job_detail(self):
self.assertDictEqual(response.data, {
"id": 1,
"url": "http://testserver/api/v1/jobs/1/",
"status": "running",
"status": "pending",
"packages": [{
"id": "django_model_utils/1.5.0",
"url": "http://testserver/api/v1/packages/django_model_utils/1.5.0/",
Expand Down
17 changes: 17 additions & 0 deletions gopython3/core/tests/test_unit.py
Expand Up @@ -50,6 +50,23 @@ def test_can_be_created_from_requirements_txt(self):
['django>=1.4,<1.5', 'Django-Geoip==0.3', 'coverage', 'coveralls>0.2'],
transform=str)

def test_status_is_calculated_based_upon_spec(self):
job = JobFactory()
assert job.status == 'pending', 'No specs yet'

job = JobFactory(specs=['foo=1,bar==2'])
assert job.status == 'pending', 'It has 2 unfinished specs'

spec = job.specs.first()
spec.status = 'running'
spec.save()
job = Job.objects.get(pk=job.pk)
assert job.status == 'running', 'Job has started, but has not finished yet'

job.specs.all().update(status='completed')
job = Job.objects.get(pk=job.pk)
assert job.status == 'completed', 'All specs have finished'


class JobSepcTest(TestCase):

Expand Down

0 comments on commit 067d33d

Please sign in to comment.