start.sh 1.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  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. printf "%s" "$currentItem"
  30. }
  31. stop_item() {
  32. toy=$1
  33. item=$2
  34. if ! podman ps | grep -q "item_${toy}_$item"
  35. then
  36. return
  37. fi
  38. podman stop "item_${toy}_$item"
  39. }