public string CreateTask(
        string projectId = "YOUR-PROJECT-ID",
        string location  = "us-central1",
        string queue     = "my-queue",
        string url       = "http://example.com",
        string payload   = "Hello World!",
        int inSeconds    = 0)
    {
        CloudTasksClient client = CloudTasksClient.Create();
        QueueName        parent = new QueueName(projectId, location, queue);

        var response = client.CreateTask(new CreateTaskRequest
        {
            Parent = parent.ToString(),
            Task   = new Task
            {
                HttpRequest = new HttpRequest
                {
                    HttpMethod = HttpMethod.Post,
                    Url        = url,
                    Body       = ByteString.CopyFromUtf8(payload)
                },
                ScheduleTime = Timestamp.FromDateTime(
                    DateTime.UtcNow.AddSeconds(inSeconds))
            }
        });

        Console.WriteLine($"Created Task {response.Name}");
        return(response.Name);
    }
Ejemplo n.º 2
0
        // Method to put a message on a queue
        // Could be expanded to include message attributes, etc., in a SendMessageRequest
        public async Task <string> SendMessage(string messageBody)
        {
            //Console.WriteLine($"Send message to queue\n  {Configuration.GetValue<string>("sqsqueue")}");
            Console.WriteLine($"Send message to queue\n  {Configuration.GetValue<string>("gqueue")}");
            var projectId = Configuration.GetValue <string>("projectid");
            var location  = Configuration.GetValue <string>("location");
            var queue     = Configuration.GetValue <string>("gqueue");

            Console.WriteLine(messageBody);

            CloudTasksClient client = CloudTasksClient.Create();
            QueueName        parent = new QueueName(projectId, location, queue);

            var response = client.CreateTask(new CreateTaskRequest
            {
                Parent = parent.ToString(),
                Task   = new Google.Cloud.Tasks.V2.Task
                {
                    AppEngineHttpRequest = new AppEngineHttpRequest
                    {
                        HttpMethod  = HttpMethod.Post,
                        RelativeUri = "/sendtransaction",
                        Body        = ByteString.CopyFromUtf8(messageBody),
                    },
                    ScheduleTime = Timestamp.FromDateTime(
                        DateTime.UtcNow.AddSeconds(5))
                }
            });

            Console.WriteLine($"Created Task {response.Name}");
            return(response.Name);
        }
Ejemplo n.º 3
0
        // [START cloud_tasks_appengine_create_task]
        public static object CreateTask(
            string projectId,
            string location,
            string queue,
            string payload,
            int inSeconds)
        {
            CloudTasksClient client = CloudTasksClient.Create();

            QueueName parent = new QueueName(projectId, location, queue);

            var unixEpoch = new DateTime(1970, 1, 1, 0, 0, 0, DateTimeKind.Utc);

            var response = client.CreateTask(new CreateTaskRequest
            {
                Parent = parent.ToString(),
                Task   = new Task
                {
                    AppEngineHttpRequest = new AppEngineHttpRequest
                    {
                        HttpMethod  = HttpMethod.Post,
                        RelativeUri = "/log_payload",
                        Body        = ByteString.CopyFromUtf8(payload)
                    },
                    ScheduleTime = new Timestamp
                    {
                        Seconds = (long)(DateTime.Now.AddSeconds(inSeconds) - unixEpoch).TotalSeconds,
                        Nanos   = 0
                    }
                }
            });

            Console.WriteLine($"Created Task {response.Name}");

            return(0);
        }