Skip to content

Commit

Permalink
Add custom frc kill script to work around jvm bug (#699)
Browse files Browse the repository at this point in the history
  • Loading branch information
ThadHouse authored Dec 12, 2023
1 parent 84bffed commit 54c15c3
Show file tree
Hide file tree
Showing 6 changed files with 96 additions and 10 deletions.
1 change: 0 additions & 1 deletion src/main/java/edu/wpi/first/gradlerio/GradleRIOPlugin.java
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,6 @@
import edu.wpi.first.gradlerio.deploy.FRCDeployPlugin;
import edu.wpi.first.gradlerio.deploy.roborio.RoboRIO;
import edu.wpi.first.gradlerio.wpi.WPIPlugin;
import edu.wpi.first.gradlerio.OneDriveException;

public abstract class GradleRIOPlugin implements Plugin<Project> {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ public StagedDeployTarget(String name, Project project, DeployExtension de) {

private void configureArtifact(Artifact artifact) {
Callable<TaskProvider<StageDeployTask>> cbl = () -> computeArtifactStageDep(artifact);
artifact.dependsOn(cbl);
artifact.dependsOnForDeployTask(cbl);
}

private final DeployStage defaultStage = DeployStage.FileDeploy;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
package edu.wpi.first.gradlerio.deploy.roborio;

import java.io.IOException;
import java.io.InputStream;

import javax.inject.Inject;

import edu.wpi.first.deployutils.deploy.artifact.AbstractArtifact;
import edu.wpi.first.deployutils.deploy.context.DeployContext;
import edu.wpi.first.gradlerio.deploy.DeployStage;
import edu.wpi.first.gradlerio.deploy.StagedDeployTarget;
import edu.wpi.first.gradlerio.wpi.dependencies.tools.ToolInstallTask;

public class FRCProgramKillArtifact extends AbstractArtifact {

@Inject
public FRCProgramKillArtifact(String name, StagedDeployTarget target) {
super(name, target);

target.setDeployStage(this, DeployStage.ProgramKill);


}

@Override
public void deploy(DeployContext ctx) {
ctx.getLogger().log(" Deploying new frcKillRobot script");
try (InputStream it = ToolInstallTask.class.getResourceAsStream("/frcKillRobot.sh")) {
ctx.put(it, "/usr/local/frc/bin/frcKillRobot.sh.tmp");
} catch (IOException e) {
throw new RuntimeException(e);
}
ctx.execute("mv /usr/local/frc/bin/frcKillRobot.sh.tmp /usr/local/frc/bin/frcKillRobot.sh && chmod +x /usr/local/frc/bin/frcKillRobot.sh");
ctx.execute("sync");
ctx.execute(". /etc/profile.d/natinst-path.sh; /usr/local/frc/bin/frcKillRobot.sh -t -r 2> /dev/null");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,8 @@
import org.gradle.api.logging.Logging;

import edu.wpi.first.deployutils.deploy.DeployExtension;
import edu.wpi.first.deployutils.deploy.artifact.MultiCommandArtifact;
import edu.wpi.first.deployutils.deploy.context.DeployContext;
import edu.wpi.first.deployutils.deploy.target.location.SshDeployLocation;
import edu.wpi.first.gradlerio.deploy.DeployStage;
import edu.wpi.first.gradlerio.deploy.FRCExtension;
import edu.wpi.first.gradlerio.deploy.WPIRemoteTarget;
import edu.wpi.first.gradlerio.wpi.WPIExtension;
Expand All @@ -28,7 +26,7 @@ public class RoboRIO extends WPIRemoteTarget {
private boolean checkImage = true;
private final List<String> validImageVersions;

private final MultiCommandArtifact programKillArtifact;
private final FRCProgramKillArtifact programKillArtifact;

@Inject
public RoboRIO(String name, Project project, DeployExtension de, FRCExtension frcExtension) {
Expand All @@ -44,16 +42,14 @@ public RoboRIO(String name, Project project, DeployExtension de, FRCExtension fr
// Make a copy of valid image versions so user defined cannot modify the global array
validImageVersions = new ArrayList<>(WPIExtension.getValidImageVersions());

programKillArtifact = project.getObjects().newInstance(MultiCommandArtifact.class, "programKill" + name, this);
setDeployStage(programKillArtifact, DeployStage.ProgramKill);
programKillArtifact.addCommand("kill", ". /etc/profile.d/natinst-path.sh; /usr/local/frc/bin/frcKillRobot.sh -t 2> /dev/null");
programKillArtifact = project.getObjects().newInstance(FRCProgramKillArtifact.class, "programKill" + name, this);

getTargetPlatform().set(NativePlatforms.roborio);

getArtifacts().add(programKillArtifact);
}

public MultiCommandArtifact getProgramKillArtifact() {
public FRCProgramKillArtifact getProgramKillArtifact() {
return programKillArtifact;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ public void execute(Task arg0) {
// From
// https://github.com/wpilibsuite/native-utils/blob/a8ea595670716c7b898878a37e36c2b52e8e3f42/src/main/groovy/edu/wpi/first/nativeutils/rules/BuildConfigRules.groovy#L450
private static void printWarningsForBinTask(String taskName, Project project) {
File file = new File(project.getBuildDir(), "tmp/" + taskName + "/output.txt");
File file = project.getLayout().getBuildDirectory().get().file("/tmp/" + taskName + "/output.txt").getAsFile();

if (!file.exists())
return;
Expand Down
54 changes: 54 additions & 0 deletions src/main/resources/frcKillRobot.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
FRC_PID_FILE=/var/run/natinst/FRC_UserProgram.pid

usage()
{
cat >&2 <<EOF
Usage: ${0##*/} [-r] [-t]
-r Restart the lvrt process
-t Configure lvrt to start a text-based program
EOF
exit 1
}

while getopts "rt" Option
do
case $Option in
r) RESTART=yes;;
t) TEXT_BASED_PROGRAM=yes;;
*) usage;;
esac
done


if [ "$TEXT_BASED_PROGRAM" = yes ]; then
/usr/local/frc/bin/frcEnableTBLStartupApp.sh
if [ "$RESTART" = yes ]; then
nirtcfg --file /etc/natinst/share/lvrt.conf --set section=LVRT,token=RTTarget.LaunchAppAtBoot,value=True
else
nirtcfg --file /etc/natinst/share/lvrt.conf --set section=LVRT,token=RTTarget.LaunchAppAtBoot,value=False
fi
fi

printf "%s - Killing robot code in frcKillRobot.sh\n" "`date`" | FRC_ConsoleTee

if [ -e $FRC_PID_FILE ]; then
DAEMON_PID=`cat $FRC_PID_FILE`
if [ -e /proc/$DAEMON_PID ]; then
PGROUP=`ps -o pid,pgid,comm | grep $DAEMON_PID | awk -v N=2 '{print $N}'`
kill -term -- -$PGROUP
sleep 1
kill -9 -- -$PGROUP
fi
rm -f $FRC_PID_FILE
fi

if [ "$RESTART" = yes ]; then
RT_STARTUP_DISABLED=`nirtcfg --get section=SYSTEMSETTINGS,token=NoApp.enabled,value=false | tr "[:upper:]" "[:lower:]"`
if [ "$RT_STARTUP_DISABLED" = false ]; then
killall lvrt
else
/usr/local/frc/bin/frcRunRobot.sh &
fi
fi

0 comments on commit 54c15c3

Please sign in to comment.