<commit_before>package main

import (
	"regexp"

	"github.com/aws/aws-sdk-go/aws"
	"github.com/aws/aws-sdk-go/aws/session"
	"github.com/aws/aws-sdk-go/service/ecs"
	"fmt"
	"os"
	"time"
)

func ExtractName(a *string) string {
	re := regexp.MustCompile("\\w+:\\w+:\\w+:[\\w-]+:\\d+:\\w+\\/(.+)")

	res := re.FindAllStringSubmatch(*a, -1);

	if res == nil {
		return "No match for " + *a;
	}

	return res[0][1]
}

func _listServices(cluster string) *ecs.ListServicesOutput {
	svc := ecs.New(session.New(), _getAwsConfig())

	params := &ecs.ListServicesInput{
		Cluster: aws.String(cluster),
		MaxResults: aws.Int64(10),
	}

	resp, err := svc.ListServices(params)
	if err != nil {
		// Print the error, cast err to awserr.Error to get the Code and
		// Message from an error.
		fmt.Println(err.Error())
		os.Exit(1);
	}

	return resp;
}

func _listTasks(cluster, service string) *ecs.ListTasksOutput {
	svc := ecs.New(session.New(), _getAwsConfig())

	params := &ecs.ListTasksInput{
		Cluster: aws.String(cluster),
		ServiceName: aws.String(service),
	}

	resp, err := svc.ListTasks(params)
	if (err != nil) {
		fmt.Println(err.Error())
		os.Exit(1)
	}

	return resp
}

func _describeService(clusterArn, serviceArn string) (*ecs.DescribeServicesOutput) {
	svc := ecs.New(session.New(), _getAwsConfig())

	params := &ecs.DescribeServicesInput{
		Cluster: aws.String(clusterArn),
		Services: []*string{aws.String(serviceArn)},
	}

	result, err := svc.DescribeServices(params)
	if (err != nil) {
		errState(err.Error())
	}

	return result;
}

func _describeTasks(clusterArn string, tasks []*string) *ecs.DescribeTasksOutput {
	svc := ecs.New(session.New(), _getAwsConfig())

	params := &ecs.DescribeTasksInput{
		Cluster: aws.String(clusterArn),
		Tasks: tasks,
	}

	result, err := svc.DescribeTasks(params)

	if (err != nil) {
		errState(err.Error())
	}

	return result
}

func ListServices(clusterArn string) {

	resp := _listServices(clusterArn)
	for i := 0; i < len(resp.ServiceArns); i++ {
		name := ExtractName(resp.ServiceArns[i])
		fmt.Println(name)
	}
}

func ListTasks(clusterArn, serviceArn string) {
	resp := _listTasks(clusterArn, serviceArn)
	for i := 0; i < len(resp.TaskArns); i++ {
		name := ExtractName(resp.TaskArns[i])
		fmt.Println(name)
	}
}

func UpdateService(clusterArn string, serviceArn string) {
	tasks := _listTasks(clusterArn, serviceArn)
	service := _describeService(clusterArn, serviceArn)

	desiredCount := *service.Services[0].DesiredCount

	if len(tasks.TaskArns) < int(desiredCount) {
		errStatef("The number of actual tasks (%d) differs from desired tasks (%d)\n", len(tasks.TaskArns), desiredCount)
	}

	for i := 0; i < len(tasks.TaskArns); i++ {
		currentTasks := _listTasks(clusterArn, serviceArn)
		fmt.Println("Task: " + *tasks.TaskArns[i])
		_stopTask(clusterArn, *tasks.TaskArns[i])
		fmt.Println("  Stopped")
		_waitForNewTask(clusterArn, serviceArn, currentTasks.TaskArns)
	}

	fmt.Printf("Service [%s] is updated\n", *service.Services[0].ServiceName)
}

func _stopTask(cluster, taskArn string) {
	svc := ecs.New(session.New(), _getAwsConfig())

	params := &ecs.StopTaskInput{
		Cluster: aws.String(cluster),
		Reason: aws.String("Stopped by WriterTool"),
		Task: aws.String(taskArn),
	}

	_, err := svc.StopTask(params)

	if (err != nil) {
		errState(err.Error())
	}
}

