#!/bin/bash
MODE=$1
if [[ -z $2 ]];then HEADTAIL_LIMIT=0; else HEADTAIL_LIMIT=$2;fi
cd "$( cd "$( dirname "${BASH_SOURCE[0]}" )" >/dev/null 2>&1 && pwd )/../.."
source scripts/funcs

echo "## Scheduling a task on Fargate

In this section, we will learn how to schedule a task so it runs automatically.
"

CheckAws

if [[ -z "$AWS_PROFILE" ]]
then
ChooseAwsProfile
fi

echo "
To schedule a task, use schedule command with parameters target_id and
cron in [CRON format](https://en.wikipedia.org/wiki/Cron).

At the bottom of project.yml of 04_install project, there is a schedule section
which defines both:
"

Prompt

Cat 04_install/project.yml

Prompt

echo "
Also note that __VARS is defined in the section instead of giving at the command line.

With the schedule section presnt, you can simply schedule the task by running:
"

Code "
handoff cloud schedule create -p 04_install
"

echo '
Alternatively, we can pass those values to handoff with `--vars` (`-v` for short) option:
'

Prompt "The following command is set to trigger the task 5 minutes later from now:"

hour=`date -u +"%H"`
min=`date -u +"%M"`
min=$(($min + 5))

Command "handoff cloud schedule create -p 04_install -v target_id=1 cron='$min $hour * * ? *' --envs __VARS='start_date=\$(date -I -d \"-7 day\")'"

echo '

At the end of the log, you should see a line like:

```shell

    Check the progress at Check the status at https://console.aws.amazon.com/ecs/home?region=us-east-1#/clusters/...

```
'

echo "
Grab the entire link and open in a browser (you need to login in to AWS) to see
it's scheduled.
"


Prompt

Thanks

echo "
We confirmed that the task run in the same way as local execution.
Now let's go to the next module to learn how to schedule the task
so it runs periodically.
"

Continue scripts/aws_get_started/09_cleanup
