1 /*
2    BAREOS® - Backup Archiving REcovery Open Sourced
3 
4    Copyright (C) 2019-2019 Free Software Foundation Europe e.V.
5 
6    This program is Free Software; you can redistribute it and/or
7    modify it under the terms of version three of the GNU Affero General Public
8    License as published by the Free Software Foundation and included
9    in the file LICENSE.
10 
11    This program is distributed in the hope that it will be useful, but
12    WITHOUT ANY WARRANTY; without even the implied warranty of
13    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14    Affero General Public License for more details.
15 
16    You should have received a copy of the GNU Affero General Public License
17    along with this program; if not, write to the Free Software
18    Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
19    02110-1301, USA.
20 */
21 
22 #ifndef BAREOS_SRC_DIRD_SCHEDULER_JOB_ITEM_QUEUE_H_
23 #define BAREOS_SRC_DIRD_SCHEDULER_JOB_ITEM_QUEUE_H_
24 
25 #include "include/bareos.h"
26 
27 #include <queue>
28 #include <vector>
29 
30 namespace directordaemon {
31 
32 class RunResource;
33 class JobResource;
34 struct SchedulerJobItemQueuePrivate;
35 
36 struct SchedulerJobItem {
37   ~SchedulerJobItem() = default;
38 
39   SchedulerJobItem() = default;
40   SchedulerJobItem(const SchedulerJobItem& other) = default;
41   SchedulerJobItem(SchedulerJobItem&& other) = default;
42   SchedulerJobItem& operator=(const SchedulerJobItem& other) = default;
43   SchedulerJobItem& operator=(SchedulerJobItem&& other) = default;
44 
SchedulerJobItemSchedulerJobItem45   SchedulerJobItem(JobResource* job_in,
46                    RunResource* run_in,
47                    time_t runtime_in,
48                    int priority_in) noexcept
49       : job(job_in), run(run_in), runtime(runtime_in), priority(priority_in)
50   {
51     is_valid = job && runtime;
52   };
53 
54   bool operator==(const SchedulerJobItem& rhs) const
55   {
56     return runtime == rhs.runtime && job == rhs.job &&
57            priority == rhs.priority && run == rhs.run;
58   }
59 
60   bool operator!=(const SchedulerJobItem& rhs) const { return !(*this == rhs); }
61 
62   JobResource* job{nullptr};
63   RunResource* run{nullptr};
64   time_t runtime{0};
65   int priority{10};
66   bool is_valid{false};
67 };
68 
69 class SchedulerJobItemQueue {
70  public:
71   SchedulerJobItemQueue();
72   ~SchedulerJobItemQueue();
73 
74   SchedulerJobItem TopItem() const;
75   SchedulerJobItem TakeOutTopItem();
76   void EmplaceItem(JobResource* job, RunResource* run, time_t runtime);
77   bool Empty() const;
78   void Clear();
79 
80   SchedulerJobItemQueue(const SchedulerJobItemQueue& other) = delete;
81   SchedulerJobItemQueue(SchedulerJobItemQueue&& other) = delete;
82   SchedulerJobItemQueue& operator=(const SchedulerJobItemQueue& other) = delete;
83   SchedulerJobItemQueue& operator=(SchedulerJobItemQueue&& other) = delete;
84 
85  private:
86   static constexpr int default_priority{10};
87   std::unique_ptr<SchedulerJobItemQueuePrivate> impl_;
88 };
89 
90 }  // namespace directordaemon
91 
92 #endif  // BAREOS_SRC_DIRD_SCHEDULER_JOB_ITEM_QUEUE_H_
93