<commit_before>package ca.omny.ecs.launcher;

import com.amazonaws.services.dynamodbv2.AmazonDynamoDBClient;
import com.amazonaws.services.dynamodbv2.model.AttributeValue;
import com.amazonaws.services.ecs.AmazonECSClient;
import com.amazonaws.services.ecs.model.ContainerDefinition;
import com.amazonaws.services.ecs.model.CreateServiceRequest;
import com.amazonaws.services.ecs.model.CreateServiceResult;
import com.amazonaws.services.ecs.model.DeleteServiceRequest;
import com.amazonaws.services.ecs.model.DescribeServicesRequest;
import com.amazonaws.services.ecs.model.DescribeServicesResult;
import com.amazonaws.services.ecs.model.RegisterTaskDefinitionRequest;
import com.amazonaws.services.ecs.model.RegisterTaskDefinitionResult;
import com.amazonaws.services.ecs.model.Service;
import com.amazonaws.services.ecs.model.UpdateServiceRequest;
import com.amazonaws.services.s3.AmazonS3Client;
import com.amazonaws.services.s3.model.S3Object;
import com.amazonaws.services.sqs.AmazonSQSClient;
import com.amazonaws.services.sqs.model.Message;
import com.amazonaws.services.sqs.model.ReceiveMessageResult;
import com.amazonaws.util.IOUtils;
import com.github.mustachejava.DefaultMustacheFactory;
import com.github.mustachejava.Mustache;
import com.github.mustachejava.MustacheFactory;
import com.google.gson.Gson;
import java.io.IOException;
import java.io.StringReader;
import java.io.StringWriter;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.logging.Level;
import java.util.logging.Logger;

public class EcsServiceLauncher {

    AmazonSQSClient sqs = new AmazonSQSClient();
    AmazonS3Client s3 = new AmazonS3Client();
    AmazonDynamoDBClient dynamo = new AmazonDynamoDBClient();
    AmazonECSClient ecs = new AmazonECSClient();
    int WAIT_TIME = 20000;
    int RETRIES = 3;

    private final static String DYNAMO_HASH_KEY_NAME = "table";
    private final static String DYNAMO_RANGE_KEY_NAME = "key";

