简体   繁体   中英

Trigger a PHP script to run on each Fargate task in an ECS service

What is the best way to trigger a PHP script on all running Fargate tasks for an ECS service? I need to trigger this from another PHP script on one of the ECS tasks. The reason I need to do this is that I have NGINX FastCGI Cache on these individual ECS tasks and need to purge the cache for all tasks when an admin makes an update in the CMS.

My NGINX configuration has a /purge/[path] endpoint that will purge the cache using fastcgi_cache_purge, and I do currently have a somewhat working hacky solution that looks something like this:

  1. Admin saves in the CMS
  2. PHP snippet runs that:
    a) Fetches the number of running ECS tasks using AWS PHP SDK
    b) Runs a for loop for the number of ECS tasks running
    c) Calls the /purge/[path] URL using cURL (tries multiple times as it sometimes hits the same ECS task)

The above works but is not optimal.

Here are some other solutions that come to mind, but I can't find much information online on how to implement:

  • Would it be possible perhaps to change the fastcgi_cache_path to a shared file system like AWS EFS, or does that hurt performance not being tmpfs? I see that there's tmpfs support for ECS, but once mounted, would it be shared across the multiple ECS tasks or individually per ECS task?
  • Using AWS SNS/SQS (write one PHP script that subscribes and another one that publishes an event)
  • Use Redis pub/sub similar to above (also not sure exactly how to implement and how to start a long-running subscriber when ECS tasks start)
  • Use ECS Exec and AWS PHP SDK (almost have a working solution that fetches all ECS tasks and loops them through, and executes an "ECS Exec" command, but "--non-interactive" is not available yet, so it doesn't work. Only "--interactive" mode works currently)

Is there an easier/better solution for this? If using any of the above implementations, can someone put me in the right direction on how to implement this using PHP?

Thanks!

This honestly seems like a duplicate of your previous question, but I'll answer:

  • Would it be possible perhaps to change the fastcgi_cache_path to a shared file system like AWS EFS, or does that hurt performance not being tmpfs? I see that there's tmpfs support for ECS, but once mounted, would it be shared across the multiple ECS tasks or individually per ECS task?

EFS wouldn't be a good option for this because EFS is SLOW . It would kill any performance benefit of having a cache.

A tmpfs on Fargate will not be shared among instances.

  • Using AWS SNS/SQS (write one PHP script that subscribes and another one that publishes an event)

SNS could work, as discussed in your previous question. SQS would not work at all, since an SQS message is only delivered to one consumer, and you want it to be delivered to all instances.

  • Use Redis pub/sub similar to above (also not sure exactly how to implement and how to start a long-running subscriber when ECS tasks start)

Yes this would work, as discussed in your previous question. But of course would require a lot of custom coding.

  • Use ECS Exec and AWS PHP SDK (almost have a working solution that fetches all ECS tasks and loops them through, and executes an "ECS Exec" command, but "--non-interactive" is not available yet, so it doesn't work. Only "--interactive" mode works currently)

This should work. Why do you state that --non-interactive is not available yet? If you just leave off the --interactive parameter, then the command is executed non-interactively.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM