ReportPackerInterface.hpp 3.96 KB
Newer Older
1
2
/*
 * @project        The CERN Tape Archive (CTA)
3
 * @copyright      Copyright(C) 2003-2021 CERN
4
5
6
7
 * @license        This program is free software: you can redistribute it and/or modify
 *                 it under the terms of the GNU General Public License as published by
 *                 the Free Software Foundation, either version 3 of the License, or
 *                 (at your option) any later version.
8
 *
9
10
11
12
 *                 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 General Public License for more details.
13
 *
14
15
16
 *                 You should have received a copy of the GNU General Public License
 *                 along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */
17

18
#pragma once
19

Victor Kotlyar's avatar
Victor Kotlyar committed
20
#include "common/log/LogContext.hpp"
21
#include "tapeserver/castor/tape/tapeserver/utils/suppressUnusedVariable.hpp"
22

23
24
25
26
27
28
#include <memory>

namespace castor {
namespace tape {
namespace tapeserver {
namespace daemon {
29
  
30
namespace detail{
31
  //nameholder
32
33
  struct Recall{};
  struct Migration{};
34
35
36
37
38
39
40
  
  // Enum describing the type of client. Some clients need batched reports,
  // some prefer reports file by file
  enum ReportBatching {
    ReportInBulk,
    ReportByFile
  };
41
}
42
43
44
45

// Forward declaration to avoid circular inclusions.
class TaskWatchDog;

46
47
48
49
/**
 * Utility class that should be inherited privately/protectedly 
 * the type PlaceHolder is either detail::Recall or detail::Migration
 */
50
template <class PlaceHolder> class ReportPackerInterface{
51
  public :
52
53
54
55
56
  
  // Pass a reference to the watchdog for initial process reporting.
  void setWatchdog(TaskWatchDog & wd) {
    m_watchdog = &wd;
  }
57

58
  protected:
59
    virtual ~ReportPackerInterface() {}
Victor Kotlyar's avatar
Victor Kotlyar committed
60
    ReportPackerInterface(cta::log::LogContext lc):
Daniele Kruse's avatar
Daniele Kruse committed
61
    m_lc(lc),
62
    m_reportBatching(detail::ReportInBulk),m_watchdog(NULL) {}
63
64
65
66
67
68
69
  
  /**
   * Log a set of files independently of the success/failure 
   * @param c The set of files to log
   * @param msg The message to be append at the end.
   */
  template <class C> void logReport(const C& c,const std::string& msg){
Victor Kotlyar's avatar
Victor Kotlyar committed
70
71
    using cta::log::LogContext;
    using cta::log::Param;
72
73
      for(typename C::const_iterator it=c.begin();it!=c.end();++it)
      {
Victor Kotlyar's avatar
Victor Kotlyar committed
74
        cta::log::ScopedParamContainer sp(m_lc);
75
        sp.add("fileId",(*it)->fileid())
76
77
78
          .add("NSFSEQ", (*it)->fseq())
          .add("NSHOST", (*it)->nshost())
          .add("NSFILETRANSACTIONID", (*it)->fileTransactionId());
Victor Kotlyar's avatar
Victor Kotlyar committed
79
        m_lc.log(cta::log::INFO,msg);
80
81
      }
  }  
82
83
84
85
86
87
88

  /**
   * Log a set of files independently of the success/failure 
   * @param c The set of files to log
   * @param msg The message to be append at the end.
   */
  template <class C> void logReportWithError(const C& c,const std::string& msg){
Victor Kotlyar's avatar
Victor Kotlyar committed
89
90
    using cta::log::LogContext;
    using cta::log::Param;
91
92
      for(typename C::const_iterator it=c.begin();it!=c.end();++it)
      {
Victor Kotlyar's avatar
Victor Kotlyar committed
93
        cta::log::ScopedParamContainer sp(m_lc);
94
        sp.add("fileId",(*it)->fileid())
95
96
97
98
99
          .add("NSFSEQ", (*it)->fseq())
          .add("NSHOST", (*it)->nshost())
          .add("NSFILETRANSACTIONID", (*it)->fileTransactionId())
          .add("ErrorMessage", (*it)->errorMessage())
          .add("ErrorCode", (*it)->errorCode());
Victor Kotlyar's avatar
Victor Kotlyar committed
100
        m_lc.log(cta::log::INFO,msg);
101
102
      }
  }
103
104
  
  /**
105
   * The  log context, copied due to threads
106
   */
Victor Kotlyar's avatar
Victor Kotlyar committed
107
  cta::log::LogContext m_lc;
108
  
109
110
111
112
  /**
   * Define how we should report to the client (by file/in bulk).
   */  
  enum detail::ReportBatching m_reportBatching;
113
  public:
114
115
116
117
118
119

  /**
   * Turn off the packing of the reports by the report packer.
   * This is used for recalls driven by read_tp.
   */
  virtual void disableBulk() { m_reportBatching = detail::ReportByFile; }
120
121
122
123
124
125
126
  
  /**
   * Pointer to the watchdog, so we can communicate communication errors
   * and end of session results to the initial process
   */
  TaskWatchDog * m_watchdog;
  
127
128
129
130
131
};

}}}}