Investigating the Visual Cron API

Sep 30
2011

We recently moved a lot of our processes to Visual Cron. This has given us a lot more control over how things run and how we’re notified when things go wrong. It’s also given us a very straightforward .NET API to query Visual Cron itself.

Here’s a simple starter script that enumerates all the active jobs and active tasks and appends the most recent exit code. We use something like this to monitor the tasks themselves and figure out whether any of them are consistently failing.

using System;
using System.Linq;
 
using VisualCron;
using VisualCronAPI;
 
namespace VisualCronTest
{
    class Program
    {
        static void Main(string[] args)
        {
            try
            {
                ExitCodes.Load();
 
                OutputJobsAndTasks(new Connection()
                {
                    Address = "VCSERVER01",
                    UserName = "admin",
                    PassWord = "",
                    Port = 16444,
                    ConnectionType = Connection.ConnectionT.Remote
                });
 
                //Add further Visual Cron server connections here
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
            }
        }
 
        public static void OutputJobsAndTasks(Connection conn)
        {
            var c = new Client() { LogToFile = false };
            var s = c.Connect(conn, true);
            var jobs = s.Jobs.GetAll();
 
            foreach (var j in jobs.Where(j => j.Stats.Active))
            {
                Console.WriteLine("JOB : " + j.Name);
                foreach (var t in j.Tasks.Where(t => t.Stats.Active))
                    Console.WriteLine("    TASK : " + t.Name + " - " + t.Stats.ExitCode == 0? "OK" : "ERROR");
            }
        }
    }
}

The only thing I’m personally hating about Visual Cron at the moment is that every time they make a release they break the remote connection API. This is a real pain because you have to recompile everything that was compiled against the previous version. I don’t believe this is down to fundamental API changes, I think it’s something deliberate, but I haven’t had time to investigate exactly what’s going on yet.

CSV Mangling in VB.NET

Feb 07
2011

I’ll probably go to hell for this one since I imagine there are much better libraries out there to do the right thing, but I needed to write a quick and dirty function in VB.NET to split some quoted CSV into it’s constituent fields.

This is horrible, but it works for the moment.

    'A function to split a quoted comma delimited string into separate fields
    Public Function GetFields(ByVal row As String) As String()
        Dim fields As New List(Of String)
        If row.Length = 0 Then
            Return fields.ToArray()
        End If
 
        Dim current As Integer = 0
        Dim buffer As New StringBuilder()
        Dim betweenQuotes As Boolean = False
        Dim eol As Boolean = False
        While Not eol
 
            Select Case row(current)
                Case CChar("""")
                    betweenQuotes = Not betweenQuotes
 
                Case CChar(",")
                    If betweenQuotes Then
                        buffer.Append(row(current))
                    Else
                        fields.Add(buffer.ToString())
                        buffer = New StringBuilder()
                    End If
 
                Case Else
                    buffer.Append(row(current))
 
            End Select
 
            current += 1
            eol = (current = row.Length)
        End While
 
        'Add the last field
        fields.Add(buffer.ToString())
 
        Return fields.ToArray()
    End Function

NB, you’ll need to add these lines to the top of the file :

Imports System.Text
Imports System.Collections.Generic

Visit Our Friends!

A few highly recommended friends...

Pages List

General info about this blog...