summaryrefslogtreecommitdiff
path: root/bin/get-assignment
blob: 6abfaef0dbfb9c6cfcbb08002cc6bded8baa9fea (plain)
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
#!/bin/sh

# get-assignment $architecture $prefered-package-list

# receive one package to be built from the build-list whose dependencies
# are already satisfied or which breaks a dependency cycle
# accepts a comma separated list of prefered packages as the first argument

# exit code shows state of success:
#  0: ok, I gave you an assignment
#  1: come back (shortly) later - I was running already
#  2: come back later - there are still packages to be built,
#     but currently none has all its dependencies ready
#  3: come back after the next run of get-package-updates - currently
#     there are no pending packages
#  4: wrong number of arguments

# shellcheck disable=SC2119,SC2120

# shellcheck source=../lib/load-configuration
. "${0%/*}/../lib/load-configuration"

# hand_out_assignment $build_assignments.id
hand_out_assignment() {

  # shellcheck disable=SC2016
  {
    printf 'SELECT '
    printf '`package_sources`.`%s`,' \
      'pkgbase' 'git_revision' 'mod_git_revision'
    printf '`upstream_repositories`.`name`,'
    printf '`binary_packages`.`sub_pkgrel`,'
    printf '`architectures`.`name`'
    printf ' FROM `upstream_repositories`'
    mysql_join_upstream_repositories_package_sources
    mysql_join_package_sources_build_assignments
    mysql_join_build_assignments_architectures
    mysql_join_build_assignments_binary_packages
    mysql_join_binary_packages_binary_packages_in_repositories
    printf ' WHERE `binary_packages_in_repositories`.`repository`=%s' \
      "${repository_ids__any_build_list}"
    printf ' AND `build_assignments`.`id`=%s' \
      "$1"
    printf ' LIMIT 1;\n'

    printf 'SELECT '
    mysql_package_name_query
    printf ' FROM `binary_packages`'
    mysql_join_binary_packages_architectures
    printf ' LEFT'
    mysql_join_binary_packages_compressions
    mysql_join_binary_packages_binary_packages_in_repositories
    printf ' WHERE `binary_packages`.`build_assignment`=%s' \
      "$1"
    printf ' AND `binary_packages_in_repositories`.`repository`=%s' \
      "${repository_ids__any_build_list}"
    printf ';\n'

    printf 'UPDATE `build_slaves`'
    printf ' SET `build_slaves`.`currently_building`=%s,' \
      "$1"
    printf ' `build_slaves`.`is_sane`=1'
    # shellcheck disable=SC2154
    printf ' WHERE `build_slaves`.`id`=from_base64("%s");\n' \
      "$(printf '%s' "${slave_id}" | base64 -w0)"

    printf 'UPDATE `build_assignments`'
    printf ' SET `build_assignments`.`priority`=0'
    printf ' WHERE `build_assignments`.`id`=%s;\n' \
      "$1"
  } | \
    mysql_run_query | \
    tr '\t' ' '

  exit 0

}

if [ -s "${work_dir}/build-master-sanity" ]; then
  >&2 echo 'Build master is not sane.'
  exit 1
fi

# Create a lock file and a trap.

exec 9> "${build_list_lock_file}"
if ! verbose_flock -n 9; then
  >&2 echo 'come back (shortly) later - I cannot lock build list.'
  exit 1
fi

exec 8> "${sanity_check_lock_file}"
if ! verbose_flock -s -n 8; then
  >&2 echo 'come back (shortly) later - sanity-check running.'
  exit 1
fi

clean_up() {
  rm -rf --one-file-system "${tmp_dir}"
}

tmp_dir=$(mktemp -d 'tmp.get-assignment.XXXXXXXXXX' --tmpdir)
trap clean_up EXIT