func _waitForNewTask(cluster string, service string, tasks []*string) {
	fmt.Print("  Waiting for new task ")
	newTask := ""

	attempts := 60
	sleepTime := 2

	for i := 0; i < attempts; i++ {
		currentTasks := _listTasks(cluster, service)
		if (len(currentTasks.TaskArns) < len(tasks)) {
			fmt.Print(".")
		} else if (newTask == ""){<commit_msg>Adding check for if there are no tasks, while waiting for new task<commit_after>package main

import (
	"regexp"

	"github.com/aws/aws-sdk-go/aws"
	"github.com/aws/aws-sdk-go/aws/session"
	"github.com/aws/aws-sdk-go/service/ecs"
	"fmt"
	"os"
	"time"
)

func ExtractName(a *string) string {
	re := regexp.MustCompile("\\w+:\\w+:\\w+:[\\w-]+:\\d+:\\w+\\/(.+)")

	res := re.FindAllStringSubmatch(*a, -1);

	if res == nil {
		return "No match for " + *a;
	}

	return res[0][1]
}

func _listServices(cluster string) *ecs.ListServicesOutput {
	svc := ecs.New(session.New(), _getAwsConfig())

	params := &ecs.ListServicesInput{
		Cluster: aws.String(cluster),
		MaxResults: aws.Int64(10),
	}

	resp, err := svc.ListServices(params)
	if err != nil {
		// Print the error, cast err to awserr.Error to get the Code and
		// Message from an error.
		fmt.Println(err.Error())
		os.Exit(1);
	}

	return resp;
}

func _listTasks(cluster, service string) *ecs.ListTasksOutput {
	svc := ecs.New(session.New(), _getAwsConfig())

	params := &ecs.ListTasksInput{
		Cluster: aws.String(cluster),
		ServiceName: aws.String(service),
	}

	resp, err := svc.ListTasks(params)
	if (err != nil) {
		fmt.Println(err.Error())
		os.Exit(1)
	}

	return resp
}

func _describeService(clusterArn, serviceArn string) (*ecs.DescribeServicesOutput) {
	svc := ecs.New(session.New(), _getAwsConfig())

	params := &ecs.DescribeServicesInput{
		Cluster: aws.String(clusterArn),
		Services: []*string{aws.String(serviceArn)},
	}

	result, err := svc.DescribeServices(params)
	if (err != nil) {
		errState(err.Error())
	}

	return result;
}

func _describeTasks(clusterArn string, tasks []*string) *ecs.DescribeTasksOutput {
	svc := ecs.New(session.New(), _getAwsConfig())

	params := &ecs.DescribeTasksInput{
		Cluster: aws.String(clusterArn),
		Tasks: tasks,
	}

	result, err := svc.DescribeTasks(params)

	if (err != nil) {
		errState(err.Error())
	}

	return result
}

func ListServices(clusterArn string) {

	resp := _listServices(clusterArn)
	for i := 0; i < len(resp.ServiceArns); i++ {
		name := ExtractName(resp.ServiceArns[i])
		fmt.Println(name)
	}
}

func ListTasks(clusterArn, serviceArn string) {
	resp := _listTasks(clusterArn, serviceArn)
	for i := 0; i < len(resp.TaskArns); i++ {
		name := ExtractName(resp.TaskArns[i])
		fmt.Println(name)
	}
}

func UpdateService(clusterArn string, serviceArn string) {
	tasks := _listTasks(clusterArn, serviceArn)
	service := _describeService(clusterArn, serviceArn)

	desiredCount := *service.Services[0].DesiredCount

	if len(tasks.TaskArns) < int(desiredCount) {
		errStatef("The number of actual tasks (%d) differs from desired tasks (%d)\n", len(tasks.TaskArns), desiredCount)
	}

	for i := 0; i < len(tasks.TaskArns); i++ {
		currentTasks := _listTasks(clusterArn, serviceArn)
		fmt.Println("Task: " + *tasks.TaskArns[i])
		_stopTask(clusterArn, *tasks.TaskArns[i])
		fmt.Println("  Stopped")
		_waitForNewTask(clusterArn, serviceArn, currentTasks.TaskArns)
	}

	fmt.Printf("Service [%s] is updated\n", *service.Services[0].ServiceName)
}

func _stopTask(cluster, taskArn string) {
	svc := ecs.New(session.New(), _getAwsConfig())

	params := &ecs.StopTaskInput{
		Cluster: aws.String(cluster),
		Reason: aws.String("Stopped by WriterTool"),
		Task: aws.String(taskArn),
	}

	_, err := svc.StopTask(params)

	if (err != nil) {
		errState(err.Error())
	}
}

func _waitForNewTask(cluster string, service string, tasks []*string) {
	fmt.Print("  Waiting for new task ")
	newTask := ""

	attempts := 60
	sleepTime := 2

	for i := 0; i < attempts; i++ {
		currentTasks := _listTasks(cluster, service)
		if (len(currentTasks.TaskArns) < len(tasks) || len(currentTasks.TaskArns) == 0) {
			fmt.Print(".")
		} else if (newTask == ""){
{'fraction_non_alphanumeric': 0.16675675675675675, 'fraction_numerical': 0.005, 'mean_word_length': 4.441911764705883, 'pattern_counts': {'":': 0, '<': 15, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5576830', 'n_tokens_mistral': 2949, 'n_tokens_neox': 2762, 'n_words': 568}