aboutsummaryrefslogtreecommitdiff
path: root/monitor/monitor.cxx
blob: 0d5545e676553a9807ee9d04157daaef2aff1538 (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
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
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
// file      : monitor/monitor.cxx -*- C++ -*-
// license   : MIT; see accompanying LICENSE file

#include <map>
#include <set>
#include <chrono>
#include <iostream>
#include <algorithm> // find_if()

#include <odb/database.hxx>
#include <odb/transaction.hxx>
#include <odb/schema-catalog.hxx>

#include <odb/pgsql/database.hxx>

#include <libbutl/pager.hxx>

#include <libbbot/build-config.hxx>

#include <libbrep/build.hxx>
#include <libbrep/common.hxx>
#include <libbrep/build-odb.hxx>
#include <libbrep/build-package.hxx>
#include <libbrep/build-package-odb.hxx>
#include <libbrep/database-lock.hxx>

#include <mod/build-config.hxx>

#include <monitor/module-options.hxx>
#include <monitor/monitor-options.hxx>

using namespace std;
using namespace butl;
using namespace bbot;
using namespace odb::core;

namespace brep
{
  // Operation failed, diagnostics has already been issued.
  //
  struct failed {};

  // We will collect and report build delays as separate steps not to hold
  // database locks while printing to stderr. Also we need to order delays
  // properly, so while printing reports we could group delays by toolchain
  // and configuration.
  //
  // To achieve that, we will iterate through all possible package builds
  // creating the list of delays with the following sort priority:
  //
  // 1: toolchain name
  // 2: toolchain version (descending)
  // 3: configuration name
  // 4: configuration target
  // 5: tenant
  // 6: package name
  // 7: package version (descending)
  //
  struct compare_delay
  {
    bool
    operator() (const shared_ptr<const build_delay>& x,
                const shared_ptr<const build_delay>& y) const
    {
      if (int r = x->toolchain_name.compare (y->toolchain_name))
        return r < 0;

      if (int r = x->toolchain_version.compare (y->toolchain_version))
        return r > 0;

      if (int r = x->configuration.compare (y->configuration))
        return r < 0;

      if (int r = x->target.compare (y->target))
        return r < 0;

      if (int r = x->tenant.compare (y->tenant))
        return r < 0;

      if (int r = x->package_name.compare (y->package_name))
        return r < 0;

      return x->package_version.compare (y->package_version) > 0;
    }
  };

  // The ordered list of delays to report.
  //
  class delay_report
  {
  public:
    // Note that in the brief mode we also need to print the total number of
    // delays (reported or not) per configuration. Thus, we add all delays to
    // the report object, marking them if we need to report them or not.
    //
    void
    add_delay (shared_ptr<build_delay>, bool report);

    bool
    empty () const
    {
      return reported_delay_count_ == 0;
    }

    // In the brief mode (if full is false) print the number of reported/total
    // (if total is true) delayed package builds per configuration rather than
    // the packages themselves.
    //
    void
    print (const char* header, bool total, bool full) const;

  private:
    // Maps delays to the report flag.
    //
    map<shared_ptr<const build_delay>, bool, compare_delay> delays_;
    size_t reported_delay_count_ = 0;
  };

  void delay_report::
  add_delay (shared_ptr<build_delay> delay, bool report)
  {
    delays_.emplace (move (delay), report);

    if (report)
      ++reported_delay_count_;
  }

  void delay_report::
  print (const char* header, bool total, bool full) const
  {
    if (empty ())
      return;

    cerr << header << " (" << reported_delay_count_;

    if (total)
      cerr << '/' << delays_.size ();

    cerr << "):" << endl;

    // Group the printed delays by toolchain and configuration.
    //
    const string*         toolchain_name    (nullptr);
    const version*        toolchain_version (nullptr);
    const string*         configuration     (nullptr);
    const target_triplet* target            (nullptr);

    size_t config_reported_delay_count (0);
    size_t config_total_delay_count (0);

    auto brief_config = [&configuration,
                         &target,
                         &config_reported_delay_count,
                         &config_total_delay_count,
                         total] ()
    {
      if (configuration != nullptr)
      {
        assert (target != nullptr);

        // Only print configurations with delays that needs to be reported.
        //
        if (config_reported_delay_count != 0)
        {
          cerr << "    " << *configuration << '/' << *target << " ("
               << config_reported_delay_count;

          if (total)
            cerr << '/' << config_total_delay_count;

          cerr << ')' << endl;
        }

        config_reported_delay_count = 0;
        config_total_delay_count = 0;
      }
    };

    for (const auto& dr: delays_)
    {
      bool report (dr.second);

      if (full && !report)
        continue;

      const shared_ptr<const build_delay>& d (dr.first);

      // Print the toolchain, if changed.
      //
      if (toolchain_name == nullptr            ||
          d->toolchain_name != *toolchain_name ||
          d->toolchain_version != *toolchain_version)
      {
        if (!full)
          brief_config ();

        if (toolchain_name != nullptr)
          cerr << endl;

        cerr << "  " << d->toolchain_name;

        if (!d->toolchain_version.empty ())
          cerr << "/" << d->toolchain_version;

        cerr << endl;

        toolchain_name    = &d->toolchain_name;
        toolchain_version = &d->toolchain_version;
        configuration     = nullptr;
        target            = nullptr;
      }

      // Print the configuration, if changed.
      //
      if (configuration == nullptr           ||
          d->configuration != *configuration ||
          d->target != *target)
      {
        if (full)
        {
          if (configuration != nullptr)
            cerr << endl;

          cerr << "    " << d->configuration << '/' << d->target << endl;
        }
        else
          brief_config ();

        configuration = &d->configuration;
        target        = &d->target;
      }

      // Print the delayed build package in the full report mode and count
      // configuration builds otherwise.
      //
      if (full)
      {
        // We can potentially extend this information with the archived flag
        // or the delay duration.
        //
        cerr << "      " << d->package_name << "/" << d->package_version;

        if (!d->tenant.empty ())
          cerr << " " << d->tenant;

        cerr << endl;
      }
      else
      {
        if (report)
          ++config_reported_delay_count;

        ++config_total_delay_count;
      }
    }

    if (!full)
      brief_config ();
  }

  static const char* help_info (
    "  info: run 'brep-monitor --help' for more information");

  static int
  main (int argc, char* argv[])
  try
  {
    cli::argv_scanner scan (argc, argv);
    options::monitor ops (scan);

    // Version.
    //
    if (ops.version ())
    {
      cout << "brep-monitor " << BREP_VERSION_ID << endl
           << "libbrep " << LIBBREP_VERSION_ID << endl
           << "libbbot " << LIBBBOT_VERSION_ID << endl
           << "libbpkg " << LIBBPKG_VERSION_ID << endl
           << "libbutl " << LIBBUTL_VERSION_ID << endl
           << "Copyright (c) " << BREP_COPYRIGHT << "." << endl
           << "This is free software released under the MIT license." << endl;

      return 0;
    }

    // Help.
    //
    if (ops.help ())
    {
      pager p ("brep-monitor help",
               false,
               ops.pager_specified () ? &ops.pager () : nullptr,
               &ops.pager_option ());

      print_usage (p.stream ());

      // If the pager failed, assume it has issued some diagnostics.
      //
      return p.wait () ? 0 : 1;
    }

    // Parse the brep module configuration.
    //
    options::module mod_ops;
    {
      if (!scan.more ())
      {
        cerr << "error: brep module configuration file is expected" << endl
             << help_info << endl;
        return 1;
      }

      string f (scan.next ());

      try
      {
        cli::argv_file_scanner scan (f, "" /* option */);

        // Parse the brep module options skipping those we don't recognize.
        //
        while (scan.more ())
        {
          // Parse until an unknown option is encountered.
          //
          mod_ops.parse (scan,
                         cli::unknown_mode::stop,
                         cli::unknown_mode::stop);

          // Skip the unknown option, unless we are done.
          //
          if (scan.more ())
          {
            // Skip the option name.
            //
            size_t l (scan.peek_line ());
            scan.skip ();

            // Skip the option value, if present.
            //
            // Note that here we rely on the configuration file having both
            // the option name and its value on the same line.
            //
            if (scan.more () && scan.peek_line () == l)
              scan.skip ();
          }
        }
      }
      catch (const cli::file_io_failure& e)
      {
        cerr << "error: unable to parse brep module configuration: " << e
             << endl;
        return 1;
      }
      catch (const cli::exception& e)
      {
        cerr << "error: unable to parse brep module configuration file '" << f
             << "': " << e << endl;
        return 1;
      }

      auto bad_alt = [&f] (const char* what)
      {
        cerr << "build-alt-" << what << "-rebuild-start and build-alt-"
             << what << "-rebuild-stop configuration options must both be "
             << "either specified or not in '" << f << "'" << endl;
      };

      if (mod_ops.build_alt_hard_rebuild_start_specified () !=
          mod_ops.build_alt_hard_rebuild_stop_specified ())
      {
        bad_alt("hard");
        return 1;
      }

      if (mod_ops.build_alt_soft_rebuild_start_specified () !=
          mod_ops.build_alt_soft_rebuild_stop_specified ())
      {
        bad_alt("soft");
        return 1;
      }
    }

    // Parse the toolchains suppressing duplicates.
    //
    // Note that specifying a toolchain both with and without version doesn't
    // make sense, so we fail if that's the case.
    //
    vector<pair<string, version>> toolchains;

    if (!scan.more ())
    {
      cerr << "error: toolchain is expected" << endl << help_info << endl;
      return 1;
    }

    while (scan.more ())
    {
      string s (scan.next ());

      string tn;
      version tv;

      try
      {
        size_t p (s.find ('/'));

        if (p == string::npos)
          tn = move (s);
        else
        {
          tn.assign (s, 0, p);
          tv = version (string (s, p + 1));
        }

        bool dup (false);
        for (const pair<string, version>& t: toolchains)
        {
          if (tn == t.first)
          {
            if (tv == t.second)
            {
              dup = true;
              break;
            }

            if (tv.empty () != t.second.empty ())
            {
              cerr << "error: toolchain '" << tn << "' is specified both "
                   << "with and without version" << endl;
              return 1;
            }
          }
        }

        if (!dup)
          toolchains.emplace_back (move (tn), move (tv));
      }
      catch (const invalid_argument& e)
      {
        cerr << "error: invalid toolchain '" << s << "': " << e << endl;
        return 1;
      }
    }

    // Parse buildtab.
    //
    if (!mod_ops.build_config_specified ())
    {
      cerr << "warning: package building functionality is disabled" << endl;
      return 0;
    }

    build_configs configs;

    try
    {
      configs = parse_buildtab (mod_ops.build_config ());
    }
    catch (const tab_parsing& e)
    {
      cerr << "error: unable to parse buildtab: " << e << endl;
      return 1;
    }
    catch (const io_error& e)
    {
      cerr << "error: unable to read '" << mod_ops.build_config () << "': "
           << e << endl;
      return 1;
    }

    // Create the database instance.
    //
    odb::pgsql::database db (
      ops.build_db_user (),
      ops.build_db_password (),
      (ops.build_db_name_specified ()
       ? ops.build_db_name ()
       : mod_ops.build_db_name ()),
      (ops.build_db_host_specified ()
       ? ops.build_db_host ()
       : mod_ops.build_db_host ()),
      (ops.build_db_port_specified ()
       ? ops.build_db_port ()
       : mod_ops.build_db_port ()),
      "options='-c default_transaction_isolation=serializable'");

    // Prevent several brep utility instances from updating the build database
    // simultaneously.
    //
    database_lock l (db);

    // Check that the database schema matches the current one.
    //
    const string ds ("build");
    if (schema_catalog::current_version (db, ds) != db.schema_version (ds))
    {
      cerr << "error: build database schema differs from the current one"
           << endl
           << "  info: use brep-migrate to migrate the database" << endl;
      return 1;
    }

    // If requested, cleanup delays for package builds that are not expected
    // anymore (build configuration is not present, etc).
    //
    if (ops.clean ())
    {
      using config_map = map<build_config_id, const build_config*>;

      config_map conf_map;
      for (const build_config& c: configs)
        conf_map[build_config_id {c.name, c.target}] = &c;

      // Prepare the build delay prepared query.
      //
      // Query package build delays in chunks in order not to hold locks for
      // too long. Sort the result by package version as a first priority to
      // minimize number of queries to the package database. Note that we
      // still need to sort by configuration and toolchain to make sure that
      // build delays are sorted consistently across queries and we don't miss
      // any of them.
      //
      using query = query<build_delay>;
      using prep_query = prepared_query<build_delay>;

      // Specify the portion.
      //
      size_t offset (0);

      query q ("ORDER BY" +
               query::id.package.tenant + "," +
               query::id.package.name +
               order_by_version (query::id.package.version,
                                 false /* first */) + "," +
               query::id.configuration + "," +
               query::id.target + "," +
               query::id.toolchain_name +
               order_by_version (query::id.toolchain_version,
                                 false /* first */) +
               "OFFSET" + query::_ref (offset) + "LIMIT 100");

      connection_ptr conn (db.connection ());

      prep_query pq (
        conn->prepare_query<build_delay> ("build-delay-query", q));

      // Cache the delayed build package object to reuse it in case the next
      // delay refers to the same package (which is often the case due to the
      // query result sorting criteria we use).
      //
      package_id pid;
      shared_ptr<build_package> p;

      for (bool ne (true); ne; )
      {
        transaction t (conn->begin ());

        // Query delays.
        //
        auto delays (pq.execute ());

        if ((ne = !delays.empty ()))
        {
          // Iterate over the build delays and cleanup the outdated ones.
          //
          for (const build_delay& d: delays)
          {
            config_map::const_iterator ci;

            bool cleanup (
              // Check that the toolchain is still used.
              //
              find_if (toolchains.begin (), toolchains.end (),
                       [&d] (const pair<string, version>& t)
                       {
                         return t.first == d.toolchain_name &&
                                t.second == d.toolchain_version;
                       }) == toolchains.end () ||
              //
              // Check that the build configuration is still present.
              //
              (ci = conf_map.find (build_config_id {d.configuration,
                                                    d.target})) ==
              conf_map.end ());

            // Check that the package still present, is buildable and doesn't
            // exclude the build configuration.
            //
            if (!cleanup)
            {
              if (d.id.package != pid)
              {
                pid = d.id.package;
                p = db.find<build_package> (pid);
              }

              cleanup = (p == nullptr  ||
                         !p->buildable ||
                         exclude (p->builds,
                                  p->constraints,
                                  *ci->second,
                                  configs.class_inheritance_map));
            }

            if (cleanup)
              db.erase (d);
            else
              ++offset;
          }
        }

        t.commit ();
      }
    }

    delay_report hard_delays_report;
    delay_report soft_delays_report;
    set<shared_ptr<const build_delay>, compare_delay> update_delays;
    {
      connection_ptr conn (db.connection ());

      // Prepare the buildable package prepared query.
      //
      // Query buildable packages in chunks in order not to hold locks for too
      // long.
      //
      using pquery = query<buildable_package>;
      using prep_pquery = prepared_query<buildable_package>;

      // Specify the portion.
      //
      size_t offset (0);

      pquery pq ("ORDER BY"                            +
                 pquery::build_package::id.tenant + "," +
                 pquery::build_package::id.name         +
                 order_by_version (pquery::build_package::id.version,
                                   false /* first */)  +
                 "OFFSET" + pquery::_ref (offset) + "LIMIT 50");

      prep_pquery ppq (
        conn->prepare_query<buildable_package> ("buildable-package-query",
                                                pq));

      // Prepare the package build prepared query.
      //
      // This query will only be used for toolchains that have no version
      // specified on the command line to obtain the latest completed build
      // across all toolchain versions, if present, and the latest incomplete
      // build otherwise.
      //
      // Why don't we pick the latest toolchain version? We don't want to
      // stuck with it on the toolchain rollback. Instead we prefer the
      // toolchain that built the package last and if there are none, pick the
      // one for which the build task was issued last.
      //
      using bquery = query<package_build>;
      using prep_bquery = prepared_query<package_build>;

      build_id id;

      const auto& bid (bquery::build::id);

      bquery bq ((equal<package_build> (bid.package, id.package)       &&
                  bid.configuration == bquery::_ref (id.configuration) &&
                  bid.target == bquery::_ref (id.target)               &&
                  bid.toolchain_name == bquery::_ref (id.toolchain_name)) +
                 "ORDER BY"                                               +
                 bquery::build::soft_timestamp + "DESC, "                 +
                 bquery::build::timestamp + "DESC"                        +
                 "LIMIT 1");

      prep_bquery pbq (
        conn->prepare_query<package_build> ("package-build-query", bq));

      timestamp now (system_clock::now ());

      // Calculate the build/rebuild expiration time, based on the respective
      // --{soft,hard}-rebuild-timeout monitor options and the
      // build-{soft,hard}-rebuild-timeout and
      // build-alt-{soft,hard}-rebuild-{start,stop,timeout} brep module
      // configuration options.
      //
      // If the --*-rebuild-timeout monitor option is zero or is not specified
      // and the respective build-*-rebuild-timeout brep's configuration
      // option is zero, then return timestamp_unknown to indicate 'never
      // expire'. Note that this value is less than any build timestamp value,
      // including timestamp_nonexistent.
      //
      // NOTE: there is a similar code in mod/mod-build-task.cxx.
      //
      auto build_expiration = [&now, &mod_ops] (
        optional<size_t> rebuild_timeout,
        const optional<pair<duration, duration>>& alt_interval,
        optional<size_t> alt_timeout,
        size_t normal_timeout)
      {
        duration t;

        // If the rebuild timeout is not specified explicitly, then calculate
        // it as the sum of the package rebuild timeout (normal rebuild
        // timeout if the alternative timeout is unspecified and the maximum
        // of two otherwise) and the build result timeout.
        //
        if (!rebuild_timeout)
        {
          if (normal_timeout == 0)
            return timestamp_unknown;

          chrono::seconds nt (normal_timeout);

          if (alt_interval)
          {
            // Calculate the alternative timeout, unless it is specified
            // explicitly.
            //
            if (!alt_timeout)
            {
              const duration& start (alt_interval->first);
              const duration& stop  (alt_interval->second);

              // Note that if the stop time is less than the start time then
              // the interval extends through the midnight.
              //
              t = start <= stop ? (stop - start) : ((24h - start) + stop);

              // If the normal rebuild time out is greater than 24 hours, then
              // increase the default alternative timeout by (normal - 24h)
              // (see build-alt-soft-rebuild-timeout configuration option for
              // details).
              //
              if (nt > 24h)
                t += nt - 24h;
            }
            else
              t = chrono::seconds (*alt_timeout);

            // Take the maximum of the alternative and normal rebuild
            // timeouts.
            //
            if (t < nt)
              t = nt;
          }
          else
            t = nt;

          // Summarize the rebuild and build result timeouts.
          //
          t += chrono::seconds (mod_ops.build_result_timeout ());
        }
        else
        {
          if (*rebuild_timeout == 0)
            return timestamp_unknown;

          t = chrono::seconds (*rebuild_timeout);
        }

        return now - t;
      };

      timestamp hard_rebuild_expiration (
        build_expiration (
          (ops.hard_rebuild_timeout_specified ()
           ? ops.hard_rebuild_timeout ()
           : optional<size_t> ()),
          (mod_ops.build_alt_hard_rebuild_start_specified ()
           ? make_pair (mod_ops.build_alt_hard_rebuild_start (),
                        mod_ops.build_alt_hard_rebuild_stop ())
           : optional<pair<duration, duration>> ()),
          (mod_ops.build_alt_hard_rebuild_timeout_specified ()
           ? mod_ops.build_alt_hard_rebuild_timeout ()
           : optional<size_t> ()),
          mod_ops.build_hard_rebuild_timeout ()));

      timestamp soft_rebuild_expiration (
        build_expiration (
          (ops.soft_rebuild_timeout_specified ()
           ? ops.soft_rebuild_timeout ()
           : optional<size_t> ()),
          (mod_ops.build_alt_soft_rebuild_start_specified ()
           ? make_pair (mod_ops.build_alt_soft_rebuild_start (),
                        mod_ops.build_alt_soft_rebuild_stop ())
           : optional<pair<duration, duration>> ()),
          (mod_ops.build_alt_soft_rebuild_timeout_specified ()
           ? mod_ops.build_alt_soft_rebuild_timeout ()
           : optional<size_t> ()),
          mod_ops.build_soft_rebuild_timeout ()));

      timestamp report_expiration (
        now - chrono::seconds (ops.report_timeout ()));

      for (bool ne (true); ne; )
      {
        transaction t (conn->begin ());

        // Query buildable packages (and cache the result).
        //
        auto bps (ppq.execute ());

        if ((ne = !bps.empty ()))
        {
          offset += bps.size ();

          for (auto& bp: bps)
          {
            shared_ptr<build_package> p (db.load<build_package> (bp.id));

            for (const build_config& c: configs)
            {
              if (exclude (p->builds,
                           p->constraints,
                           c,
                           configs.class_inheritance_map))
                continue;

              for (const pair<string, version>& t: toolchains)
              {
                id = build_id (p->id, c.name, c.target, t.first, t.second);

                // If the toolchain version is unspecified then search for the
                // latest build across all toolchain versions and search for a
                // specific build otherwise.
                //
                shared_ptr<build> b;

                if (id.toolchain_version.empty ())
                {
                  auto pbs (pbq.execute ());

                  if (!pbs.empty ())
                    b = move (pbs.begin ()->build);
                }
                else
                  b = db.find<build> (id);

                // Note that we consider a build as delayed if it is not
                // completed in the expected timeframe. So even if the build
                // task have been issued recently we may still consider the
                // build as delayed.
                //
                timestamp bht (b != nullptr
                               ? b->hard_timestamp
                               : timestamp_nonexistent);

                timestamp bst (b != nullptr
                               ? b->soft_timestamp
                               : timestamp_nonexistent);

                // Create the delay object to record a timestamp when the
                // package build could have potentially been started, unless
                // it already exists.
                //
                shared_ptr<build_delay> d (db.find<build_delay> (id));

                if (d == nullptr)
                {
                  // If the archived package has no build nor build delay
                  // for this configuration, then we assume that the
                  // configuration was added after the package tenant has
                  // been archived and so the package could have never been
                  // built for this configuration. Thus, we don't consider
                  // this build as delayed and so skip it.
                  //
                  if (bp.archived && b == nullptr)
                    continue;

                  // Use the build hard, soft, or status change timestamp (see
                  // the timestamps description for their ordering
                  // information) as the build delay tracking starting point
                  // and fallback to the current time if there is no build
                  // yet.
                  //
                  timestamp pts (
                    b == nullptr                 ? now :
                    bht != timestamp_nonexistent ? bht :
                    bst != timestamp_nonexistent ? bst :
                                                   b->timestamp);

                  d = make_shared<build_delay> (move (id.package.tenant),
                                                move (id.package.name),
                                                p->version,
                                                move (id.configuration),
                                                move (id.target),
                                                move (id.toolchain_name),
                                                t.second,
                                                pts);
                  db.persist (d);
                }

                // Handle package builds differently based on their tenant's
                // archive status.
                //
                // If the package is not archived then consider it as delayed
                // if it is not (re-)built by the expiration time. Otherwise,
                // consider it as delayed if it is unbuilt.
                //
                // We also don't need to report an unbuilt archived package
                // twice, as both soft and hard build delays.
                //
                bool hard_delayed;
                bool soft_delayed;

                if (!bp.archived)
                {
                  auto delayed = [&d] (timestamp bt, timestamp be)
                  {
                    timestamp t (bt != timestamp_nonexistent
                                 ? bt
                                 : d->package_timestamp);
                    return t <= be;
                  };

                  hard_delayed = delayed (bht, hard_rebuild_expiration);
                  soft_delayed = delayed (bst, soft_rebuild_expiration);
                }
                else
                {
                  hard_delayed = (bst == timestamp_nonexistent);
                  soft_delayed = false;
                }

                // Add hard/soft delays to the respective reports and collect
                // the delay for update, if it is reported.
                //
                // Note that we update the delay objects persistent state
                // later, after we successfully print the reports.
                //
                bool reported (false);

                if (hard_delayed)
                {
                  // If the report timeout is zero then report the delay
                  // unconditionally. Otherwise, report the active package
                  // build delay if the report timeout is expired and the
                  // archived package build delay if it was never reported.
                  // Note that fixing the building infrastructure won't help
                  // building an archived package, so reporting its build
                  // delays repeatedly is meaningless.
                  //
                  bool report (
                    ops.report_timeout () == 0 ||
                    (!bp.archived
                     ? d->report_hard_timestamp <= report_expiration
                     : d->report_hard_timestamp == timestamp_nonexistent));

                  if (report)
                  {
                    d->report_hard_timestamp = now;
                    reported = true;
                  }

                  hard_delays_report.add_delay (d, report);
                }

                if (soft_delayed)
                {
                  bool report (ops.report_timeout () == 0 ||
                               d->report_soft_timestamp <= report_expiration);

                  if (report)
                  {
                    d->report_soft_timestamp = now;
                    reported = true;
                  }

                  soft_delays_report.add_delay (d, report);
                }

                // If we don't consider the report timestamps for reporting
                // delays, it seems natural not to update these timestamps
                // either. Note that reporting all delays and still updating
                // the report timestamps can be achieved by specifying the
                // zero report timeout.
                //
                if (reported && ops.report_timeout_specified ())
                  update_delays.insert (move (d));
              }
            }
          }
        }

        t.commit ();
      }
    }

    // Print delay reports, if not empty.
    //
    if (!hard_delays_report.empty () || !soft_delays_report.empty ())
    try
    {
      cerr.exceptions (ostream::badbit | ostream::failbit);

      // Don't print the total delay count if the report timeout is zero since
      // all delays are reported in this case.
      //
      bool total (ops.report_timeout () != 0);

      hard_delays_report.print ("Package hard rebuild delays",
                                total,
                                ops.full_report ());

      // Separate reports with an empty line.
      //
      if (!hard_delays_report.empty () && !soft_delays_report.empty ())
        cerr << endl;

      soft_delays_report.print ("Package soft rebuild delays",
                                total,
                                ops.full_report ());
    }
    catch (const io_error&)
    {
      return 1; // Not much we can do on stderr writing failure.
    }

    // Persist the delay report timestamps.
    //
    if (!update_delays.empty ())
    {
      transaction t (db.begin ());

      for (shared_ptr<const build_delay> d: update_delays)
        db.update (d);

      t.commit ();
    }

    return 0;
  }
  catch (const database_locked&)
  {
    cerr << "brep-monitor or some other brep utility is running" << endl;
    return 2;
  }
  catch (const recoverable& e)
  {
    cerr << "recoverable database error: " << e << endl;
    return 3;
  }
  catch (const cli::exception& e)
  {
    cerr << "error: " << e << endl << help_info << endl;
    return 1;
  }
  catch (const failed&)
  {
    return 1; // Diagnostics has already been issued.
  }
  // Fully qualified to avoid ambiguity with odb exception.
  //
  catch (const std::exception& e)
  {
    cerr << "error: " << e << endl;
    return 1;
  }
}

int
main (int argc, char* argv[])
{
  return brep::main (argc, argv);
}