check_task_count.py 850 B

12345678910111213141516171819202122232425262728293031
  1. """
  2. Task to check the number of running and pending tasks
  3. """
  4. import logging
  5. import boto3
  6. from decorators import with_logging
  7. logger = logging.getLogger()
  8. client = boto3.client("ecs")
  9. @with_logging
  10. def handler(event, context):
  11. try:
  12. service = client.describe_services(
  13. cluster=event["Cluster"],
  14. services=[
  15. event["ServiceName"],
  16. ],
  17. )["services"][0]
  18. pending = service["pendingCount"]
  19. running = service["runningCount"]
  20. return {"Pending": pending, "Running": running, "Total": pending + running}
  21. except IndexError:
  22. logger.error("Unable to find service '%s'", event["ServiceName"])
  23. raise ValueError(
  24. "Service {} in cluster {} not found".format(
  25. event["ServiceName"], event["Cluster"]
  26. )
  27. )