proxygen
wangle::FilePoller Class Reference

#include <FilePoller.h>

Inheritance diagram for wangle::FilePoller:
NoDiskPoller

Classes

struct  FileData
 
struct  FileModificationData
 
class  ThreadProtector
 

Public Types

using Cob = std::function< void()>
 
using Condition = std::function< bool(const FileModificationData &, const FileModificationData &)>
 

Public Member Functions

 FilePoller (std::chrono::milliseconds pollInterval=kDefaultPollInterval)
 
virtual ~FilePoller ()
 
 FilePoller (const FilePoller &other)=delete
 
FilePolleroperator= (const FilePoller &other)=delete
 
 FilePoller (FilePoller &&other)=delete
 
FilePoller && operator= (FilePoller &&other)=delete
 
void addFileToTrack (const std::string &fileName, Cob yCob, Cob nCob=nullptr, Condition condition=fileTouchedCondInternal)
 
void removeFileToTrack (const std::string &fileName)
 
void stop ()
 

Static Public Member Functions

static Condition fileTouchedWithinCond (std::chrono::seconds expireTime)
 
static Condition doAlwaysCond ()
 
static Condition fileTouchedCond ()
 

Protected Member Functions

virtual FileModificationData getFileModData (const std::string &path) noexcept
 

Private Types

using FileDatas = std::unordered_map< std::string, FileData >
 

Private Member Functions

void checkFiles () noexcept
 
void initFileData (const std::string &fName, FileData &fData) noexcept
 
void init (std::chrono::milliseconds pollInterval)
 

Static Private Member Functions

static bool fileTouchedWithinCondInternal (const FilePoller::FileModificationData &, const FilePoller::FileModificationData &fModData, std::chrono::seconds expireTime)
 
static bool doAlwaysCondInternal (const FileModificationData &, const FileModificationData &)
 
static bool fileTouchedCondInternal (const FileModificationData &oldModData, const FileModificationData &modData)
 

Private Attributes

FileDatas fileDatum_
 
std::mutex filesMutex_
 
uint64_t pollerId_ {0}
 
std::shared_ptr< folly::FunctionSchedulerscheduler_
 

Static Private Attributes

static constexpr std::chrono::milliseconds kDefaultPollInterval
 

Detailed Description

Polls for updates in the files. This poller uses modified times to track changes in the files, so it is the responsibility of the caller to check whether the contents have actually changed. It also assumes that when the file is modified, the modfied time changes. This is a reasonable assumption to make, so it should not be used in situations where files may be modified without changing the modified time.

Definition at line 41 of file FilePoller.h.

Member Typedef Documentation

using wangle::FilePoller::Cob = std::function<void()>

Definition at line 50 of file FilePoller.h.

using wangle::FilePoller::Condition = std::function< bool(const FileModificationData&, const FileModificationData&)>

Definition at line 54 of file FilePoller.h.

using wangle::FilePoller::FileDatas = std::unordered_map<std::string, FileData>
private

Definition at line 113 of file FilePoller.h.

Constructor & Destructor Documentation

wangle::FilePoller::FilePoller ( std::chrono::milliseconds  pollInterval = kDefaultPollInterval)
explicit

Definition at line 65 of file FilePoller.cpp.

References folly::init().

65  {
66  init(pollInterval);
67 }
void init(std::chrono::milliseconds pollInterval)
Definition: FilePoller.cpp:71
wangle::FilePoller::~FilePoller ( )
virtual

Definition at line 69 of file FilePoller.cpp.

References stop().

69 { stop(); }
wangle::FilePoller::FilePoller ( const FilePoller other)
delete
wangle::FilePoller::FilePoller ( FilePoller &&  other)
delete

Member Function Documentation

void wangle::FilePoller::addFileToTrack ( const std::string fileName,
Cob  yCob,
Cob  nCob = nullptr,
Condition  condition = fileTouchedCondInternal 
)

Definition at line 115 of file FilePoller.cpp.

Referenced by wangle::MultiFilePoller::registerFiles(), and TEST_F().

