<commit_before>﻿using AzureIntro.AzureHelpers;
using Microsoft.Azure.WebJobs;
using Microsoft.WindowsAzure.Scheduler.Models;
using System;
using System.IO;
using System.Text;
using System.Xml.Serialization;

namespace AzureIntro.WebJobs.AzureScheduler
{
    // note: The name of this class can/should be updated, it will be used in logs displayed via the kudu dashboard.
    public class Functions
    {
        // This function will get triggered/executed when a new message is written 
        // on an Azure Queue called azurescheduler.
        // Note: This name of this method can/should be updated, it will be used in logs displayed via the kudu dashboard.
        // The WebJob SDK registers all methods that include the QueueTrigger parameter all calls them when a new message appears in the specified queue.
        public void ProcessQueueMessage([QueueTrigger("azurescheduler")] string message, TextWriter log)
        {
            // Note: The Azure Scheduler can not drop a json message into a queue, only an XML message.
            // All this WebJob does is read the XML message out of one queue (the payload of which is a json string)
            // and writes that message to a different queue.

            // note: Logs written to the TextWriter will be saved in the AzureStorage account and displayed via the kudu dashboard.
            // Logs written to the console will not by default. (see project AzureWebJobLogsToSql for solution)
            log.WriteLine($"textWriter: message received: {message}");
            Console.WriteLine($"console: message received: {message}");

            var storageQueueMessage = this.DeserializeStorageQueueMessage(message);

            // Note: We will inject dependencies in the WebJobQueue project, just new these up here for this demo.
            var config = new AzureConfiguration();
            var queueService = new AzureStorageQueueService(config.GetConnectionString("AzureWebJobsStorage"));

            queueService.EnqueueMessage("webjobqueue", storageQueueMessage.Message);
        }

        private StorageQueueMessage DeserializeStorageQueueMessage(string message)
        {
            var encoding = this.GetEncoding(message);

            StorageQueueMessage result = null;

            var xmlSerializer = new XmlSerializer(typeof(StorageQueueMessage));
            using (var ms = new MemoryStream(encoding.GetBytes(message)))<commit_msg>add out and error for testing<commit_after>﻿using AzureIntro.AzureHelpers;
using Microsoft.Azure.WebJobs;
using Microsoft.WindowsAzure.Scheduler.Models;
using System;
using System.IO;
using System.Text;
using System.Xml.Serialization;

namespace AzureIntro.WebJobs.AzureScheduler
{
    // note: The name of this class can/should be updated, it will be used in logs displayed via the kudu dashboard.
    public class Functions
    {
        // This function will get triggered/executed when a new message is written 
        // on an Azure Queue called azurescheduler.
        // Note: This name of this method can/should be updated, it will be used in logs displayed via the kudu dashboard.
        // The WebJob SDK registers all methods that include the QueueTrigger parameter all calls them when a new message appears in the specified queue.
        public void ProcessQueueMessage([QueueTrigger("azurescheduler")] string message, TextWriter log)
        {
            // Note: The Azure Scheduler can not drop a json message into a queue, only an XML message.
            // All this WebJob does is read the XML message out of one queue (the payload of which is a json string)
            // and writes that message to a different queue.

            // Note: For an improved logging solution, see the AzureWebJobLogsToSql project.

            // Logs written to the TextWriter param will be saved in the AzureWebJobsDashboard storage account and displayed via the kudu dashboard.
            log.WriteLine($"textWriter: message received: {message.Replace(Environment.NewLine, "")}");

            // Logs written to the console will no be displayed at [kudu]/azurejobs/#/jobs/continuous/AzureScheduler, and are available on the file system /data/jobs/continuous/AzureScheduler/job_log.txt
            Console.Out.WriteLine($"console: message received: {message.Replace(Environment.NewLine, "")}");

            // Console.Error will also be visible via kudu dashboard and the file system, these are not guaranteed to be in order with Console.Out
            Console.Error.WriteLine("console error writeline test");

            var storageQueueMessage = this.DeserializeStorageQueueMessage(message);

            // Note: We will inject dependencies in the WebJobQueue project, just new these up here for this demo.
            var config = new AzureConfiguration();
            var queueService = new AzureStorageQueueService(config.GetConnectionString("AzureWebJobsStorage"));

            queueService.EnqueueMessage("webjobqueue", storageQueueMessage.Message);
        }

        private StorageQueueMessage DeserializeStorageQueueMessage(string message)
        {
            var encoding = this.GetEncoding(message);

            StorageQueueMessage result = null;

            var xmlSerializer = new XmlSerializer(typeof(StorageQueueMessage));
            using (var ms = new MemoryStream(encoding.GetBytes(message)))
{'fraction_non_alphanumeric': 0.06591970121381886, 'fraction_numerical': 0.0, 'mean_word_length': 3.0545041635124903, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 2}, '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': '4435132', 'n_tokens_mistral': 1270, 'n_tokens_neox': 1241, 'n_words': 530}