    public void run(String queue, String cluster, String bucket, String db, boolean usePrivateIp) {
        EcsTaskTracker ecsTaskTracker = new EcsTaskTracker(usePrivateIp);
        Gson gson = new Gson();
        MustacheFactory mf = new DefaultMustacheFactory();

        while (true) {
            try {
                ReceiveMessageResult receiveMessage = sqs.receiveMessage(queue);
                for (Message message : receiveMessage.getMessages()) {
                    //register task
                    ServiceUpdateRequest request = gson.fromJson(message.getBody(), ServiceUpdateRequest.class);
                    S3Object taskDefinitionObject = s3.getObject(bucket, request.getSite() + "/" + request.getServiceName() + "/container-definition.json");
                    String taskDefinitionString = IOUtils.toString(taskDefinitionObject.getObjectContent());
                    StringWriter writer = new StringWriter();
                    HashMap<String, Object> scopes = new HashMap<>();
                    scopes.put("buildNumber", request.getBuildNumber());
                    String serviceAndBuildNumber = request.getServiceName() + "-" + request.getBuildNumber();
                    Mustache mustache = mf.compile(new StringReader(taskDefinitionString), serviceAndBuildNumber);
                    mustache.execute(writer, scopes);

                    String family = request.getServiceName();
                    String newTaskDefinitionString = writer.toString();
                    ContainerDefinition definition = gson.fromJson(newTaskDefinitionString, ContainerDefinition.class);
                    RegisterTaskDefinitionResult registerTaskDefinition = ecs.registerTaskDefinition(
                            new RegisterTaskDefinitionRequest()<commit_msg>enable reading message from SNS<commit_after>package ca.omny.ecs.launcher;

import com.amazonaws.services.dynamodbv2.AmazonDynamoDBClient;
import com.amazonaws.services.dynamodbv2.model.AttributeValue;
import com.amazonaws.services.ecs.AmazonECSClient;
import com.amazonaws.services.ecs.model.ContainerDefinition;
import com.amazonaws.services.ecs.model.CreateServiceRequest;
import com.amazonaws.services.ecs.model.CreateServiceResult;
import com.amazonaws.services.ecs.model.DeleteServiceRequest;
import com.amazonaws.services.ecs.model.DescribeServicesRequest;
import com.amazonaws.services.ecs.model.DescribeServicesResult;
import com.amazonaws.services.ecs.model.RegisterTaskDefinitionRequest;
import com.amazonaws.services.ecs.model.RegisterTaskDefinitionResult;
import com.amazonaws.services.ecs.model.Service;
import com.amazonaws.services.ecs.model.UpdateServiceRequest;
import com.amazonaws.services.s3.AmazonS3Client;
import com.amazonaws.services.s3.model.S3Object;
import com.amazonaws.services.sqs.AmazonSQSClient;
import com.amazonaws.services.sqs.model.Message;
import com.amazonaws.services.sqs.model.ReceiveMessageResult;
import com.amazonaws.util.IOUtils;
import com.github.mustachejava.DefaultMustacheFactory;
import com.github.mustachejava.Mustache;
import com.github.mustachejava.MustacheFactory;
import com.google.gson.Gson;
import java.io.IOException;
import java.io.StringReader;
import java.io.StringWriter;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.logging.Level;
import java.util.logging.Logger;

public class EcsServiceLauncher {

    AmazonSQSClient sqs = new AmazonSQSClient();
    AmazonS3Client s3 = new AmazonS3Client();
    AmazonDynamoDBClient dynamo = new AmazonDynamoDBClient();
    AmazonECSClient ecs = new AmazonECSClient();
    int WAIT_TIME = 20000;
    int RETRIES = 3;

    private final static String DYNAMO_HASH_KEY_NAME = "table";
    private final static String DYNAMO_RANGE_KEY_NAME = "key";

    public void run(String queue, String cluster, String bucket, String db, boolean usePrivateIp) {
        EcsTaskTracker ecsTaskTracker = new EcsTaskTracker(usePrivateIp);
        Gson gson = new Gson();
        MustacheFactory mf = new DefaultMustacheFactory();

        while (true) {
            try {
                ReceiveMessageResult receiveMessage = sqs.receiveMessage(queue);
                for (Message message : receiveMessage.getMessages()) {
                    //register task
                    String body = message.getBody();
                    Map m = gson.fromJson(message.getBody(), Map.class);
                    ServiceUpdateRequest request;
                    if(m.containsKey("Message")) {
                        request = gson.fromJson(m.get("Message").toString(), ServiceUpdateRequest.class);
                    } else {
                        request = gson.fromJson(message.getBody(), ServiceUpdateRequest.class);
                    }
                    S3Object taskDefinitionObject = s3.getObject(bucket, request.getSite() + "/" + request.getServiceName() + "/container-definition.json");
                    String taskDefinitionString = IOUtils.toString(taskDefinitionObject.getObjectContent());
                    StringWriter writer = new StringWriter();
                    HashMap<String, Object> scopes = new HashMap<>();
                    scopes.put("buildNumber", request.getBuildNumber());
                    String serviceAndBuildNumber = request.getServiceName() + "-" + request.getBuildNumber();
                    Mustache mustache = mf.compile(new StringReader(taskDefinitionString), serviceAndBuildNumber);
                    mustache.execute(writer, scopes);

                    String family = request.getServiceName();
                    String newTaskDefinitionString = writer.toString();
                    ContainerDefinition definition = gson.fromJson(newTaskDefinitionString, ContainerDefinition.class);
                    RegisterTaskDefinitionResult registerTaskDefinition = ecs.registerTaskDefinition(
                            new RegisterTaskDefinitionRequest()
{'fraction_non_alphanumeric': 0.09042487303356869, 'fraction_numerical': 0.004211569428960733, 'mean_word_length': 3.475609756097561, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 7, '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': '6269651', 'n_tokens_mistral': 2165, 'n_tokens_neox': 2135, 'n_words': 402}