start.sh 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  1. #!/bin/sh
  2. start_item() {
  3. toy=$1
  4. mkdir -p "toys/$toy/"
  5. while ! mkdir "toys/$toy/.lock"
  6. do
  7. sleep 1
  8. pid=$(cat "toys/$toy/.lock/pid")
  9. if [ -n "$pid" ] && ! kill -0 "$pid"
  10. then
  11. rm -r "toys/$toy/.lock"
  12. fi
  13. done
  14. echo $$ >"toys/$toy/.lock/pid"
  15. set -e
  16. lastItem=$(find "toys/$toy/" -type d -maxdepth 1 -mindepth 1 | grep -E '[0-9]+' | sed "s|toys/$toy/||" | sort -n | tail -n 1)
  17. lastItem=${lastItem:--1}
  18. currentItem=$(( lastItem + 1 ))
  19. mkdir "toys/$toy/$currentItem"
  20. while [ "$(find "toys/$toy/" -type d -maxdepth 1 -mindepth 1 | grep -Ec '[0-9]+')" -gt 6 ]
  21. do
  22. earliestItem=$(find "toys/$toy/" -type d -maxdepth 1 -mindepth 1 | grep -E '[0-9]+' | sed "s|toys/$toy/||" | sort -n | head -n 1)
  23. rm -rf "toys/$toy/$earliestItem" "toys/$toy/$earliestItem.log" "toys/$toy/$earliestItem.exit"
  24. done
  25. rm -r "toys/$toy/.lock"
  26. touch "toys/$toy/$currentItem.log"
  27. # shellcheck disable=SC2154
  28. podman run -d --timeout 86400 --log-opt=path="$docroot/toys/$toy/$currentItem.log" --name "item_${toy}_$currentItem" "toy_$toy" "$currentItem" >/dev/null 2>&1
  29. # podman run … --dns=202.61.197.122 --dns=80.152.203.134 …
  30. printf "%s" "$currentItem"
  31. }
  32. stop_item() {
  33. toy=$1
  34. item=$2
  35. if ! podman ps | grep -q "item_${toy}_$item"
  36. then
  37. return
  38. fi
  39. podman stop "item_${toy}_$item"
  40. }