<commit_before>﻿using System;
using System.Diagnostics;
using System.IO;
using System.Threading;
using System.Threading.Tasks;
using Medallion.Shell;
using Topshelf.Logging;

namespace Rollem.TaskRunnerService.Tasks
{
    internal class FileTask : BaseTask
    {
        private readonly LogWriter _logger = HostLogger.Get(typeof(FileTask));

        public FileTask(string taskName, int intervalInMinutes, int timeoutInMinutes)
            : base(taskName, intervalInMinutes, timeoutInMinutes)
        {
        }

        public string FileLocation { get; set; }

        protected override Task ExecuteInternal(CancellationToken token)
        {
            var file = FixUpFileLocation(FileLocation);

            var cmd = Command.Run(file, null, opts =>
            {
                opts
                    .StartInfo(i =>
                    {
                        i.RedirectStandardOutput = true;
                        i.WindowStyle = ProcessWindowStyle.Hidden;
                    });
            });

            return cmd.Task;
        }

        public override void OutputResults(object taskResult)
        {
            var result = taskResult as CommandResult;

            if (result != null)
            {
                _logger.InfoFormat(Tokens.Formats.TaskLogFmt, TaskName, Tokens.TaskResults.Output, result.StandardOutput);

                if (!result.Success)
                {
                    _logger.ErrorFormat(Tokens.Formats.TaskLogFmt, TaskName, Tokens.TaskResults.Error, result.StandardError);
                    _logger.ErrorFormat(Tokens.Formats.TaskLogFmt, TaskName, Tokens.TaskResults.ExitCode, result.ExitCode);
                }   
            }

            base.OutputResults(result);
        }

        private string FixUpFileLocation(string fileLocation)
        {
            //if they use a : we assume full path, otherwise we need to add
            //this location to it.
            return (!fileLocation.Contains(":")
                ? Path.Combine(Environment.CurrentDirectory, FileLocation)
                : fileLocation);
        }
    }
}<commit_msg>bug fix for cancellation token<commit_after>﻿using System;
using System.Diagnostics;
using System.IO;
using System.Threading;
using System.Threading.Tasks;
using Medallion.Shell;
using Topshelf.Logging;

namespace Rollem.TaskRunnerService.Tasks
{
    internal class FileTask : BaseTask
    {
        private readonly LogWriter _logger = HostLogger.Get(typeof(FileTask));

        public FileTask(string taskName, int intervalInMinutes, int timeoutInMinutes)
            : base(taskName, intervalInMinutes, timeoutInMinutes)
        {
        }

        public string FileLocation { get; set; }

        protected override Task ExecuteInternal(CancellationToken token)
        {
            var file = FixUpFileLocation(FileLocation);

            var cmd = Command.Run(file, null, opts =>
            {
                opts
                    .StartInfo(i =>
                    {
                        i.RedirectStandardOutput = true;
                        i.WindowStyle = ProcessWindowStyle.Hidden;
                    });
            });

            //register the kill command to cancellation token
            // https://github.com/madelson/MedallionShell/issues/18
            token.Register(() => cmd.Kill());

            return cmd.Task;
        }

        public override void OutputResults(object taskResult)
        {
            var result = taskResult as CommandResult;

            if (result != null)
            {
                _logger.InfoFormat(Tokens.Formats.TaskLogFmt, TaskName, Tokens.TaskResults.Output, result.StandardOutput);

                if (!result.Success)
                {
                    _logger.ErrorFormat(Tokens.Formats.TaskLogFmt, TaskName, Tokens.TaskResults.Error, result.StandardError);
                    _logger.ErrorFormat(Tokens.Formats.TaskLogFmt, TaskName, Tokens.TaskResults.ExitCode, result.ExitCode);
                }   
            }

            base.OutputResults(result);
        }

        private string FixUpFileLocation(string fileLocation)
        {
            //if they use a : we assume full path, otherwise we need to add
            //this location to it.
            return (!fileLocation.Contains(":")
                ? Path.Combine(Environment.CurrentDirectory, FileLocation)
                : fileLocation);
        }
    }
}
{'fraction_non_alphanumeric': 0.08160036166365281, 'fraction_numerical': 0.0004520795660036166, 'mean_word_length': 1.7882797731568998, 'pattern_counts': {'":': 2, '<': 3, '<?xml version=': 0, '>': 8, 'https://': 1, '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': '5924991', 'n_tokens_mistral': 1119, 'n_tokens_neox': 1080, 'n_words': 244}