119  {
120  if (fileName.empty()) {
121  // ignore empty file paths
122  return;
123  }
125  LOG(ERROR) << "Adding files from a callback is disallowed";
126  return;
127  }
128  std::lock_guard<std::mutex> lg(filesMutex_);
129  fileDatum_[fileName] = FileData(yCob, nCob, condition);
130  initFileData(fileName, fileDatum_[fileName]);
131 }
FileDatas fileDatum_
Definition: FilePoller.h:143
void initFileData(const std::string &fName, FileData &fData) noexcept
Definition: FilePoller.cpp:107
std::mutex filesMutex_
Definition: FilePoller.h:146
void wangle::FilePoller::checkFiles ( )
privatenoexcept

Definition at line 92 of file FilePoller.cpp.

References folly::pushmi::__adl::tp.

Referenced by fileTouchedCondInternal().

92  {
93  std::lock_guard<std::mutex> lg(filesMutex_);
94  ThreadProtector tp;
95  for (auto& fData : fileDatum_) {
96  auto modData = getFileModData(fData.first);
97  auto& fileData = fData.second;
98  if (fileData.condition(fileData.modData, modData) && fileData.yCob) {
99  fileData.yCob();
100  } else if (fileData.nCob) {
101  fileData.nCob();
102  }
103  fileData.modData = modData;
104  }
105 }
virtual FileModificationData getFileModData(const std::string &path) noexcept
Definition: FilePoller.cpp:146
FileDatas fileDatum_
Definition: FilePoller.h:143
std::mutex filesMutex_
Definition: FilePoller.h:146
static Condition wangle::FilePoller::doAlwaysCond ( )
inlinestatic

Definition at line 88 of file FilePoller.h.

References doAlwaysCondInternal().

88  {
89  return doAlwaysCondInternal;
90  }
static bool doAlwaysCondInternal(const FileModificationData &, const FileModificationData &)
Definition: FilePoller.h:123
static bool wangle::FilePoller::doAlwaysCondInternal ( const FileModificationData ,
const FileModificationData  
)
inlinestaticprivate

Definition at line 123 of file FilePoller.h.

Referenced by doAlwaysCond().

125  {
126  return true;
127  }
static Condition wangle::FilePoller::fileTouchedCond ( )
inlinestatic

Definition at line 92 of file FilePoller.h.

References fileTouchedCondInternal(), getFileModData(), folly::pushmi::__adl::noexcept(), and string.

92  {
94  }
static bool fileTouchedCondInternal(const FileModificationData &oldModData, const FileModificationData &modData)
Definition: FilePoller.h:129
static bool wangle::FilePoller::fileTouchedCondInternal ( const FileModificationData oldModData,
const FileModificationData modData 
)
inlinestaticprivate

Definition at line 129 of file FilePoller.h.

References checkFiles(), wangle::FilePoller::FileModificationData::exists, init(), initFileData(), wangle::FilePoller::FileModificationData::modTime, folly::pushmi::__adl::noexcept(), and string.

Referenced by fileTouchedCond().

131  {
132  const bool fileStillExists = oldModData.exists && modData.exists;
133  const bool fileTouched = modData.modTime != oldModData.modTime;
134  const bool fileCreated = !oldModData.exists && modData.exists;
135  return (fileStillExists && fileTouched) || fileCreated;
136  }
static Condition wangle::FilePoller::fileTouchedWithinCond ( std::chrono::seconds  expireTime)
inlinestatic

Definition at line 80 of file FilePoller.h.

References folly::netops::bind(), and fileTouchedWithinCondInternal().

80  {
81  return std::bind(
83  std::placeholders::_1,
84  std::placeholders::_2,
85  expireTime);
86  }
int bind(NetworkSocket s, const sockaddr *name, socklen_t namelen)
Definition: NetOps.cpp:76
static bool fileTouchedWithinCondInternal(const FilePoller::FileModificationData &, const FilePoller::FileModificationData &fModData, std::chrono::seconds expireTime)
Definition: FilePoller.h:115
static bool wangle::FilePoller::fileTouchedWithinCondInternal ( const FilePoller::FileModificationData ,
const FilePoller::FileModificationData fModData,
std::chrono::seconds  expireTime 
)
inlinestaticprivate

Definition at line 115 of file FilePoller.h.

