diff --git a/opt/cs50/bin/rstudio b/opt/cs50/bin/rstudio index c4ca3d9b..84b4c3d4 100755 --- a/opt/cs50/bin/rstudio +++ b/opt/cs50/bin/rstudio @@ -65,11 +65,19 @@ jq ".initial_working_directory = \"/workspaces/$RepositoryName\"" /opt/cs50/lib/ # Copy files into container docker cp --quiet /opt/cs50/lib/rstudio/_icons.css rstudio:/tmp docker cp --quiet /opt/cs50/lib/rstudio/rstudio-server.json rstudio:/etc/rstudio/ +docker cp --quiet /opt/cs50/lib/rstudio/rsession.conf rstudio:/etc/rstudio/ docker cp --quiet /tmp/rstudio-prefs.json rstudio:/etc/rstudio/ +# Copy monitoring script into container +sudo chmod +x /opt/cs50/lib/rstudio/monitor_rsession.sh +docker cp --quiet /opt/cs50/lib/rstudio/monitor_rsession.sh rstudio:/usr/local/bin/monitor_rsession.sh + # Start container docker start "$NAME" > /dev/null +# Run the monitoring script in the background +docker exec -d "$NAME" sh -c "/usr/local/bin/monitor_rsession.sh" + # Inject CSS docker exec "$NAME" sh -c "cat /tmp/_icons.css >> /usr/lib/rstudio-server/www/css/icons.css" diff --git a/opt/cs50/lib/rstudio/monitor_rsession.sh b/opt/cs50/lib/rstudio/monitor_rsession.sh new file mode 100644 index 00000000..62c8ad30 --- /dev/null +++ b/opt/cs50/lib/rstudio/monitor_rsession.sh @@ -0,0 +1,24 @@ +#!/bin/bash + +cleanup() { + kill -TERM 1 +} + +# Wait for the rsession process to start +while ! pgrep -x "rsession" > /dev/null; do + sleep 1 +done + +# Monitor the rsession process +while true; do + if pgrep -x "rsession" > /dev/null; then + sleep 1 + else + # If the rsession process is not running for more than 10 seconds, terminate the container + # Restarting R session shouldn't take more than 10 seconds + sleep 10 + if ! pgrep -x "rsession" > /dev/null; then + cleanup + fi + fi +done diff --git a/opt/cs50/lib/rstudio/rsession.conf b/opt/cs50/lib/rstudio/rsession.conf new file mode 100644 index 00000000..d84c5729 --- /dev/null +++ b/opt/cs50/lib/rstudio/rsession.conf @@ -0,0 +1 @@ +session-timeout-minutes=30 \ No newline at end of file