NCBI C++ ToolKit
ns_service_thread.cpp
Go to the documentation of this file.

Go to the SVN repository for this file.

1 /* $Id: ns_service_thread.cpp 71715 2016-03-24 14:13:07Z satskyse $
2  * ===========================================================================
3  *
4  * PUBLIC DOMAIN NOTICE
5  * National Center for Biotechnology Information
6  *
7  * This software/database is a "United States Government Work" under the
8  * terms of the United States Copyright Act. It was written as part of
9  * the author's official duties as a United States Government employee and
10  * thus cannot be copyrighted. This software/database is freely available
11  * to the public for use. The National Library of Medicine and the U.S.
12  * Government have not placed any restriction on its use or reproduction.
13  *
14  * Although all reasonable efforts have been taken to ensure the accuracy
15  * and reliability of the software and data, the NLM and the U.S.
16  * Government do not and cannot warrant the performance or results that
17  * may be obtained by using this software or data. The NLM and the U.S.
18  * Government disclaim all warranties, express or implied, including
19  * warranties of performance, merchantability or fitness for any particular
20  * purpose.
21  *
22  * Please cite the author in any work or product based on this material.
23  *
24  * ===========================================================================
25  *
26  * Authors: Denis Vakatov (design), Sergey Satskiy (implementation)
27  *
28  * File Description: NetSchedule service thread
29  *
30  *
31  */
32 
33 #include <ncbi_pch.hpp>
34 #include <corelib/request_ctx.hpp>
35 
36 #include "queue_database.hpp"
37 #include "ns_service_thread.hpp"
39 #include "ns_handler.hpp"
40 #include "ns_server.hpp"
41 
42 
44 
45 
46 // The thread does 2 service things:
47 // - check for drained shutdown every 10 seconds
48 // - logging statistics counters every 100 seconds if logging is on
50 {
51  if (!m_Host.ShouldRun())
52  return;
53 
54  time_t current_time = time(0);
55 
56  // Check for shutdown is done every 10 seconds
57  if (current_time - m_LastDrainCheck >= 10) {
59  m_LastDrainCheck = current_time;
60  }
61 
62  // Check that the config file is the original one every 60 seconds
63  if (current_time - m_LastConfigFileCheck >= 60) {
65  m_LastConfigFileCheck = current_time;
66  }
67 
68  if (m_JobCountersInterval > 0) {
69  if (current_time - m_LastJobCountersOutput >= m_JobCountersInterval) {
70  m_LastJobCountersOutput = current_time;
72  }
73  }
74 
76  return;
77 
78  if (current_time - m_LastStatisticsOutput >= m_StatisticsInterval) {
79  // Here: it's time to log counters
80  m_LastStatisticsOutput = current_time;
81 
82  // Print statistics for all the queues
83  size_t aff_count = 0;
84  m_QueueDB.PrintStatistics(aff_count);
86  }
87 }
88 
89 
91 {
92  if (m_Server.IsDrainShutdown() == false)
93  return; // No request to shut down
94 
96  return; // There are still submitters
97 
98  if (m_QueueDB.AnyJobs())
99  return; // There are still jobs
100 
101  // 0 is a signal number
102  // true means the DB has been drained successfully
103  m_Server.SetShutdownFlag(0, true);
104 }
105 
106 
108 {
110  vector<string> config_checksum_warnings;
111  string config_checksum = NS_GetConfigFileChecksum(
112  app->GetConfigPath(), config_checksum_warnings);
113  if (config_checksum == m_Server.GetDiskConfigFileChecksum()) {
114  // The current is the same as it was last time
115  return;
116  }
117 
118  // Memorize the current disk config file checksum
119  m_Server.SetDiskConfigFileChecksum(config_checksum);
120  if (!config_checksum_warnings.empty()) {
121  string alert_msg;
122  for (vector<string>::const_iterator
123  k = config_checksum_warnings.begin();
124  k != config_checksum_warnings.end(); ++k) {
125  if (!alert_msg.empty())
126  alert_msg += "\n";
127  alert_msg += *k;
128  ERR_POST(*k);
129  }
131  return;
132  }
133 
134 
135  // Here: the sum has been calculated properly. Compare it with the RAM
136  // version
137  if (config_checksum != m_Server.GetRAMConfigFileChecksum()) {
138  string msg = "The configuration file on the disk "
139  "does not match the currently loaded one: " +
140  app->GetConfigPath();
141  ERR_POST(msg);
143  }
144 }
145 
147 
virtual bool ShouldRun()=0
static CNcbiApplication * Instance(void)
Singleton method.
Definition: ncbiapp.cpp:264
string GetDiskConfigFileChecksum(void) const
Definition: ns_server.hpp:202
void RegisterAlert(EAlertType alert_type, const string &message)
Definition: ns_server.cpp:643
void SetShutdownFlag(int signum=0, bool db_was_drained=false)
Definition: ns_server.cpp:382
void SetDiskConfigFileChecksum(const string &checksum)
Definition: ns_server.cpp:781
bool IsDrainShutdown(void) const
Definition: ns_server.hpp:118
string GetRAMConfigFileChecksum(void) const
Definition: ns_server.hpp:199
unsigned int GetCurrentSubmitsCounter(void)
Definition: ns_server.hpp:125
void PrintStatistics(size_t &aff_count)
bool AnyJobs(void) const
void PrintJobCounters(void)
void x_CheckDrainShutdown(void)
CNetScheduleServer & m_Server
CBackgroundHost & m_Host
CQueueDataBase & m_QueueDB
void x_CheckConfigFile(void)
const unsigned int & m_JobCountersInterval
virtual void DoJob(void)
Payload function.
time_t m_LastJobCountersOutput
const unsigned int & m_StatisticsInterval
const bool & m_StatisticsLogging
static void PrintServerWide(size_t affinities)
const string & GetConfigPath(void) const
Get the full path to the configuration file (if any) we ended up using.
#define ERR_POST(message)
Error posting with file, line number information but without error codes.
Definition: ncbidiag.hpp:186
#define END_NCBI_SCOPE
End previously defined NCBI scope.
Definition: ncbistl.hpp:103
#define BEGIN_NCBI_SCOPE
Define ncbi namespace.
Definition: ncbistl.hpp:100
@ eConfigOutOfSync
Definition: ns_alert.hpp:53
string NS_GetConfigFileChecksum(const string &file_name, vector< string > &warnings)
Definition: ns_util.cpp:605
Defines CRequestContext class for NCBI C++ diagnostic API.
Modified on Sun May 05 05:21:45 2024 by modify_doxy.py rev. 669887