Welcome to mirror list, hosted at ThFree Co, Russian Federation.

run_on_incoming_connect_interval.cc « dird « src « core - github.com/bareos/bareos.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 0175b5ee9a09d72d1b01d5a62b8db7dc4b0ae20b (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
/*
   BAREOS® - Backup Archiving REcovery Open Sourced

   Copyright (C) 2000-2012 Free Software Foundation Europe e.V.
   Copyright (C) 2011-2016 Planets Communications B.V.
   Copyright (C) 2019-2022 Bareos GmbH & Co. KG

   This program is Free Software; you can redistribute it and/or
   modify it under the terms of version three of the GNU Affero General Public
   License as published by the Free Software Foundation and included
   in the file LICENSE.

   This program is distributed in the hope that it will be useful, but
   WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
   Affero General Public License for more details.

   You should have received a copy of the GNU Affero General Public License
   along with this program; if not, write to the Free Software
   Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
   02110-1301, USA.
*/

#include "include/bareos.h"
#include "cats/cats.h"
#include "dird/dird_conf.h"
#include "dird/get_database_connection.h"
#include "dird/job.h"
#include "dird/job_trigger.h"
#include "dird/director_jcr_impl.h"
#include "dird/run_on_incoming_connect_interval.h"
#include "dird/scheduler.h"
#include "dird/jcr_util.h"
#include <utility>

namespace directordaemon {

RunOnIncomingConnectInterval::RunOnIncomingConnectInterval(
    std::string client_name)
    : client_name_(std::move(client_name))
    , scheduler_(Scheduler::GetMainScheduler())
{
  // initialize database by job settings
}

RunOnIncomingConnectInterval::RunOnIncomingConnectInterval(
    std::string client_name,
    Scheduler& scheduler,
    BareosDb* db)
    : client_name_(std::move(client_name)), scheduler_(scheduler), db_(db)
{
  // constructor used for tests to inject mocked scheduler and database
}

time_t RunOnIncomingConnectInterval::FindLastJobStart(JobResource* job)
{
  JobControlRecord* jcr = NewDirectorJcr(DirdFreeJcr);
  SetJcrDefaults(jcr, job);
  auto db = db_ != nullptr ? db_ : GetDatabaseConnection(jcr);
  if (db == nullptr) {
    Dmsg0(200, "Could not retrieve a database connection.\n");
    return 0;
  }
  jcr->db = db;

  std::vector<char> stime;

  BareosDb::SqlFindResult result = jcr->db->FindLastJobStartTimeForJobAndClient(
      jcr, job->resource_name_, client_name_, stime);

  time_t time{-1};

  switch (result) {
    case BareosDb::SqlFindResult::kSuccess: {
      time_t time_converted = static_cast<time_t>(StrToUtime(stime.data()));
      time = time_converted == 0 ? -1 : time_converted;
      break;
    }
    case BareosDb::SqlFindResult::kEmptyResultSet:
      time = 0;
      break;
    default:
    case BareosDb::SqlFindResult::kError:
      time = -1;
      break;
  }

  FreeJcr(jcr);

  return time;
}

void RunOnIncomingConnectInterval::RunJobIfIntervalExceeded(
    JobResource* job,
    time_t last_start_time)
{
  using std::chrono::duration_cast;
  using std::chrono::hours;
  using std::chrono::seconds;
  using std::chrono::system_clock;

  bool interval_time_exceeded = false;
  bool job_ran_before = last_start_time != 0;

  if (job_ran_before) {
    auto timepoint_now = system_clock::now();
    auto timepoint_last_start = system_clock::from_time_t(last_start_time);

    auto diff_seconds
        = duration_cast<seconds>(timepoint_now - timepoint_last_start).count();

    auto maximum_interval_seconds
        = seconds(job->RunOnIncomingConnectInterval).count();

    interval_time_exceeded = diff_seconds > maximum_interval_seconds;
  }

  if (!job_ran_before || interval_time_exceeded) {
    Dmsg1(800, "Add job %s to scheduler queue.\n", job->resource_name_);
    scheduler_.AddJobWithNoRunResourceToQueue(job, JobTrigger::kClient);
  }
}

void RunOnIncomingConnectInterval::Run()
{
  std::vector<JobResource*> job_resources
      = GetAllJobResourcesByClientName(client_name_);

  if (job_resources.empty()) { return; }

  for (auto job : job_resources) {
    if (job->RunOnIncomingConnectInterval != 0) {
      time_t last_start_time = FindLastJobStart(job);
      if (last_start_time != -1) {
        Dmsg2(800, "Try RunOnIncomingConnectInterval job %s for client %s.\n",
              job->resource_name_, client_name_.c_str());
        RunJobIfIntervalExceeded(job, last_start_time);
      }
    }
  }
}

}  // namespace directordaemon