Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Lock file Optional when running logger #11

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,8 @@ public static void saveConfiguration(SSHRemote remote, CameraSettings settings,
deploy.addVariable("RESTART_LOGGER", restartonSave ? "true" : "false");
deploy.addTextFile("CAMERA_SETTINGS", "CameraSettings.yaml", CameraSettingsLoader.toString(settings), getCameraSettingsFile(remote), false);
deploy.addTextFile("STATIC_HOST_LIST", "ControllerHosts.yaml", StaticHostListLoader.toString(staticHostList), getHostsFile(remote), false);
deploy.deploy("if ${RESTART_LOGGER}; then sudo /bin/systemctl restart ihmc-logger.service; echo \"Restarted logger\"; else echo \"Skipped logger restart\"; fi");
deploy.deploy(
"if ${RESTART_LOGGER}; then sudo /bin/systemctl restart ihmc-logger.service; echo \"Restarted logger\"; else echo \"Skipped logger restart\"; fi");
}
catch (IOException e)
{
Expand Down Expand Up @@ -57,19 +58,25 @@ public static StaticHostList loadStaticHostList(SSHRemote remote) throws IOExcep
return StaticHostListLoader.loadHostList(data);
}

public static void deploy(SSHRemote remote, String dist, boolean restartNightly, FXConsole deployConsole, boolean logger_service)
public static void deploy(SSHRemote remote,
String dist,
boolean restartNightly,
FXConsole deployConsole,
boolean logger_service,
boolean deploy_with_lock_file)
{
SSHDeploy deploy = new SSHDeploy(remote, deployConsole);
URL deployScript = loader.getResource("deploy.sh");
URL loggerService = loader.getResource("ihmc-logger.service");
URL crontab = loader.getResource("ihmc-logger-cron");

deploy.addBinaryFile("DIST", dist, "/tmp/logger.tar", false);
deploy.addTextFile("LOGGER_SERVICE", "ihmc-logger.service", loggerService, "/etc/systemd/system/ihmc-logger.service", true);
deploy.addTextFile("CRON_ENTRY", "ihmc-logger-cron", crontab, "/tmp/ihmc-logger-cron", true);

deploy.addVariable("NIGHTLY_RESTART", restartNightly ? "true" : "false");
deploy.addVariable("DEPLOY_SERVICE", logger_service ? "true" : "false");
deploy.addVariable("DEPLOY_WITH_LOCK_FILE", deploy_with_lock_file ? "true" : "false");

deploy.deploy(deployScript);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,9 @@ public class LoggerDeployController implements Initializable

@FXML
CheckBox logger_service;

@FXML
CheckBox deploy_with_lock_file;


@Override
Expand All @@ -120,6 +123,8 @@ public void initialize(URL location, ResourceBundle resources)

prefs.linkToPrefs(logger_service, true);

prefs.linkToPrefs(deploy_with_lock_file, true);

camera_table.setEditable(true);

camera_id_col.setCellValueFactory(new PropertyValueFactory<CameraBean, Integer>("camera_id"));
Expand Down Expand Up @@ -359,7 +364,8 @@ void logger_deploy(ActionEvent e)
logger_dist.getText(),
logger_restart_midnight.isSelected(),
getStage(),
logger_service.isSelected());
logger_service.isSelected(),
deploy_with_lock_file.isSelected());
}

private Stage getStage()
Expand Down
33 changes: 14 additions & 19 deletions src/main/java/us/ihmc/publisher/logger/ui/LoggerDeployScript.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@
public interface LoggerDeployScript
{
/**
* Called when clicking "Deploy logger" in the application
*
* Called when clicking "Deploy logger" in the application
*
* @param logger_host
* @param logger_user
* @param logger_password
Expand All @@ -30,11 +30,12 @@ default void deploy(String logger_host,
String logger_dist,
boolean nightly_restart,
Stage stage,
boolean logger_service)
boolean logger_service,
boolean deploy_with_lock_file)
{
FXConsole deployConsole = new FXConsole(stage);
SSHRemote remote = new SSHRemote(logger_host, logger_user, logger_password, logger_sudo_password);
LoggerDeployConfiguration.deploy(remote, logger_dist, nightly_restart, deployConsole, logger_service);
LoggerDeployConfiguration.deploy(remote, logger_dist, nightly_restart, deployConsole, logger_service, deploy_with_lock_file);
}

default boolean implementsAutoRestart()
Expand All @@ -53,27 +54,21 @@ default void saveConfiguration(String logger_host,
{
FXConsole deployConsole = new FXConsole(stage);
SSHRemote remote = new SSHRemote(logger_host, logger_user, logger_password, logger_sudo_password);

LoggerDeployConfiguration.saveConfiguration(remote, settings, staticHostList, restartonSave, deployConsole);
}

default CameraSettings loadCameraConfiguration(String logger_host,
String logger_user,
String logger_password,
String logger_sudo_password,
Stage stage) throws IOException

default CameraSettings loadCameraConfiguration(String logger_host, String logger_user, String logger_password, String logger_sudo_password, Stage stage)
throws IOException
{
SSHRemote remote = new SSHRemote(logger_host, logger_user, logger_password, logger_sudo_password);
SSHRemote remote = new SSHRemote(logger_host, logger_user, logger_password, logger_sudo_password);
return LoggerDeployConfiguration.loadCameraConfiguration(remote);
}

default StaticHostList loadStaticHostList(String logger_host,
String logger_user,
String logger_password,
String logger_sudo_password,
Stage stage) throws IOException

default StaticHostList loadStaticHostList(String logger_host, String logger_user, String logger_password, String logger_sudo_password, Stage stage)
throws IOException
{
SSHRemote remote = new SSHRemote(logger_host, logger_user, logger_password, logger_sudo_password);
SSHRemote remote = new SSHRemote(logger_host, logger_user, logger_password, logger_sudo_password);
return LoggerDeployConfiguration.loadStaticHostList(remote);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -36,31 +36,40 @@ public class YoVariableLoggerDispatcher implements DataServerDiscoveryListener
private final YoVariableLoggerOptions options;

/**
* Create a new YovariableLoggerDispatcher. For every log that comes online, a YoVariableLogger is
* Create a new YoVariableLoggerDispatcher. For every log that comes online, a YoVariableLogger is
* created.
*
* @param options
* @throws IOException
*/
public YoVariableLoggerDispatcher(YoVariableLoggerOptions options) throws IOException
{
if (lockFile.exists())
if (options.getDeployWithLockFile())
{
LogTools.info("Maybe if you weren't so full of yourself you would have checked if the logger was already running");
LogTools.info("Check the file: " + lockFile.getAbsolutePath() + " or run (ps aux | grep java)");
System.exit(0);
}
if (lockFile.exists())
{

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Would be helpful to put "Lockfile found at (the path to the lockfile), This is likely from an existing logger running. Maybe if you weren't so full of yourself you would have checked if the logger was already running. If the logger is not running, it is safe to remove the file and restart the logger.

LogTools.info("Maybe if you weren't so full of yourself you would have checked if the logger was already running");
LogTools.info("Check the file: " + lockFile.getAbsolutePath() + " or run (ps aux | grep java)");
System.exit(0);
}

lockFile.createNewFile();
Set<PosixFilePermission> perms = new HashSet<>();
if (!SystemUtils.OS_NAME.contains("Windows"))
{
perms.add(PosixFilePermission.OWNER_READ);
Files.setPosixFilePermissions(lockFile.toPath(), perms);
}

LogTools.info("Created Logger lock file");

lockFile.createNewFile();
Set<PosixFilePermission> perms = new HashSet<>();
if (!SystemUtils.OS_NAME.contains("Windows"))
Runtime.getRuntime().addShutdownHook(new Thread(this::shutDownLockFile, "ShutdownThread"));
}
else
{
perms.add(PosixFilePermission.OWNER_READ);
Files.setPosixFilePermissions(lockFile.toPath(), perms);

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Whoa whoa whoa, logger is starting without Lock File, be careful to make sure you only run one logger at a time. When redeploying as a service you should stop the service before deployment. Use ps aux | grep java to confirm.

LogTools.info("Whoa whoa whoa, logger is starting without Lock File, be careful to make sure you only run one logger at a time");
}

LogTools.info("Created Logger lock file");

this.options = options;
LogTools.info("Starting YoVariableLoggerDispatcher");

Expand All @@ -70,16 +79,21 @@ public YoVariableLoggerDispatcher(YoVariableLoggerOptions options) throws IOExce

LogTools.info("Client started, waiting for data server sessions");

Runtime.getRuntime().addShutdownHook(new Thread(this::shutDownLockFile, "ShutdownThread"));

ThreadTools.sleepForever();
}

private void shutDownLockFile()
{
lockFile.delete();
boolean shutdownProperly = lockFile.delete();

LogTools.info("Interrupted by Ctrl+C, deleting lock file");
if (!shutdownProperly)
{
LogTools.error("Could not delete lock file: " + lockFile.getAbsolutePath());
}
else
{
LogTools.info("Interrupted by Ctrl+C, deleted lock file successfully");
}
}