arch=$(
  printf '%s' "$1" | \
    base64 -w0
)
shift
if [ $# -eq 1 ]; then
  requested=$(
    printf '%s' "$1" | \
      base64 -w0
  )
  shift
else
  requested=''
fi
# shellcheck disable=SC2016
if [ $# -ne 0 ]; then
  >&2 echo '"get-assignment" was called with wrong number of arguments.'
  >&2 echo 'call either:'
  >&2 echo '  get-assignment $arch'
  >&2 echo '  get-assignment $arch $favourite_package'
  exit 4
fi

# if we're building something already, hand it out (again)
currently_building=$(
  # shellcheck disable=SC2016
  {
    printf 'SELECT `build_assignments`.`id`'
    printf ' FROM `build_assignments`'
    mysql_join_build_assignments_build_slaves
    mysql_join_build_assignments_binary_packages
    mysql_join_binary_packages_binary_packages_in_repositories
    printf ' JOIN `architecture_compatibilities`'
    printf ' ON `architecture_compatibilities`.`built_for`=`build_assignments`.`architecture`'
    printf ' JOIN `architectures`'
    printf ' ON `architecture_compatibilities`.`runs_on`=`architectures`.`id`'
    printf ' AND `architecture_compatibilities`.`build_slave_compatible`'
    printf ' WHERE `build_slaves`.`id`=from_base64("%s")' \
      "$(printf '%s' "${slave_id}" | base64 -w0)"
    printf ' AND `binary_packages_in_repositories`.`repository`=%s' \
      "${repository_ids__any_build_list}"
    printf ' AND `architectures`.`name` = from_base64("%s")' \
      "${arch}"
    printf ' LIMIT 1;\n'
  } | \
    mysql_run_query
)

if [ -n "${currently_building}" ]; then
  hand_out_assignment "${currently_building}"
fi

# shellcheck disable=SC2154
while [ -s "${work_dir}/forced-package-builds.${slave}" ]; do
  next_building=$(
    sed -i '
      1 {
        w /dev/stdout
        d
      }
    ' "${work_dir}/forced-package-builds.${slave}"
  )
  if [ ! -s "${work_dir}/forced-package-builds.${slave}" ]; then
    rm "${work_dir}/forced-package-builds.${slave}"
  fi
  if [ -n "${next_building}" ]; then
    next_building=$(
      # shellcheck disable=SC2016
      {
        printf 'SELECT'
        printf ' `build_assignments`.`id`'
        printf ' FROM `build_assignments`'
        mysql_join_build_assignments_binary_packages
        mysql_join_binary_packages_binary_packages_in_repositories
        printf ' AND `binary_packages_in_repositories`.`repository`=%s' \
          "${repository_ids__any_build_list}"
        mysql_join_build_assignments_package_sources
        mysql_join_build_assignments_architectures
        printf ' WHERE `package_sources`.`pkgbase`=from_base64("%s")' \
          "$(
            printf '%s' "${next_building#* }" | \
              base64 -w0
          )"
        printf ' AND `architectures`.`name`=from_base64("%s")' \
          "$(
            printf '%s' "${next_building%% *}" | \
              base64 -w0
          )"
        printf ' LIMIT 1;\n'
      } | \
        mysql_run_query
    )
    if [ -n "${next_building}" ]; then
      hand_out_assignment "${next_building}"
    fi
  fi
done