References wangle::FilePoller::FileModificationData::exists, wangle::FilePoller::FileModificationData::modTime, and folly::detail::distributed_mutex::time().

Referenced by fileTouchedWithinCond().

118  {
119  return fModData.exists &&
120  (time(nullptr) < fModData.modTime + expireTime.count());
121  }
std::chrono::nanoseconds time()
FilePoller::FileModificationData wangle::FilePoller::getFileModData ( const std::string path)
protectedvirtualnoexcept

Reimplemented in NoDiskPoller.

Definition at line 146 of file FilePoller.cpp.

References deadlock::info().

Referenced by fileTouchedCond().

147  {
148  struct stat info;
149  int ret = stat(path.c_str(), &info);
150  if (ret != 0) {
151  return FileModificationData{false, 0};
152  }
153  return FileModificationData{true, info.st_mtime};
154 }
def info()
Definition: deadlock.py:447
void wangle::FilePoller::init ( std::chrono::milliseconds  pollInterval)
private

Definition at line 71 of file FilePoller.cpp.

References context.

Referenced by fileTouchedCondInternal().

71  {
72  auto context = contextSingleton.try_get();
73  if (!context) {
74  LOG(ERROR) << "Poller context requested after destruction.";
75  return;
76  }
77  pollerId_ = context->getNextId();
78  scheduler_ = context->getScheduler();
79  scheduler_->addFunction(
80  [this] { this->checkFiles(); },
81  pollInterval,
82  folly::to<std::string>(pollerId_));
83 }
std::shared_ptr< folly::FunctionScheduler > scheduler_
Definition: FilePoller.h:153
context
Definition: CMakeCache.txt:563
uint64_t pollerId_
Definition: FilePoller.h:148
void checkFiles() noexcept
Definition: FilePoller.cpp:92
void wangle::FilePoller::initFileData ( const std::string fName,
FileData fData 
)
privatenoexcept

Definition at line 107 of file FilePoller.cpp.

Referenced by fileTouchedCondInternal().

109  {
110  auto modData = getFileModData(fName);
111  fData.modData.exists = modData.exists;
112  fData.modData.modTime = modData.modTime;
113 }
virtual FileModificationData getFileModData(const std::string &path) noexcept
Definition: FilePoller.cpp:146
FilePoller& wangle::FilePoller::operator= ( const FilePoller other)
delete
FilePoller&& wangle::FilePoller::operator= ( FilePoller &&  other)
delete
void wangle::FilePoller::removeFileToTrack ( const std::string fileName)

Definition at line 133 of file FilePoller.cpp.

Referenced by wangle::MultiFilePoller::cancelCallback().

133  {
134  if (fileName.empty()) {
135  // ignore
136  return;
137  }
139  LOG(ERROR) << "Adding files from a callback is disallowed";
140  return;
141  }
142  std::lock_guard<std::mutex> lg(filesMutex_);
143  fileDatum_.erase(fileName);
144 }
FileDatas fileDatum_
Definition: FilePoller.h:143
std::mutex filesMutex_
Definition: FilePoller.h:146
void wangle::FilePoller::stop ( )

Definition at line 85 of file FilePoller.cpp.

85  {
86  if (scheduler_) {
87  scheduler_->cancelFunctionAndWait(
88  folly::to<std::string>(pollerId_));
89  }
90 }
std::shared_ptr< folly::FunctionScheduler > scheduler_
Definition: FilePoller.h:153
uint64_t pollerId_
Definition: FilePoller.h:148

Member Data Documentation

FileDatas wangle::FilePoller::fileDatum_
private

Definition at line 143 of file FilePoller.h.

std::mutex wangle::FilePoller::filesMutex_
private

Definition at line 146 of file FilePoller.h.

constexpr std::chrono::milliseconds wangle::FilePoller::kDefaultPollInterval
staticprivate
Initial value:
=
std::chrono::milliseconds(10000)

Definition at line 100 of file FilePoller.h.

uint64_t wangle::FilePoller::pollerId_ {0}
private

Definition at line 148 of file FilePoller.h.

std::shared_ptr<folly::FunctionScheduler> wangle::FilePoller::scheduler_
private

Definition at line 153 of file FilePoller.h.


The documentation for this class was generated from the following files: