Skip to content

Commit

Permalink
Fix final statistics message not getting to output file
Browse files Browse the repository at this point in the history
  • Loading branch information
fireflycons committed Sep 28, 2018
1 parent 30027f2 commit 76f8431
Show file tree
Hide file tree
Showing 5 changed files with 107 additions and 89 deletions.
108 changes: 54 additions & 54 deletions Firefly.SqlCmdParser.Client/CommandExecuter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -611,6 +611,60 @@ public virtual void ServerList()
{
}

/// <summary>
/// Writes a message to current <c>stderr</c> destination
/// </summary>
/// <param name="message">The message.</param>
public void WriteStderrMessage(string message)
{
if (this.StderrDestination == OutputDestination.File)
{
if (!message.EndsWith(Environment.NewLine))
{
message += Environment.NewLine;
}

var bytes = Encoding.UTF8.GetBytes(message);
this.stderrFile.Write(bytes, 0, bytes.Length);

// in case someone is tailing the file, flush to keep it up to date.
this.stderrFile.Flush();
}
else
{
this.Message?.Invoke(
this,
new OutputMessageEventArgs(this.NodeNumber, message, this.StderrDestination));
}
}

/// <summary>
/// Writes a message to current <c>stdout</c> destination
/// /// </summary>
/// <param name="message">The message.</param>
public void WriteStdoutMessage(string message)
{
if (this.StdoutDestination == OutputDestination.File)
{
if (!message.EndsWith(Environment.NewLine))
{
message += Environment.NewLine;
}

var bytes = Encoding.UTF8.GetBytes(message);
this.stdoutFile.Write(bytes, 0, bytes.Length);

// in case someone is tailing the file, flush to keep it up to date.
this.stdoutFile.Flush();
}
else
{
this.Message?.Invoke(
this,
new OutputMessageEventArgs(this.NodeNumber, message, this.StdoutDestination));
}
}

/// <inheritdoc />
/// <summary>
/// <c>:XML</c> directive.
Expand Down Expand Up @@ -980,60 +1034,6 @@ private void OutputWithResults(SqlCommand sqlCommand)
}
}

/// <summary>
/// Writes a message to current <c>stderr</c> destination
/// </summary>
/// <param name="message">The message.</param>
private void WriteStderrMessage(string message)
{
if (this.StderrDestination == OutputDestination.File)
{
if (!message.EndsWith(Environment.NewLine))
{
message += Environment.NewLine;
}

var bytes = Encoding.UTF8.GetBytes(message);
this.stderrFile.Write(bytes, 0, bytes.Length);

// in case someone is tailing the file, flush to keep it up to date.
this.stderrFile.Flush();
}
else
{
this.Message?.Invoke(
this,
new OutputMessageEventArgs(this.NodeNumber, message, this.StderrDestination));
}
}

/// <summary>
/// Writes a message to current <c>stdout</c> destination
/// /// </summary>
/// <param name="message">The message.</param>
private void WriteStdoutMessage(string message)
{
if (this.StdoutDestination == OutputDestination.File)
{
if (!message.EndsWith(Environment.NewLine))
{
message += Environment.NewLine;
}

var bytes = Encoding.UTF8.GetBytes(message);
this.stdoutFile.Write(bytes, 0, bytes.Length);

// in case someone is tailing the file, flush to keep it up to date.
this.stdoutFile.Flush();
}
else
{
this.Message?.Invoke(
this,
new OutputMessageEventArgs(this.NodeNumber, message, this.StdoutDestination));
}
}

/// <summary>
/// Buffer object for output of <see cref="CommandExecuter.ExecuteShellCommand"/>
/// </summary>
Expand Down
19 changes: 0 additions & 19 deletions Firefly.SqlCmdParser.Client/SqlExecuteImpl.cs
Original file line number Diff line number Diff line change
Expand Up @@ -260,33 +260,14 @@ private void InvokeParser(int nodeNumber, RunConfiguration runConfiguration)
this.arguments.DisableVariablesSet,
this.arguments.CurrentDirectoryResolver);

var sw = new Stopwatch();
sw.Start();

try
{
parser.Parse();
}
finally
{
sw.Stop();

this.BatchCount += parser.BatchCount;
this.ErrorCount += runConfiguration.CommandExecuter.ErrorCount;
var batch = parser.BatchCount == 1 ? "batch" : "batches";
var error = runConfiguration.CommandExecuter.ErrorCount == 1 ? "error" : "errors";
this.arguments.OutputMessage?.Invoke(
this,
new OutputMessageEventArgs(
nodeNumber,
$"{parser.BatchCount} {batch} processed in {sw.Elapsed.Minutes} min, {sw.Elapsed.Seconds}.{sw.Elapsed.Milliseconds:D3} sec.",
runConfiguration.CommandExecuter.StdoutDestination));
this.arguments.OutputMessage?.Invoke(
this,
new OutputMessageEventArgs(
nodeNumber,
$"{runConfiguration.CommandExecuter.ErrorCount} SQL {error} in execution.",
runConfiguration.CommandExecuter.StdoutDestination));
}
}
}
Expand Down
12 changes: 12 additions & 0 deletions Firefly.SqlCmdParser/ICommandExecuter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -212,5 +212,17 @@ public interface ICommandExecuter : IDisposable
/// <param name="sender">The sender.</param>
/// <param name="args">The <see cref="InputSourceChangedEventArgs"/> instance containing the event data.</param>
void OnInputSourceChanged(object sender, InputSourceChangedEventArgs args);

/// <summary>
/// Writes a message to current <c>stderr</c> destination
/// /// </summary>
/// <param name="message">The message.</param>
void WriteStderrMessage(string message);

/// <summary>
/// Writes a message to current <c>stdout</c> destination
/// /// </summary>
/// <param name="message">The message.</param>
void WriteStdoutMessage(string message);
}
}
50 changes: 37 additions & 13 deletions Firefly.SqlCmdParser/Parser.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
using System;
using System.Collections.Generic;
using System.Data.SqlClient;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Text.RegularExpressions;
Expand Down Expand Up @@ -161,20 +162,22 @@ public Parser(
/// or</exception>
public void Parse()
{
if (this.runConfiguration.OutputFile != null)
var sw = new Stopwatch();

try
{
this.CommandExecuter.Out(OutputDestination.File, this.runConfiguration.OutputFile);
}
if (this.runConfiguration.OutputFile != null)
{
this.CommandExecuter.Out(OutputDestination.File, this.runConfiguration.OutputFile);
}

this.InputSourceChanged += this.CommandExecuter.OnInputSourceChanged;
this.InputSourceChanged += this.CommandExecuter.OnInputSourceChanged;

this.CommandExecuter.ConnectWithConnectionString(this.runConfiguration.ConnectionString);
this.CommandExecuter.ConnectWithConnectionString(this.runConfiguration.ConnectionString);

this.SetInputSource(this.runConfiguration.InitialBatchSource);
var tokenizer = new Tokenizer();
this.SetInputSource(this.runConfiguration.InitialBatchSource);
var tokenizer = new Tokenizer();

try
{
while (this.sourceStack.Count > 0)
{
this.Source = this.sourceStack.Peek();
Expand Down Expand Up @@ -232,13 +235,14 @@ public void Parse()

try
{
// Increment first, so a failed batch is counted
++this.BatchCount;
this.CommandExecuter.ProcessBatch(this.currentBatch, go.ExecutionCount);
}
finally
{
this.previousBatch = this.currentBatch;
this.currentBatch = new SqlBatch();
++this.BatchCount;
}

break;
Expand Down Expand Up @@ -345,12 +349,16 @@ public void Parse()

case ErrorCommand error:

this.CommandExecuter.Error(error.OutputDestination, FileParameterCommand.GetNodeFilepath(this.nodeNumber, error.Filename));
this.CommandExecuter.Error(
error.OutputDestination,
FileParameterCommand.GetNodeFilepath(this.nodeNumber, error.Filename));
break;

case OutCommand @out:

this.CommandExecuter.Out(@out.OutputDestination, FileParameterCommand.GetNodeFilepath(this.nodeNumber, @out.Filename));
this.CommandExecuter.Out(
@out.OutputDestination,
FileParameterCommand.GetNodeFilepath(this.nodeNumber, @out.Filename));
break;

// ReSharper disable once UnusedVariable
Expand Down Expand Up @@ -457,8 +465,9 @@ public void Parse()
// If we have anything left, it's the last batch
if (!string.IsNullOrWhiteSpace(this.currentBatch.Sql))
{
this.CommandExecuter.ProcessBatch(this.currentBatch, 1);
// Increment first, so a failed batch is counted
++this.BatchCount;
this.CommandExecuter.ProcessBatch(this.currentBatch, 1);
}
}
catch (ParserException)
Expand All @@ -473,6 +482,21 @@ public void Parse()
{
throw new ParserException(ex.Message, ex, this.Source);
}
finally
{
// Output statistics
sw.Stop();

var batches = this.BatchCount == 1 ? "batch" : "batches";
var errors = this.CommandExecuter.ErrorCount == 1 ? "error" : "errors";

this.CommandExecuter.WriteStdoutMessage(
string.Join(
Environment.NewLine,
Environment.NewLine,
$"{this.BatchCount} {batches} processed in {sw.Elapsed.Minutes} min, {sw.Elapsed.Seconds}.{sw.Elapsed.Milliseconds:D3} sec.",
$"{this.CommandExecuter.ErrorCount} SQL {errors} in execution."));
}
}

/// <summary>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ public abstract class FileParameterCommand : ICommandMatcher
public abstract CommandType CommandType { get; }

/// <summary>
/// Gets the command text (out, perftrace etc.)
/// Gets the command text (out, perf trace etc.)
/// </summary>
/// <value>
/// The command text.
Expand Down Expand Up @@ -72,10 +72,11 @@ public static string GetNodeFilepath(int nodeNumber, string filepath)
return filepath;
}

var fn = Path.GetFileName(filepath);
var fn = Path.GetFileNameWithoutExtension(filepath);
var ext = Path.GetExtension(filepath);
var dir = Path.GetDirectoryName(filepath);

fn = $"{nodeNumber:D2}-{fn}";
fn = $"{fn}.{nodeNumber:D2}{ext}";

if (!string.IsNullOrEmpty(dir))
{
Expand Down

0 comments on commit 76f8431

Please sign in to comment.