quintilesims / layer0

Build, Manage, and Deploy Your Applications
Apache License 2.0
44 stars 20 forks source link

Create scheduled tasks #282

Open zpatrick opened 7 years ago

zpatrick commented 7 years ago

http://docs.aws.amazon.com/AmazonECS/latest/developerguide/scheduled_tasks.html

sesh-kebab commented 7 years ago

I've done some initial investigation and prototyped a potential solution to determine the feasibility of supporting Scheduled Tasks in Layer0.

CLI - changes l0 task create <taskname> <environment_id> <deploy_id> --schedule <cron/rate expression>

The above introduces a new parameter --schedule, to the existing cli command for creating a task. This allows the user to specify a CRON or Rate expression which will then:

  1. Create a new Job to create the task
  2. This task will be picked up by the Layer0 Runner and will have access to the additional schedule value.

API - changes When the Layer0 Runner picks up the job to create a task, if the schedule parameter value is not empty, the create task code in task manager will also create:

  1. Cloudwatch Events Rule (schedule a reoccurring event)
  2. Cloudwatch Event Target to Run a TaskDefinition (executes the actual task on a schedule event)
  3. (Note: the IAM Role we use for Layer0 needs an additional policy ecs:RunTask

The above changes are enough to create a scheduled task via Layer0. But thinking about how we could manage the whole lifecycle of a scheduled task, a few problems arise:

ScheduledTask Entity (New) Introducing a new flag schedule to a task create command; while it provides a easy pathway from a Task to a Scheduled Task, it poses problems when thinking about how to manage the lifecycle of a Scheduled Task.

Task != Scheduled Task. A Task is a one-off short running job is somewhat fire and forget. A Schedule Task on the other hand augments a Task but also can exist indefinitely, with the properties: state (active/inactive) & schedule (rate(1 day)). So representing a Scheduled Task as a special type of Task is somewhat misleading at best.

An alternative approach would be to create a new entity to represent a ScheduledTask. This can be supported by both L0 CLI & our Layer0 Terraform Provider.

Tags There are some issues here that I don't completely understand but did arise during discussing this with @zpatrick

Scaler Layer0 Scaler doesn't have concept of a Scheduled Task. Although as a task gets scheduled (PENDING state), my assumption is that the scaler can work the same way when it scales for a normal task. But there was also some other discussion here which indicated otherwise especially if we try to pre-emptively provision resources for scheduled tasks based on a schedule.

AWS API Scheduled tasks aren't first class citizens of ECS. Although it may seem like it from the AWS Console, the only new addition is that a Cloudwatch Event now allows an ECS TaskDefinition inside an ECS Cluster as a target. So querying for scheduled events from AWS ECS endpoint isn't currently supported. We would have to query Cloudwatch Events to match a specific pattern (based on cluster/task name) to find scheduled tasks for a given cluster. There maybe a hidden api call that AWS Console is using to display Scheduled Tasks in ECS Cluster page, but I haven't seen any mention of it in AWS documentation.