public static void main(String[] args) throws JSAPException, IOException, InterruptedException
Expand All @@ -106,7 +120,7 @@ public void connected(HTTPDataServerConnection connection)
{
try
{
new YoVariableLogger(connection, options, (request) -> finishedLog(request));
new YoVariableLogger(connection, options, this::finishedLog);
activeLogSessions.add(hashAnnouncement);
LogTools.info("Logging session started for " + announcement.getNameAsString());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@

public class YoVariableLoggerOptions
{
private static boolean deployWithLockFile = true;
public final static String defaultLogDirectory = System.getProperty("user.home") + "/robotLogs";

public final static CodecID defaultCodec = CodecID.AV_CODEC_ID_MJPEG;
Expand Down Expand Up @@ -39,46 +40,53 @@ public static YoVariableLoggerOptions parse(String[] args) throws JSAPException
SimpleJSAP jsap = new SimpleJSAP("YoVariabeLogger",
"Logs YoVariables and video from a robot",
new Parameter[] {new Switch("disableVideo", 'n', "noVideo", "Disable video recording"),
new FlaggedOption("logDirectory",
JSAP.STRING_PARSER,
YoVariableLoggerOptions.defaultLogDirectory,
JSAP.NOT_REQUIRED,
'd',
"directory",
"Directory where to save log files"),
new FlaggedOption("videoQuality",
JSAP.DOUBLE_PARSER,
String.valueOf(YoVariableLoggerOptions.defaultVideoQuality),
JSAP.NOT_REQUIRED,
'q',
"quality",
"Video quality for MJPEG"),
new FlaggedOption("videoCodec",
JSAP.STRING_PARSER,
String.valueOf(defaultCodec),
JSAP.NOT_REQUIRED,
'c',
"codec",
"Desired video codec. AV_CODEC_ID_H264 or AV_CODEC_ID_MJPEG"),
new FlaggedOption("crf",
JSAP.INTEGER_PARSER,
String.valueOf(defaultCRF),
JSAP.NOT_REQUIRED,
'r',
"crf",
"CRF (Constant rate factor) for H264. 0-51, 0 is lossless. Sane values are 18 to 28."),
new FlaggedOption("rotate",
JSAP.INTEGER_PARSER,
"0",
JSAP.NOT_REQUIRED,
'o',
"rotate",
"Rotate logs in incoming folder, keep n logs. Set to zero to keep all logs."),
new Switch("flushAggressivelyToDisk",
's',
"sync",
"Aggressively flush data to disk. Reduces change of data loss but doesn't work on slow platters."),
new Switch("disableAutoDiscovery", 'a', "noDiscovery", "Disable autodiscovery of clients.")});
new FlaggedOption("deployWithoutLockFile",
JSAP.BOOLEAN_PARSER,
String.valueOf(YoVariableLoggerOptions.deployWithLockFile),
JSAP.NOT_REQUIRED,
'l',
"lockFile",
"Deploy with lock file"),
new FlaggedOption("logDirectory",
JSAP.STRING_PARSER,
YoVariableLoggerOptions.defaultLogDirectory,
JSAP.NOT_REQUIRED,
'd',
"directory",
"Directory where to save log files"),
new FlaggedOption("videoQuality",
JSAP.DOUBLE_PARSER,
String.valueOf(YoVariableLoggerOptions.defaultVideoQuality),
JSAP.NOT_REQUIRED,
'q',
"quality",
"Video quality for MJPEG"),
new FlaggedOption("videoCodec",
JSAP.STRING_PARSER,
String.valueOf(defaultCodec),
JSAP.NOT_REQUIRED,
'c',
"codec",
"Desired video codec. AV_CODEC_ID_H264 or AV_CODEC_ID_MJPEG"),
new FlaggedOption("crf",
JSAP.INTEGER_PARSER,
String.valueOf(defaultCRF),
JSAP.NOT_REQUIRED,
'r',
"crf",
"CRF (Constant rate factor) for H264. 0-51, 0 is lossless. Sane values are 18 to 28."),
new FlaggedOption("rotate",
JSAP.INTEGER_PARSER,
"0",
JSAP.NOT_REQUIRED,
'o',
"rotate",
"Rotate logs in incoming folder, keep n logs. Set to zero to keep all logs."),
new Switch("flushAggressivelyToDisk",
's',
"sync",
"Aggressively flush data to disk. Reduces change of data loss but doesn't work on slow platters."),
new Switch("disableAutoDiscovery", 'a', "noDiscovery", "Disable autodiscovery of clients.")});
JSAPResult config = jsap.parse(args);
if (jsap.messagePrinted())
{
Expand All @@ -88,6 +96,7 @@ public static YoVariableLoggerOptions parse(String[] args) throws JSAPException
}

YoVariableLoggerOptions options = new YoVariableLoggerOptions();
options.setDeployWithLockFile(config.getBoolean("deployWithoutLockFile"));
options.setLogDirectory(config.getString("logDirectory"));
options.setVideoQuality(config.getDouble("videoQuality"));
options.setDisableVideo(config.getBoolean("disableVideo"));
Expand Down Expand Up @@ -195,4 +204,13 @@ public void setDisableAutoDiscovery(boolean disableAutoDiscovery)
this.disableAutoDiscovery = disableAutoDiscovery;
}

public void setDeployWithLockFile(boolean deployWithoutLockFile)
{
YoVariableLoggerOptions.deployWithLockFile = deployWithoutLockFile;
}

public boolean getDeployWithLockFile()
{
return deployWithLockFile;
}
}
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
# /etc/cron.d/ihmc-logger-cron Restarts the ihmc-logger service nightly

0 3 * * * root /bin/systemctl restart ihmc-logger
0 3 * * * root /bin/systemctl restart ihmc-logger -l ${DEPLOY_WITH_LOCK_FILE}
Loading
Loading