# a package with [all dependencies met (or unsuccessfully built) or which is part of a loop]
# and which is currently not being built, ordered by:
# 1: we requested it
# 2: tool chain priority
# 3: its priority
# 4: was built the least times
# 5: architecture matches (or the slave is from the majority architecture)
# 6: is part of a loop
# 7: has the oldes package source
# 8: build_assignment id
next_building=$(
  # shellcheck disable=SC2016
  {
    for suffix in '' '_2' '_3' '_4'; do
      printf 'CREATE TEMPORARY TABLE `considered_build_assignments%s` (' \
        "${suffix}"
        printf '`id` BIGINT,'
        printf ' UNIQUE KEY `id`(`id`)'
      printf ');\n'
    done

    printf 'INSERT IGNORE INTO `considered_build_assignments` (`id`)'
    printf ' SELECT `build_assignments`.`id`'
    printf ' FROM `build_assignments`'
    mysql_join_build_assignments_package_sources
    mysql_join_build_assignments_binary_packages
    mysql_join_binary_packages_binary_packages_in_repositories
    printf ' JOIN `architecture_compatibilities`'
    printf ' ON `architecture_compatibilities`.`built_for`=`build_assignments`.`architecture`'
    printf ' AND `architecture_compatibilities`.`build_slave_compatible`'
    printf ' JOIN `architectures`'
    printf ' ON `architecture_compatibilities`.`runs_on`=`architectures`.`id`'
    # "any" packages are built with the highest-level build command architecture
    printf ' JOIN `architectures` AS `build_command_architectures`'
    printf ' ON `build_command_architectures`.`id`=IF('
      printf '`build_assignments`.`architecture`=%s,' \
        "${architecture_ids__any}"
      printf '`architectures`.`id`,'
      printf '`build_assignments`.`architecture`'
    printf ')'
    printf ' LEFT'
    mysql_join_package_sources_toolchain_order
    printf ' WHERE ('
      printf '`build_assignments`.`is_blocked` IS NULL'
      if [ -n "${requested}" ]; then
        printf ' OR'
        printf ' `package_sources`.`pkgbase`=from_base64("%s")' \
          "${requested}"
      fi
    printf ') AND ('
      mysql_query_is_part_of_loop '`build_assignments`.`id`'
      printf ' OR NOT '
      mysql_query_has_pending_dependencies '`build_command_architectures`.`id`' '`build_assignments`.`id`'
      printf ' OR `toolchain_order`.`pkgbase` IS NOT NULL'
    printf ')'
    printf ' AND `binary_packages_in_repositories`.`repository`=%s' \
      "${repository_ids__any_build_list}"
    printf ' AND `architectures`.`name` = from_base64("%s");\n' \
      "${arch}"

    printf 'INSERT IGNORE INTO `considered_build_assignments_2` (`id`)'
    printf ' SELECT `considered_build_assignments`.`id`'
    printf ' FROM `considered_build_assignments`;\n'

    # delete all non-toolchain packages from considered list iff any
    # toolchain package is on that list
    printf 'CREATE TEMPORARY TABLE `toolchain_architectures` ('
      printf '`id` SMALLINT NOT NULL,'
      printf 'PRIMARY KEY (`id`)'
    printf ');\n'
    printf 'INSERT IGNORE INTO `toolchain_architectures` (`id`)'
    printf ' SELECT `build_assignments`.`architecture`'
    printf ' FROM `considered_build_assignments`'
    printf ' JOIN `build_assignments`'
    printf ' ON `build_assignments`.`id`=`considered_build_assignments`.`id`'
    mysql_join_build_assignments_package_sources
    mysql_join_package_sources_toolchain_order
    printf ';\n'

    printf 'DELETE `considered_build_assignments`'
    printf ' FROM `toolchain_architectures`'
    printf ' JOIN `build_assignments`'
    printf ' ON `build_assignments`.`architecture`=`toolchain_architectures`.`id`'
    printf ' JOIN `considered_build_assignments`'
    printf ' ON `build_assignments`.`id`=`considered_build_assignments`.`id`'
    mysql_join_build_assignments_package_sources
    printf ' LEFT'
    mysql_join_package_sources_toolchain_order
    printf ' WHERE `toolchain_order`.`number` IS NULL;\n'

    printf 'DELETE `considered_build_assignments_2`'
    printf ' FROM `considered_build_assignments_2`;\n'
    for suffix in '_2' '_3' '_4'; do
      printf 'INSERT IGNORE INTO `considered_build_assignments%s` (`id`)' \
        "${suffix}"
      printf ' SELECT `considered_build_assignments`.`id`'
      printf ' FROM `considered_build_assignments`;\n'
    done

    # delete all but one package from considered list iff any toolchain
    # package is on that list
    # the order is:
    #  - does not exist blocked before exists blocked
    #  - unbuilt before built
    #  - exists unblocked before does not exist unblocked

    printf 'CREATE TEMPORARY TABLE `ordered_considered_build_assignments`('
      printf '`order` BIGINT NOT NULL AUTO_INCREMENT,'
      printf '`id` BIGINT NOT NULL,'
      printf '`exists_blocked` BIT NOT NULL,'
      printf '`exists_built` BIT NOT NULL,'
      printf '`exists_unblocked` BIT NOT NULL,'
      printf 'PRIMARY KEY (`order`),'
      printf 'KEY (`id`)'
    printf ');\n'
    printf 'CREATE TEMPORARY TABLE `orders`('
      printf '`order` BIGINT NOT NULL AUTO_INCREMENT,'
      printf 'PRIMARY KEY (`order`)'
    printf ');\n'

# TODO: This is broken, it cannot work and probably never will do this way.
    printf 'INSERT IGNORE INTO `ordered_considered_build_assignments` ('
      printf '`id`,'
      printf '`exists_blocked`,'
      printf '`exists_built`,'
      printf '`exists_unblocked`'
    printf ')'
    printf 'SELECT'
    printf ' `build_assignments`.`id`,'
    printf 'EXISTS ('
      printf 'SELECT 1'
      printf ' FROM `toolchain_order` AS `late`'
      printf ' JOIN `toolchain_order` AS `early`'
      printf ' ON `late`.`pkgbase`!=`early`.`pkgbase`'
      printf ' AND `late`.`number`>`early`.`number`'
      mysql_join_toolchain_order_package_sources 'early' 'e_ps'
      mysql_join_package_sources_build_assignments 'e_ps' 'e_ba'
      printf ' JOIN `considered_build_assignments_3` AS `e_cba`'
      printf ' ON `e_cba`.`id`=`e_ba`.`id`'
      printf ' WHERE `late`.`pkgbase`=`package_sources`.`pkgbase`'
      # the toolchain_order-blocked build-assignment
      printf ' AND ('
        # must be marked as requires_all_dependencies_built (second half of list)
        printf '`late`.`requires_all_dependencies_built` OR'
        # or all blocking dependencies must not yet been built
        printf ' NOT EXISTS ('
          printf 'SELECT 1 FROM `binary_packages` AS `e_bp`'
          mysql_join_binary_packages_binary_packages_in_repositories 'e_bp' 'e_bpir'
          mysql_join_binary_packages_in_repositories_repositories 'e_bpir' 'e_r'
          printf ' WHERE `e_bp`.`build_assignment`=`e_ba`.`id`'
          printf ' AND `e_r`.`is_on_master_mirror`'
        printf ')'
      printf ')'
    printf ') AS `exists_blocked`,'
    printf 'EXISTS ('
      printf 'SELECT 1'
      printf ' FROM `binary_packages_in_repositories`'
      mysql_join_binary_packages_in_repositories_repositories
      printf ' AND `repositories`.`is_on_master_mirror`'
      printf ' JOIN `architecture_compatibilities`'
      printf ' ON `repositories`.`architecture`=`architecture_compatibilities`.`built_for`'
      printf ' AND `architecture_compatibilities`.`build_slave_compatible`'
      printf ' JOIN `architectures`'
      printf ' ON `architectures`.`id`=`architecture_compatibilities`.`runs_on`'
      printf ' AND `architectures`.`name`=from_base64("%s")' \
        "${arch}"
      mysql_join_binary_packages_in_repositories_binary_packages
      printf ' WHERE `binary_packages`.`build_assignment`=`build_assignments`.`id`'
    printf ') AS `exists_built`,'
    printf 'EXISTS ('
      printf 'SELECT 1'
      printf ' FROM `toolchain_order` AS `late`'
      printf ' WHERE NOT EXISTS ('
        printf 'SELECT 1'
        printf ' FROM `toolchain_order` AS `early`'
        mysql_join_toolchain_order_package_sources 'early' 'e_ps'
        mysql_join_package_sources_build_assignments 'e_ps' 'e_ba'
        printf ' JOIN `considered_build_assignments_4` AS `e_cba`'
        printf ' ON `e_ba`.`id`=`e_cba`.`id`'
        printf ' WHERE `late`.`pkgbase`!=`early`.`pkgbase`'
        printf ' AND `late`.`number`>`early`.`number`'
      printf ')'
      printf ' AND `late`.`pkgbase`=`package_sources`.`pkgbase`'
    printf ') AS `exists_unblocked`'
    printf ' FROM `considered_build_assignments`'
    printf ' JOIN `build_assignments`'
    printf ' ON `build_assignments`.`id`=`considered_build_assignments`.`id`'
    printf ' JOIN `toolchain_architectures`'
    printf ' ON `build_assignments`.`architecture`=`toolchain_architectures`.`id`'
    mysql_join_build_assignments_package_sources
    printf ' ORDER BY'
    # does it exist toolchain-blocked?
    printf ' `exists_blocked`,'
    # does it exist built?
    printf '`exists_built`,'
    # does it exist not toolchain-blocked?
    printf '`exists_unblocked` DESC'
    printf ';\n'

    printf 'INSERT IGNORE INTO `orders`(`order`)'
    printf ' SELECT'
    printf ' MIN(`ordered_considered_build_assignments`.`order`)'
    printf ' FROM `ordered_considered_build_assignments`'
    printf ' JOIN `build_assignments`'
    printf ' ON `build_assignments`.`id`=`ordered_considered_build_assignments`.`id`'
    printf ' JOIN `toolchain_architectures`'
    printf ' JOIN `architecture_compatibilities`'
    printf ' ON `architecture_compatibilities`.`fully_compatible`'
    printf ' AND ('
      printf '('
        printf '`architecture_compatibilities`.`runs_on`=`build_assignments`.`architecture`'
        printf ' AND `architecture_compatibilities`.`built_for`=`toolchain_architectures`.`id`'
      printf ') OR ('
        printf '`architecture_compatibilities`.`built_for`=`build_assignments`.`architecture`'
        printf ' AND `architecture_compatibilities`.`runs_on`=`toolchain_architectures`.`id`'
      printf ')'
    printf ')'
    printf ' GROUP BY `toolchain_architectures`.`id`'
    printf ';\n'

    printf 'DELETE `considered_build_assignments`'
    printf ' FROM `toolchain_architectures`'
    printf ' JOIN `build_assignments`'
    printf ' ON `build_assignments`.`architecture`=`toolchain_architectures`.`id`'
    printf ' JOIN `considered_build_assignments`'
    printf ' ON `build_assignments`.`id`=`considered_build_assignments`.`id`;\n'

    printf 'INSERT IGNORE INTO `considered_build_assignments` (`id`)'
    printf ' SELECT'
    printf ' `ordered_considered_build_assignments`.`id`'
    printf ' FROM `ordered_considered_build_assignments`'
    printf ' JOIN `orders`'
    printf ' ON `orders`.`order`=`ordered_considered_build_assignments`.`order`'
    printf ';\n'

    printf 'DROP TEMPORARY TABLE `ordered_considered_build_assignments`;\n'
    printf 'DROP TEMPORARY TABLE `orders`;\n'

    # delete all currently building packages from considered list
    printf 'DELETE `considered_build_assignments`'
    printf ' FROM `considered_build_assignments`'
    mysql_join_build_assignments_build_slaves 'considered_build_assignments'
    printf ';\n'

    printf 'SELECT `considered_build_assignments`.`id` FROM'
    printf ' `considered_build_assignments`'
    printf ' JOIN `build_assignments`'
    printf ' ON `build_assignments`.`id`=`considered_build_assignments`.`id`'
    mysql_join_build_assignments_package_sources
    mysql_join_build_assignments_binary_packages
    printf ' LEFT JOIN ('
      printf 'SELECT'
      printf ' `failed_builds`.`build_assignment`,'
      printf 'COUNT(DISTINCT `failed_builds`.`id`) AS `count`'
      printf ' FROM `failed_builds`'
      printf ' GROUP BY `failed_builds`.`build_assignment`'
    printf ') AS `latest_failed_builds`'
    printf ' ON `latest_failed_builds`.`build_assignment`=`build_assignments`.`id`'
    printf ' LEFT JOIN ('
      # Does this job wait for something else in `toolchain_order`?
      printf 'SELECT '
      printf '`tc_q`.`pkgbase`,'
      printf '`tc_q`.`architecture`,'
      printf 'IF('
        printf '`tc_q`.`is_blocked`,'
        printf 'IF('
          printf '`tc_q`.`is_built`,'
          printf '2,'
          printf '1'
        printf '),'
        printf '0'
      printf ') AS `priority`'
      printf ' FROM ('
        printf 'SELECT '
        printf '`late`.`pkgbase`,'
        printf '`binary_packages`.`architecture`,'
        printf 'MAX('
          printf 'IF('
            printf 'EXISTS ('
              printf 'SELECT 1'
              printf ' FROM `binary_packages` AS `built_bp`'
              mysql_join_binary_packages_binary_packages_in_repositories 'built_bp' 'built_bpir'
              mysql_join_binary_packages_in_repositories_repositories 'built_bpir' 'built_r'
              printf ' AND `built_r`.`is_on_master_mirror`'
              printf ' WHERE `built_bp`.`build_assignment`=`build_assignments`.`id`'
            printf '),'
            printf '1,'
            printf '0'
          printf ')'
        printf ') AS `is_built`,'
        printf 'MAX('
          printf 'IF('
            printf 'EXISTS ('
              printf 'SELECT 1'
              printf ' FROM `package_sources` AS `block_ps`'
              mysql_join_package_sources_build_assignments 'block_ps' 'block_ba'
              mysql_join_build_assignments_binary_packages 'block_ba' 'block_bp'
              mysql_join_binary_packages_binary_packages_in_repositories 'block_bp' 'block_bpir'
              printf ' AND `block_bpir`.`repository`=%s' \
                "${repository_ids__any_build_list}"
              mysql_join_package_sources_toolchain_order 'block_ps' 'early'
              printf ' WHERE `early`.`number`<`late`.`number`'
              printf ' AND `early`.`pkgbase`!=`late`.`pkgbase`'
              printf ' AND ('
                printf '`block_bp`.`architecture`=`binary_packages`.`architecture`'
                # shellcheck disable=SC2154
                printf ' OR `%s`.`architecture`=%s' \
                  'block_bp' "${architecture_ids__any}" \
                  'binary_packages' "${architecture_ids__any}"
              printf ')'
            printf '),'
            printf '1,'
            printf '0'
          printf ')'
        printf ') AS `is_blocked`'
        printf ' FROM `toolchain_order` AS `late`'
        mysql_join_toolchain_order_package_sources 'late'
        mysql_join_package_sources_build_assignments
        mysql_join_build_assignments_binary_packages
        mysql_join_binary_packages_binary_packages_in_repositories
        printf ' AND `binary_packages_in_repositories`.`repository`=%s' \
          "${repository_ids__any_build_list}"
        printf ' GROUP BY CONCAT(`late`.`pkgbase`,"-",`binary_packages`.`architecture`)'
      printf ') AS `tc_q`'
    printf ') AS `toolchain_query`'
    printf ' ON `toolchain_query`.`pkgbase`=`package_sources`.`pkgbase`'
    printf ' AND `toolchain_query`.`architecture`=`binary_packages`.`architecture`'
    printf ' JOIN `architectures`'
    printf ' ON `architectures`.`name`=from_base64("%s")' \
      "${arch}"
    printf ' ORDER BY '
    if [ -n "${requested}" ]; then
      printf 'IF(`package_sources`.`pkgbase`=from_base64("%s"),1,0) DESC,' \
        "${requested}"
    fi
    # 0: in toolchain, should be built now
    # 1: in toolchain, should be built later
    # 2: not in toolchain
    printf 'IFNULL(`toolchain_query`.`priority`,2),'
    printf '`build_assignments`.`priority` DESC,'
    printf 'IFNULL(`latest_failed_builds`.`count`,0),'
    printf 'IF('
      printf '`architectures`.`id`=%s' \
        "$(mysql_determine_majority_build_slave_architecture_id)"
      printf ' OR `architectures`.`id`=`build_assignments`.`architecture`,'
      printf '1,0) DESC,'
    printf 'IF('
      mysql_query_is_part_of_loop '`build_assignments`.`id`'
    printf ',1,0),'
    printf '`package_sources`.`commit_time`,'
    printf '`build_assignments`.`id`'
    printf ' LIMIT 1;\n'
  } | \
    mysql_run_query 'unimportant'
)
if [ -n "${next_building}" ]; then
  hand_out_assignment "${next_building}"
