1 /*
2    BAREOS® - Backup Archiving REcovery Open Sourced
3 
4    Copyright (C) 2000-2011 Free Software Foundation Europe e.V.
5    Copyright (C) 2011-2012 Planets Communications B.V.
6    Copyright (C) 2013-2020 Bareos GmbH & Co. KG
7 
8    This program is Free Software; you can redistribute it and/or
9    modify it under the terms of version three of the GNU Affero General Public
10    License as published by the Free Software Foundation and included
11    in the file LICENSE.
12 
13    This program is distributed in the hope that it will be useful, but
14    WITHOUT ANY WARRANTY; without even the implied warranty of
15    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16    Affero General Public License for more details.
17 
18    You should have received a copy of the GNU Affero General Public License
19    along with this program; if not, write to the Free Software
20    Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
21    02110-1301, USA.
22 */
23 /*
24  * Kern Sibbald, May MM, major revision December MMIII
25  */
26 /**
27  * @file
28  * BAREOS scheduler
29  *
30  * It looks at what jobs are to be run and when
31  * and waits around until it is time to
32  * fire them up.
33  */
34 
35 #include "include/bareos.h"
36 #include "dird.h"
37 #include "dird/dird_globals.h"
38 #include "dird/scheduler.h"
39 #include "dird/scheduler_private.h"
40 #include "dird/scheduler_time_adapter.h"
41 #include "dird/jcr_private.h"
42 #include "dird/job.h"
43 #include "dird/storage.h"
44 #include "include/make_unique.h"
45 #include "lib/parse_conf.h"
46 
47 class JobControlRecord;
48 
49 namespace directordaemon {
50 
51 class JobResource;
52 class SchedulerTimeAdapter;
53 
54 static constexpr int debuglevel = 200;
55 
GetMainScheduler()56 Scheduler& Scheduler::GetMainScheduler() noexcept
57 {
58   static Scheduler scheduler;
59   return scheduler;
60 }
61 
Scheduler()62 Scheduler::Scheduler() noexcept : impl_(std::make_unique<SchedulerPrivate>()){};
63 
Scheduler(std::unique_ptr<SchedulerTimeAdapter> time_adapter,std::function<void (JobControlRecord *)> ExecuteJob)64 Scheduler::Scheduler(std::unique_ptr<SchedulerTimeAdapter> time_adapter,
65                      std::function<void(JobControlRecord*)> ExecuteJob) noexcept
66     : impl_(std::make_unique<SchedulerPrivate>(std::move(time_adapter),
67                                                std::move(ExecuteJob)))
68 {
69   // constructor used for tests to inject mocked time adapter and callbacks
70 }
71 
72 Scheduler::~Scheduler() = default;
73 
AddJobWithNoRunResourceToQueue(JobResource * job,JobTrigger job_trigger)74 void Scheduler::AddJobWithNoRunResourceToQueue(JobResource* job,
75                                                JobTrigger job_trigger)
76 {
77   impl_->AddJobWithNoRunResourceToQueue(job, job_trigger);
78 }
79 
Run()80 void Scheduler::Run()
81 {
82   while (impl_->active) {
83     Dmsg0(debuglevel, "Scheduler Cycle\n");
84     impl_->FillSchedulerJobQueueOrSleep();
85     impl_->WaitForJobsToRun();
86   }
87 }
88 
Terminate()89 void Scheduler::Terminate()
90 {
91   impl_->active = false;
92   impl_->time_adapter->time_source_->Terminate();
93 }
94 
ClearQueue()95 void Scheduler::ClearQueue() { impl_->prioritised_job_item_queue.Clear(); }
96 
97 } /* namespace directordaemon */
98