-
Notifications
You must be signed in to change notification settings - Fork 70
/
task_lifecycle.py
1210 lines (1032 loc) · 43.7 KB
/
task_lifecycle.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
import logging
import random
from pydoc import locate
from django.conf import settings
from django.db import connection
from django.db import transaction
from django.db.models import Case
from django.db.models import When
from django.db.models import Q
from django.db.models import Value
from django.db.models import IntegerField
from django.utils import timezone
from orchestra.communication.slack import add_worker_to_project_team
from orchestra.communication.slack import archive_project_slack_group
from orchestra.communication.utils import mark_worker_as_winner
from orchestra.core.errors import AssignmentPolicyError
from orchestra.core.errors import CreationPolicyError
from orchestra.core.errors import IllegalTaskSubmission
from orchestra.core.errors import ReviewPolicyError
from orchestra.core.errors import TaskAssignmentError
from orchestra.core.errors import TaskDependencyError
from orchestra.core.errors import TaskStatusError
from orchestra.core.errors import ProjectStatusError
from orchestra.core.errors import WorkerCertificationError
from orchestra.models import Iteration
from orchestra.models import Project
from orchestra.models import Task
from orchestra.models import Todo
from orchestra.models import TaskAssignment
from orchestra.models import Worker
from orchestra.models import WorkerCertification
from orchestra.todos.api import add_todolist_template
from orchestra.utils.notifications import notify_status_change
from orchestra.utils.notifications import notify_project_status_change
from orchestra.utils.task_properties import assignment_history
from orchestra.utils.task_properties import current_assignment
from orchestra.utils.task_properties import get_latest_iteration
logger = logging.getLogger(__name__)
class AssignmentPolicyType(object):
ENTRY_LEVEL = 'entry_level'
REVIEWER = 'reviewer'
def worker_assigned_to_max_tasks(worker):
"""
Check whether worker is assigned to the maximum number of tasks.
Args:
worker (orchestra.models.Worker):
The specified worker object.
Returns:
assigned_to_max_tasks (bool):
True if worker is assigned to the maximum number of tasks.
"""
assigned_to_max_tasks = False
return assigned_to_max_tasks
def worker_assigned_to_rejected_task(worker):
"""
Check whether worker is assigned to a task that has been rejected.
Args:
worker (orchestra.models.Worker):
The specified worker object.
Returns:
assigned_to_rejected_task (bool):
True if worker is assigned to a task that has been rejected.
"""
assigned_to_rejected_task = (
TaskAssignment.objects
.filter(worker=worker,
status=TaskAssignment.Status.PROCESSING,
task__status=Task.Status.POST_REVIEW_PROCESSING)
.exists()
)
return assigned_to_rejected_task
def worker_has_reviewer_status(worker,
task_class=WorkerCertification.TaskClass.REAL):
"""
Check whether worker is a reviewer for any certification for a
given task class.
Args:
worker (orchestra.models.Worker):
The specified worker object.
task_class (orchestra.models.WorkerCertification.TaskClass):
The specified task class.
Returns:
has_reviwer_status (bool):
True if worker is a reviewer for any certification for a
given task class.
"""
has_reviwer_status = (
WorkerCertification.objects
.filter(worker=worker,
role=WorkerCertification.Role.REVIEWER,
task_class=task_class)
.exists())
return has_reviwer_status
def is_worker_certified_for_task(
worker, task, role, task_class=WorkerCertification.TaskClass.REAL,
require_staffbot_enabled=False):
"""
Check whether worker is certified for a given task, role, and task
class.
Args:
worker (orchestra.models.Worker):
The specified worker object.
task (orchestra.models.Task):
The specified task object.
task_class (orchestra.models.WorkerCertification.TaskClass):
The specified task class.
require_staffbot_enabled (bool):
Whether to require that the `staffbot_enabled` flag be `True`.
Returns:
certified_for_task (bool):
True if worker is certified for a given task, role, and task
class.
"""
step = task.step
worker_certifications = (
WorkerCertification
.objects
.filter(worker=worker,
role=role,
task_class=task_class,
certification__in=step.required_certifications.all()))
if require_staffbot_enabled:
worker_certifications = worker_certifications.filter(
staffbot_enabled=True)
certified_for_task = (
step.required_certifications.count() == worker_certifications.count())
return certified_for_task
def get_role_from_counter(role_counter):
if role_counter == 0:
return WorkerCertification.Role.ENTRY_LEVEL
return WorkerCertification.Role.REVIEWER
def role_counter_required_for_new_task(task):
"""
Given a task find a level of expert that is required
to be assigned for task.
"""
if task.status not in [Task.Status.AWAITING_PROCESSING,
Task.Status.PENDING_REVIEW]:
raise TaskAssignmentError('Status incompatible with new assignment')
elif (task.status == Task.Status.AWAITING_PROCESSING and
task.assignments.filter(assignment_counter=0).exists()):
raise TaskAssignmentError('Task is in incorrect state')
assignments_count = task.assignments.count()
for assignment_counter in range(assignments_count):
task_assignment = (task.assignments
.filter(assignment_counter=assignment_counter))
if not task_assignment.exists():
return assignment_counter
return assignments_count
def role_required_for_new_task(task):
roles = {
Task.Status.AWAITING_PROCESSING: WorkerCertification.Role.ENTRY_LEVEL,
Task.Status.PENDING_REVIEW: WorkerCertification.Role.REVIEWER
}
required_role = roles.get(task.status)
if required_role is None:
raise TaskAssignmentError('Status incompatible with new assignment')
return required_role
@transaction.atomic
def assign_task(worker_id, task_id, staffing_request_inquiry=None):
"""
Return a given task after assigning or reassigning it to the specified
worker.
Args:
worker_id (int):
The ID of the worker to be assigned.
task_id (int):
The ID of the task to be assigned.
Returns:
task (orchestra.models.Task):
The newly assigned task.
Raises:
orchestra.core.errors.TaskAssignmentError:
The specified worker is already assigned to the given task
or the task status is not compatible with new assignment.
orchestra.core.errors.WorkerCertificationError:
The specified worker is not certified for the given task.
"""
worker = Worker.objects.get(id=worker_id)
task = Task.objects.get(id=task_id)
required_role = role_required_for_new_task(task)
required_role_counter = role_counter_required_for_new_task(task)
assignment = current_assignment(task)
if not is_worker_certified_for_task(worker, task, required_role):
raise WorkerCertificationError('Worker not certified for this task.')
if task.is_worker_assigned(worker):
raise TaskAssignmentError('Worker already assigned to this task.')
assignment_counter = task.assignments.count()
in_progress_task_data = {}
if assignment:
# In-progress task data is the latest
# submission by a previous worker
in_progress_task_data = assignment.in_progress_task_data
previous_status = task.status
if previous_status == Task.Status.AWAITING_PROCESSING:
task.status = Task.Status.PROCESSING
elif previous_status == Task.Status.PENDING_REVIEW:
task.status = Task.Status.REVIEWING
task.save()
assignment = (
TaskAssignment.objects
.create(worker=worker,
task=task,
status=TaskAssignment.Status.PROCESSING,
assignment_counter=assignment_counter,
in_progress_task_data=in_progress_task_data))
Iteration.objects.create(
assignment=assignment,
start_datetime=assignment.start_datetime)
add_worker_to_project_team(worker, task.project)
notify_status_change(task, previous_status, staffing_request_inquiry)
mark_worker_as_winner(worker, task, required_role_counter,
staffing_request_inquiry)
return task
@transaction.atomic
def reassign_assignment(worker_id, assignment_id,
staffing_request_inquiry=None):
"""
Return a given assignment after reassigning it to the specified
worker.
Args:
worker_id (int):
The ID of the worker to be assigned.
assignment_id (int):
The ID of the assignment to be assigned.
Returns:
assignment (orchestra.models.TaskAssignment):
The newly assigned assignment.
Raises:
orchestra.core.errors.TaskAssignmentError:
The specified worker is already assigned to the given task.
orchestra.core.errors.WorkerCertificationError:
The specified worker is not certified for the given assignment.
"""
worker = Worker.objects.get(id=worker_id)
assignment = TaskAssignment.objects.get(id=assignment_id)
if assignment.assignment_counter > 0:
role = WorkerCertification.Role.REVIEWER
else:
role = WorkerCertification.Role.ENTRY_LEVEL
if not is_worker_certified_for_task(worker, assignment.task, role):
raise WorkerCertificationError(
'Worker not certified for this assignment.')
if assignment.task.is_worker_assigned(worker):
raise TaskAssignmentError('Worker already assigned to this task.')
assignment.worker = worker
assignment.save()
mark_worker_as_winner(worker, assignment.task,
assignment.assignment_counter,
staffing_request_inquiry)
add_worker_to_project_team(worker, assignment.task.project)
return assignment
@transaction.atomic
def complete_and_skip_task(task_id):
"""
Submits a task on behalf of the worker working on it. If the task
isn't assigned to a worker, marks it and its assignments as
complete and creates subsequent tasks.
Args:
task_id (int):
The ID of the task to be marked as complete and skipped.
Returns:
task (orchestra.models.Task):
The completed and skipped task.
"""
task = Task.objects.get(id=task_id)
assignment = current_assignment(task)
if assignment and assignment.worker:
if assignment.status != TaskAssignment.Status.PROCESSING:
return task
task_data = assignment.in_progress_task_data or {}
task_data.update(_orchestra_internal={'complete_and_skip_task': True})
submit_task(
task_id, task_data,
Iteration.Status.REQUESTED_REVIEW, assignment.worker)
else:
task.status = Task.Status.COMPLETE
task.save()
for assignment in task.assignments.all():
assignment.status = TaskAssignment.Status.SUBMITTED
assignment.save()
return task
def get_task_details(task_id):
"""
Return various information about the specified task.
Args:
task_id (int): The ID of the desired task.
Returns:
task_details (dict): Information about the specified task.
"""
task = Task.objects.select_related('step__workflow_version__workflow').get(
id=task_id)
step = task.step
project = task.project
workflow_version = step.workflow_version
workflow = workflow_version.workflow
prerequisites = get_previously_completed_task_data(step, project)
task_details = {
'workflow': {
'slug': workflow.slug,
'name': workflow.name,
},
'workflow_version': {
'slug': workflow_version.slug,
'name': workflow_version.name,
},
'step': {
'slug': step.slug,
'name': step.name
},
'task_id': task.id,
'project': {
'id': project.id,
'details': project.short_description,
'scratchpad_url': project.scratchpad_url,
'project_data': project.project_data,
'status': dict(Project.STATUS_CHOICES)[project.status]
},
'prerequisites': prerequisites
}
return task_details
def get_task_assignment_details(task_assignment):
"""
Return various information about the specified task assignment.
Args:
task_assignment (orchestra.models.TaskAssignment):
The specified task assignment.
Returns:
task_assignment_details (dict):
Information about the specified task assignment.
"""
reviewer_task_assignment = (
TaskAssignment.objects.filter(
task=task_assignment.task)
.order_by('-assignment_counter')[0])
worker = task_assignment.worker
worker_info = {attr: getattr(getattr(worker, 'user', None), attr, None)
for attr in ('username', 'first_name', 'last_name')}
return {
'assignment_id': task_assignment.id,
'task': {
'data': task_assignment.in_progress_task_data,
'status': (dict(Task.STATUS_CHOICES)
[task_assignment.task.status])
},
'worker': worker_info,
'status': (dict(TaskAssignment.STATUS_CHOICES)
[task_assignment.status]),
'is_reviewer': (
task_assignment.id == reviewer_task_assignment.id and
task_assignment.assignment_counter > 0),
'is_read_only': (
task_assignment.status != TaskAssignment.Status.PROCESSING),
}
def get_task_overview_for_worker(task_id, worker):
"""
Get information about `task` and its assignment for `worker`.
Args:
task_id (int):
The ID of the desired task object.
worker (orchestra.models.Worker):
The specified worker object.
Returns:
task_assignment_details (dict):
Information about `task` and its assignment for `worker`.
"""
task = Task.objects.get(id=task_id)
task_details = get_task_details(task_id)
task_details['is_project_admin'] = worker.is_project_admin()
if task.is_worker_assigned(worker):
task_assignment = TaskAssignment.objects.get(worker=worker, task=task)
elif worker.is_project_admin():
task_assignment = assignment_history(task).last()
task_details['is_read_only'] = True
else:
raise TaskAssignmentError('Worker is not associated with task')
task_assignment_details = get_task_assignment_details(task_assignment)
task_assignment_details.update(task_details)
return task_assignment_details
def tasks_assigned_to_worker(worker):
"""
Get all the tasks associated with `worker`.
Args:
worker (orchestra.models.Worker):
The specified worker object.
Returns:
tasks_assigned (dict):
A dict with information about the worker's tasks, used in
displaying the Orchestra dashboard.
"""
valid_task_assignments = TaskAssignment.objects.exclude(
task__status=Task.Status.ABORTED)
# get all active task assignments for a user
active_task_assignments = (
valid_task_assignments
.filter(
worker=worker,
status=TaskAssignment.Status.PROCESSING)
.exclude(
task__project__status__in=[Project.Status.PAUSED,
Project.Status.COMPLETED])
.order_by('-task__project__priority',
'task__project__start_datetime'))
inactive_task_assignments = (
valid_task_assignments
.filter(
worker=worker,
status=TaskAssignment.Status.SUBMITTED
)
.exclude(
task__status=Task.Status.COMPLETE)
.exclude(
task__project__status__in=[Project.Status.PAUSED,
Project.Status.COMPLETED])
.order_by('-task__project__priority',
'task__project__start_datetime'))
inactive_processing_task_assignments = []
inactive_review_task_assignments = []
for task_assignment in inactive_task_assignments:
if (
valid_task_assignments
.filter(
status=TaskAssignment.Status.PROCESSING,
task__id=task_assignment.task.id,
assignment_counter__lt=task_assignment.assignment_counter)
.exists()):
inactive_processing_task_assignments.append(task_assignment)
else:
inactive_review_task_assignments.append(task_assignment)
# TODO(marcua): Do a better job of paginating than cutting off to the most
# recent 200 tasks.
complete_task_assignments = (
valid_task_assignments
.filter(Q(worker=worker) &
(Q(task__status=Task.Status.COMPLETE) |
Q(task__project__status=Project.Status.COMPLETED)))
.order_by('-task__project__priority',
'-task__project__start_datetime')[:200])
paused_task_assignments = (
valid_task_assignments
.filter(
worker=worker,
status=TaskAssignment.Status.PROCESSING,
task__project__status=Project.Status.PAUSED)
.order_by('-task__project__priority',
'task__project__start_datetime'))
task_assignments_overview = {
'returned': (
active_task_assignments
.filter(task__status=Task.Status.POST_REVIEW_PROCESSING)),
'in_progress': (
active_task_assignments
.exclude(task__status=Task.Status.POST_REVIEW_PROCESSING)),
'pending_review': inactive_review_task_assignments,
'pending_processing': inactive_processing_task_assignments,
'paused': paused_task_assignments,
'complete': complete_task_assignments}
tasks_assigned = []
time_now = timezone.now()
pending_todos_filter = Q(status=Todo.Status.PENDING.value)
non_template_todo_filter = Q(template__isnull=True)
no_section_todo_filter = Q(section__isnull=True) | Q(section='')
for state, task_assignments in iter(task_assignments_overview.items()):
for task_assignment in task_assignments:
step = task_assignment.task.step
workflow_version = step.workflow_version
next_todo_title = None
next_todo_dict = {}
should_be_active = False
if state in ('returned', 'in_progress', 'paused'):
# TODO(aditya): Temporarily we are filtering out todos
# with section values. Remove this comment once we
# figure out a long term logic.
next_todo = (
task_assignment.task.todos
.filter(
pending_todos_filter &
non_template_todo_filter &
no_section_todo_filter
).annotate(
todo_order=Case(
When(
start_by_datetime__gt=time_now,
then=Value(3)),
When(
due_datetime=None,
then=Value(2)),
default=Value(1),
output_field=IntegerField()
)
)
.order_by(
'todo_order',
'due_datetime',
'start_by_datetime',
'-created_at'
)
.first())
if next_todo:
next_todo_title = next_todo.title
start_str = (
next_todo.start_by_datetime.strftime(
'%Y-%m-%dT%H:%M:%SZ'
) if next_todo.start_by_datetime else ''
)
due_str = (
next_todo.due_datetime.strftime(
'%Y-%m-%dT%H:%M:%SZ'
) if next_todo.due_datetime else ''
)
next_todo_dict = {
'title': next_todo.title,
'start_by_datetime': start_str,
'due_datetime': due_str
}
# TODO(aditya): Temporarily we are filtering out todos
# with section values. Remove this comment once we
# figure out a long term logic.
num_non_template_todos = (
task_assignment.task.todos
.filter(
non_template_todo_filter &
no_section_todo_filter
).count())
# If a task has no todos (complete or incomplete)
# assigned to it, then by default the task would be
# marked as pending. When a task is first created and
# picked up by a worker, it will thus be in pending
# state, which is confusing behavior. We thus treat a
# task with zero todos as active. After a task has one
# or more todos assigned to it, its active/pending
# state is determined by the presence of incomplete
# todos.
task_started = (
next_todo_title is not None
and (
next_todo.start_by_datetime is None
or next_todo.start_by_datetime <= time_now
)
)
should_be_active = (
state != 'paused' and
((num_non_template_todos == 0)
or task_started))
tasks_assigned.append({
'id': task_assignment.task.id,
'assignment_id': task_assignment.id,
'step': step.name,
'project': workflow_version.name,
'detail': task_assignment.task.project.short_description,
'priority': task_assignment.task.project.priority,
'state': state,
'assignment_start_datetime': task_assignment.start_datetime,
'next_todo_dict': next_todo_dict,
'should_be_active': should_be_active,
'tags': task_assignment.task.tags.get('tags', [])
})
return tasks_assigned
def _is_review_needed(task):
"""
Determine if `task` will be reviewed according to its step policy.
Args:
task (orchestra.models.Task):
The specified task object.
Returns:
review_needed (bool):
True if review is determined to be needed according to the
task's step policy.
Raises:
orchestra.core.errors.ReviewPolicyError:
The specified review policy type is not supported.
"""
policy_dict = task.step.review_policy
policy = policy_dict.get('policy')
sample_rate = policy_dict.get('rate')
max_reviews = policy_dict.get('max_reviews')
if (policy == 'sampled_review' and
sample_rate is not None and
max_reviews is not None):
task_assignment_count = task.assignments.all().count()
if max_reviews <= task_assignment_count - 1:
return False
return random.random() < sample_rate
elif policy == 'no_review':
return False
else:
raise ReviewPolicyError('Review policy incorrectly specified.')
def get_next_task_status(task, iteration_status):
"""
Given current task status and snapshot type provide new task status.
If the second level reviewer rejects a task then initial reviewer
cannot reject it further down, but must fix and submit the task.
Args:
task (orchestra.models.Task):
The specified task object.
iteration_status (orchestra.models.Iteration.Status):
The action taken upon task submission (i.e., REQUESTED_REVIEW
or PROVIDED_REVIEW).
Returns:
next_status (orchestra.models.Task.Status):
The next status of `task`, once the `iteration_status` action
has been completed.
Raises:
orchestra.core.errors.IllegalTaskSubmission:
The `iteration_status` action cannot be taken for the task in
its current status.
"""
# Task with one of these statuses cannot be submitted
illegal_statuses = [Task.Status.AWAITING_PROCESSING,
Task.Status.PENDING_REVIEW,
Task.Status.COMPLETE]
if task.status in illegal_statuses:
raise IllegalTaskSubmission('Task has illegal status for submit.')
elif iteration_status == Iteration.Status.REQUESTED_REVIEW:
if task.status in (Task.Status.PROCESSING, Task.Status.REVIEWING):
if _is_review_needed(task):
return Task.Status.PENDING_REVIEW
else:
return Task.Status.COMPLETE
elif task.status == Task.Status.POST_REVIEW_PROCESSING:
return Task.Status.REVIEWING
else:
raise IllegalTaskSubmission('Task not in submittable state.')
elif iteration_status == Iteration.Status.PROVIDED_REVIEW:
if task.status == Task.Status.REVIEWING:
return Task.Status.POST_REVIEW_PROCESSING
else:
raise IllegalTaskSubmission('Task not in rejectable state.')
def check_worker_allowed_new_assignment(worker):
"""
Check if the worker can be assigned to a new task.
Args:
worker (orchestra.models.Worker):
The worker submitting the task.
task_status (orchestra.models.Task.Status):
The status of the desired new task assignment.
Returns:
allowed_new_assignment (bool):
True if the worker can be assigned to a new task.
Raises:
orchestra.core.errors.TaskAssignmentError:
Worker has pending reviewer feedback or is assigned to the
maximum number of tasks.
orchestra.core.errors.TaskStatusError:
New task assignment is not permitted for the given status.
"""
if (worker_assigned_to_rejected_task(worker) and
settings.ORCHESTRA_ENFORCE_NO_NEW_TASKS_DURING_REVIEW):
raise TaskAssignmentError('Worker has pending reviewer feedback that '
'must be addressed.')
elif worker_assigned_to_max_tasks(worker):
raise TaskAssignmentError('Worker assigned to max number of tasks.')
def assert_new_task_status_valid(task_status):
valid_statuses = [Task.Status.AWAITING_PROCESSING,
Task.Status.PENDING_REVIEW]
if task_status not in valid_statuses:
raise TaskStatusError('Invalid status for new task assignment.')
@transaction.atomic
def save_task(task_id, task_data, worker):
"""
Save the latest data to the database for a task assignment,
overwriting previously saved data.
Args:
task_id (int):
The ID of the task to save.
task_data (str):
A JSON blob of task data to commit to the database.
worker (orchestra.models.Worker):
The worker saving the task.
Returns:
None
Raises:
orchestra.core.errors.TaskAssignmentError:
The provided worker is not assigned to the given task or the
assignment is in a non-processing state.
"""
task = Task.objects.get(id=task_id)
if not task.is_worker_assigned(worker):
raise TaskAssignmentError('Worker is not associated with task')
# Use select_for_update to prevent concurrency issues with submit_task.
# See https://github.com/b12io/orchestra/issues/2.
assignment = (TaskAssignment.objects.select_for_update()
.get(task=task, worker=worker))
if assignment.status != TaskAssignment.Status.PROCESSING:
raise TaskAssignmentError('Worker is not allowed to save')
assignment.in_progress_task_data = task_data
assignment.save()
def _are_desired_steps_completed_on_project(desired_steps,
project=None,
completed_tasks=None):
"""
Determines if `desired_steps` have already been completed on
`project`. Either `project` or `completed_tasks` will be passed in,
since the caller sometimes has one but not the other.
Args:
desired_steps (django.db.models.QuerySet):
A queryset of orchestra.models.Step objects to check for
completion.
project (orchestra.models.Project):
The project to check for desired step completion,
optionally passed in instead of a list of completed tasks.
completed_tasks (django.db.models.QuerySet):
A queryset of orchestra.models.Task to check for desired step
completion, optionally passed in instead of a project.
Returns:
desired_steps_completed (bool):
True if the desired steps have been completed for the given
project or list of tasks.
Raises:
Exception: Either project or completed_tasks must be provided.
"""
if completed_tasks is None:
if project is None:
raise Exception('Must provide either project or completed_tasks')
completed_tasks = Task.objects.filter(status=Task.Status.COMPLETE,
project=project)
completed_step_slugs = set(completed_tasks.values_list('step__slug',
flat=True))
desired_step_slugs = set(desired_steps.values_list('slug', flat=True))
return not (desired_step_slugs - completed_step_slugs)
@transaction.atomic
def submit_task(task_id, task_data, iteration_status, worker):
"""
Returns a dict mapping task prerequisites onto their
latest task assignment information. The dict is of the form:
{'previous-slug': {task_assignment_data}, ...}
Args:
task_id (int):
The ID of the task to submit.
task_data (str):
A JSON blob of task data to submit.
iteration_status (orchestra.models.Iteration.Status):
The action taken upon task submission (i.e., REQUESTED_REVIEW
or PROVIDED_REVIEW).
worker (orchestra.models.Worker):
The worker submitting the task.
Returns:
task (orchestra.models.Task):
The modified task object.
Raises:
orchestra.core.errors.IllegalTaskSubmission:
Submission prerequisites for the task are incomplete or the
assignment is in a non-processing state.
orchestra.core.errors.TaskAssignmentError:
Worker belongs to more than one assignment for the given
task.
orchestra.core.errors.TaskStatusError:
Task has already been completed.
"""
submit_datetime = timezone.now()
task = Task.objects.select_related('step', 'project').get(id=task_id)
step = task.step
if not _are_desired_steps_completed_on_project(step.submission_depends_on,
project=task.project):
raise IllegalTaskSubmission('Submission prerequisites are not '
'complete.')
if task.status == Task.Status.COMPLETE:
raise TaskStatusError('Task already completed')
# Use select_for_update to prevent concurrency issues with save_task.
# See https://github.com/b12io/orchestra/issues/2.
assignments = (TaskAssignment.objects.select_for_update()
.filter(worker=worker, task=task))
# Worker can belong to only one assignment for a given task.
if not assignments.count() == 1:
raise TaskAssignmentError(
'Task assignment with worker is in broken state.')
assignment = assignments[0]
if assignment.status != TaskAssignment.Status.PROCESSING:
raise IllegalTaskSubmission('Worker is not allowed to submit')
next_status = get_next_task_status(task, iteration_status)
assignment.in_progress_task_data = task_data
# Submit latest iteration
latest_iteration = get_latest_iteration(assignment)
latest_iteration.status = iteration_status
latest_iteration.submitted_data = assignment.in_progress_task_data
latest_iteration.end_datetime = submit_datetime
latest_iteration.save()
assignment.status = TaskAssignment.Status.SUBMITTED
assignment.save()
previous_status = task.status
task.status = next_status
task.save()
if task.status == Task.Status.PENDING_REVIEW:
# Check the assignment policy to try to assign a reviewer automatically
task = _preassign_workers(task, AssignmentPolicyType.REVIEWER)
elif task.status == Task.Status.REVIEWING:
update_related_assignment_status(task,
assignment.assignment_counter + 1,
assignment.in_progress_task_data,
submit_datetime)
elif task.status == Task.Status.POST_REVIEW_PROCESSING:
update_related_assignment_status(task,
assignment.assignment_counter - 1,
assignment.in_progress_task_data,
submit_datetime)
elif task.status == Task.Status.COMPLETE:
create_subsequent_tasks(task.project)
notify_status_change(task, previous_status)
return task
def get_previously_completed_task_data(step, project):
"""
Returns a dict mapping task prerequisites onto their
latest task assignment information. The dict is of the form:
{'previous-slug': {latest_assignment_data}, ...}
Args:
step (orchestra.models.Step): The specified step object.
project (orchestra.models.Project): The specified project object.
Returns:
prerequisites (dict):
A dict mapping task prerequisites onto their latest task
assignment information.
"""
# Find all prerequisite steps in graph
to_visit = list(step.creation_depends_on.all())
prerequisite_steps = set(to_visit)
while to_visit:
current_step = to_visit.pop()
for step in current_step.creation_depends_on.all():
if step not in prerequisite_steps:
to_visit.append(step)
prerequisite_steps.add(step)
prerequisite_data = {}
for step in prerequisite_steps:
required_task = Task.objects.get(step=step, project=project)
if required_task.status != Task.Status.COMPLETE:
raise TaskDependencyError('Task depenency is not satisfied')
assignment = assignment_history(required_task).last()
prerequisite_data[step.slug] = assignment.in_progress_task_data
return prerequisite_data
def update_related_assignment_status(task, assignment_counter, data,
submit_datetime):
"""
Copy data to a specified task assignment and mark it as processing.
Args:
task (orchestra.models.Task):
The task whose assignments will be updated.
assignment_counter (int):
The index of the assignment to be updated.
data (str):
A JSON blob containing data to add to the assignment.
end_datetime (datetime.datetime):
The datetime when the previous task assignment was submitted.
Returns:
None
"""
assignment = (TaskAssignment.objects
.get(task=task,
assignment_counter=assignment_counter))
assignment.in_progress_task_data = data
assignment.status = TaskAssignment.Status.PROCESSING
assignment.save()
Iteration.objects.create(
assignment=assignment,
start_datetime=submit_datetime)
def _call_abort_completion_function(project):
abort_completion_fn = project.workflow_version.abort_completion_function