Skip to content

Commit

Permalink
refactored lockfile implementation, address PR comments
Browse files Browse the repository at this point in the history
  • Loading branch information
RogerZhongAWS committed Apr 9, 2022
1 parent ba5b580 commit 6f23c3e
Show file tree
Hide file tree
Showing 8 changed files with 46 additions and 163 deletions.
6 changes: 6 additions & 0 deletions source/config/Config.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -251,6 +251,12 @@ bool PlainConfig::LoadFromEnvironment()
}
}

const char *lockFilePathStr = std::getenv("LOCK_FILE_PATH");
if (lockFilePathStr)
{
lockFilePath = lockFilePathStr;
}

return logConfig.LoadFromEnvironment() && jobs.LoadFromEnvironment() && tunneling.LoadFromEnvironment() &&
deviceDefender.LoadFromEnvironment() && fleetProvisioning.LoadFromEnvironment() &&
fleetProvisioningRuntimeConfig.LoadFromEnvironment() && pubSub.LoadFromEnvironment() &&
Expand Down
1 change: 1 addition & 0 deletions source/config/Config.h
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,7 @@ namespace Aws
Aws::Crt::Optional<std::string> thingName;

aws_mem_trace_level memTraceLevel{AWS_MEMTRACE_NONE};
std::string lockFilePath;

struct LogConfig : public LoadableFromJsonAndCliAndEnvironment
{
Expand Down
41 changes: 34 additions & 7 deletions source/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
#include "Version.h"
#include "config/Config.h"
#include "util/EnvUtils.h"
#include "util/LockFileUtils.h"
#include "util/LockFile.h"
#include "util/Retry.h"

#if !defined(EXCLUDE_DD)
Expand Down Expand Up @@ -87,10 +87,34 @@ const char *TAG = "Main.cpp";

vector<Feature *> features;
shared_ptr<SharedCrtResourceManager> resourceManager;
unique_ptr<LockFile> lockFile;
mutex featuresReadWriteLock;
bool attemptingShutdown{false};
Config config;

/**
* TODO: For future expandability of main
* Currently creates a lockfile to prevent the creation of multiple Device Client processes.
* @return true if no exception is caught, false otherwise
*/
bool init()
{
try
{
string filename = config.config.lockFilePath;
if (!filename.empty())
{
lockFile = unique_ptr<LockFile>(new LockFile{filename});
}
}
catch (std::runtime_error &e)
{
std::cout << e.what() << std::endl;
return false;
}
return true;
}

/**
* Attempts to perform a graceful shutdown of each running feature. If this function is
* executed more than once, it will terminate immediately.
Expand Down Expand Up @@ -121,7 +145,6 @@ void shutdown()
resourceManager.get()->disconnect();
#endif
LoggerFactory::getLoggerInstance().get()->shutdown();
LockFileUtils::ProcessUnlock();
exit(0);
}
}
Expand Down Expand Up @@ -166,7 +189,8 @@ void handle_feature_stopped(Feature *feature)
void attemptConnection()
{
Retry::ExponentialRetryConfig retryConfig = {10 * 1000, 900 * 1000, -1, nullptr};
auto publishLambda = []() -> bool {
auto publishLambda = []() -> bool
{
int connectionStatus = resourceManager.get()->establishConnection(config.config);
if (SharedCrtResourceManager::ABORT == connectionStatus)
{
Expand All @@ -189,8 +213,8 @@ void attemptConnection()
return false;
}
};
std::thread attemptConnectionThread(
[retryConfig, publishLambda] { Retry::exponentialBackoff(retryConfig, publishLambda); });
std::thread attemptConnectionThread([retryConfig, publishLambda]
{ Retry::exponentialBackoff(retryConfig, publishLambda); });
attemptConnectionThread.join();
}

Expand Down Expand Up @@ -272,7 +296,6 @@ namespace Aws

int main(int argc, char *argv[])
{
LockFileUtils::ProcessLock();
CliArgs cliArgs;
if (!Config::ParseCliArgs(argc, argv, cliArgs) || !config.init(cliArgs))
{
Expand All @@ -297,6 +320,11 @@ int main(int argc, char *argv[])
LOG_WARN(TAG, "Unable to append current working directory to PATH environment variable.");
}

if (!init())
{
return -1;
}

LOGM_INFO(TAG, "Now running AWS IoT Device Client version %s", DEVICE_CLIENT_VERSION_FULL);

// Register for listening to interrupt signals
Expand Down Expand Up @@ -469,6 +497,5 @@ int main(int argc, char *argv[])
break;
}
}

return 0;
}
9 changes: 4 additions & 5 deletions source/util/LockFile.cpp
Original file line number Diff line number Diff line change
@@ -1,18 +1,17 @@
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
// SPDX-License-Identifier: Apache-2.0

#include <stdexcept>
#include "LockFile.h"
#include <stdexcept>

using namespace std;
using namespace Aws::Iot::DeviceClient::Util;

LockFile::LockFile(const std::string& filename)
: filename(filename)
, file(fopen(filename.c_str(), "wx"))
LockFile::LockFile(const std::string &filename) : filename(filename), file(fopen(filename.c_str(), "wx"))
{
if (!file)
{
throw std::runtime_error{"unable to open lockfile"};
throw std::runtime_error{"Unable to open lockfile... please check permissions and if device client is already running."};
}
flockfile(file);
}
Expand Down
2 changes: 1 addition & 1 deletion source/util/LockFile.h
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ namespace Aws
FILE *file{nullptr};

public:
LockFile(const std::string& filename);
LockFile(const std::string &filename);
~LockFile();

// Non-copyable.
Expand Down
68 changes: 0 additions & 68 deletions source/util/LockFileUtils.cpp

This file was deleted.

52 changes: 0 additions & 52 deletions source/util/LockFileUtils.h

This file was deleted.

30 changes: 0 additions & 30 deletions test/util/TestLockFileUtils.cpp

This file was deleted.

0 comments on commit 6f23c3e

Please sign in to comment.