sapling/eden/fs/notifications/CommandNotifier.cpp
Michael Cuevas aa4732e433 refactor notifications
Summary:
Refactor the existing notification class so it uses the new Notifier interface. The notifier interface will be used to set up notifications/notification trays on different platforms.

The notifier interface is not fully completed yet. More methods will be added in a future diff.

As of now, we only have 1 Notifier, the command notifier. I hope to remove this notifier once we implement notifiers for all platforms that we support.

Reviewed By: chadaustin

Differential Revision: D34291809

fbshipit-source-id: a2a67af2683f1f88781428e8d88191f49e100e96
2022-03-01 17:52:58 -08:00

59 lines
1.5 KiB
C++

/*
* Copyright (c) Meta Platforms, Inc. and affiliates.
*
* This software may be used and distributed according to the terms of the
* GNU General Public License version 2.
*/
#include "eden/fs/notifications/CommandNotifier.h"
#include <folly/futures/Future.h>
#include "eden/fs/config/EdenConfig.h"
#include "eden/fs/utils/SpawnedProcess.h"
#include "eden/fs/utils/SystemError.h"
namespace facebook {
namespace eden {
namespace {
bool isGenericConnectivityError(const std::exception& err) {
int errnum = EIO;
if (auto* sys = dynamic_cast<const std::system_error*>(&err)) {
if (isErrnoError(*sys)) {
errnum = sys->code().value();
}
} else if (auto* timeout = dynamic_cast<const folly::FutureTimeout*>(&err)) {
errnum = ETIMEDOUT;
}
return errnum == EIO || errnum == ETIMEDOUT;
}
} // namespace
void CommandNotifier::showNetworkNotification(const std::exception& err) {
if (!isGenericConnectivityError(err)) {
return;
}
if (!canShowNotification()) {
return;
}
*lastShown_.wlock() = std::chrono::steady_clock::now();
std::vector<std::string> args;
if (folly::kIsWindows) {
args.emplace_back("powershell");
args.emplace_back("-NoProfile");
args.emplace_back("-Command");
} else {
args.emplace_back("/bin/sh");
args.emplace_back("-c");
}
args.emplace_back(
config_.getEdenConfig()->genericErrorNotificationCommand.getValue());
SpawnedProcess(args).detach();
}
} // namespace eden
} // namespace facebook