fi

# Check if there are any pending packages at all
count_pending=$(
  # shellcheck disable=SC2016
  {
    printf 'SELECT COUNT(1)'
    printf ' FROM `build_assignments`'
    mysql_join_build_assignments_binary_packages
    mysql_join_binary_packages_binary_packages_in_repositories
    mysql_join_binary_packages_in_repositories_repositories
    printf ' JOIN `architecture_compatibilities`'
    printf ' ON `architecture_compatibilities`.`built_for`=`build_assignments`.`id`'
    printf ' AND `architecture_compatibilities`.`build_slave_compatible`'
    printf ' JOIN `architectures`'
    printf ' ON `architecture_compatibilities`.`runs_on`=`architectures`.`id`'
    printf ' WHERE `repositories`.`name`="build-list"'
    printf ' AND `build_assignments`.`is_blocked` IS NULL'
    printf ' AND `architectures`.`name`=from_base64("%s")' \
      "${arch}"
    printf ';\n'
  } | \
    mysql_run_query 'unimportant'
)

if [ "${count_pending}" -eq 0 ]; then
  >&2 echo 'come back after the next run of get-package-updates - currently there are no pending packages'
  exit 3
else
  >&2 echo 'come back later - there are still packages to be built, but currently none has all its dependencies ready'
  exit 2
fi