[autotest] Re-sync aborted jobs to shards

If a job is aborted on the master, the shards need to know, so
they can abort the job.

With this commit jobs that are already synced but have been aborted
in the meantime will be re-synced.

BUG=None
TEST=Ran suites

Change-Id: I077400047ba51b19151035f0f58c585959ceba7f
Reviewed-on: https://chromium-review.googlesource.com/218390
Tested-by: Jakob Jülich <jakobjuelich@chromium.org>
Reviewed-by: Fang Deng <fdeng@chromium.org>
Commit-Queue: Jakob Jülich <jakobjuelich@chromium.org>
diff --git a/frontend/afe/models.py b/frontend/afe/models.py
index 39646b4..a813a4e 100644
--- a/frontend/afe/models.py
+++ b/frontend/afe/models.py
@@ -1328,14 +1328,16 @@
         # If this changes or they are triggered manually, this applies:
         # Jobs may be returned more than once by concurrent calls of this
         # function, as there is a race condition between SELECT and UPDATE.
+        unassigned_or_aborted_query = (
+            dbmodels.Q(shard=None) | dbmodels.Q(hostqueueentry__aborted=True))
         job_ids = list(Job.objects.filter(
-            shard=None,
+            unassigned_or_aborted_query,
             dependency_labels=shard.labels.all()
             ).exclude(
             hostqueueentry__complete=True
             ).exclude(
             hostqueueentry__active=True
-            ).values_list('pk', flat=True))
+            ).distinct().values_list('pk', flat=True))
         if job_ids:
             Job.objects.filter(pk__in=job_ids).update(shard=shard)
             return list(Job.objects.filter(pk__in=job_ids).all())