Returning Result through API - VisualCron - Forum

Community forum

nkheynis88
2015-12-02T21:54:22Z
Hi,

I have my code here below. I need to check if the job is currently running. I found when I get the job variable there is a bool "NotStartIfRunning" but this doesnt appear to be what I need. I need a result to tell me if job is Running, Failed, or Success but I can't seem to figure out how to return this.


// create Server object that holds all VisualCron objects like Jobs, Credentials, Connections etc.
Server s = new Server();

// create Client object that is used to connect to the Server
Client c = new Client();

// create a Connection object which tells how VisualCron should connect
Connection conn = new Connection();

// use remote connection method to connect to a Server instance on another computer (default local connection)
conn.ConnectionType = Connection.ConnectionT.Remote;

// specify the DNS name or IP of remote server
conn.Address = "xxx.xx.xx.xxx";

// specify username you want connect as (default "admin") - user must exist in the Manage user Permissions window at Server
conn.UserName = "admin";

// specify password you want to connect with (default empty)
conn.PassWord = "";

// set below to true if you want to connect with your AD credentials (must be setup on Server to support) - if set you ignore setting username and password
//conn.UseADLogon = true;

// connect to Server, set sync to true to get all Server objects
var connTest = c.Connect(conn, true);

s = connTest;

var lJob = s.Jobs.GetJobByName("Sleep Testing");
var lJobId = lJob.Id;

if (lJob.NotStartIfRunning)
{
lMessage = "currently running.";
return Json(new { msg = lMessage }, JsonRequestBehavior.AllowGet);
}

s.Jobs.Run(lJobId, false);

// finally disconnect
s.Disconnect();
ErikC
2015-12-03T07:25:44Z
Hi nkheynis88 (nice name),

You uare checking the wrong property. The one you are checking is a checkbox in the job.
You shouls use the Stats properties of the job to get what you want:

            JobClass job = s.Jobs.GetJobByName("Sleep Testing");

            switch (job.Stats.Status)
            {
                case JobStatsClass.StatusT.Running:
                    break;
                case JobStatsClass.StatusT.Waiting:
                    break;
                default:
                    break;
            }

            switch (job.Stats.ExitCodeResult)
            {
                case ExitCodeClass.ExitCodeResultT.Failure:
                    break;
                case ExitCodeClass.ExitCodeResultT.NotFound:
                    break;
                case ExitCodeClass.ExitCodeResultT.NotSet:
                    break;
                case ExitCodeClass.ExitCodeResultT.Success:
                    break;
                default:
                    break;
            }


Regards
Erik
Uses Visualcron since 2006.
ErikC
2015-12-03T07:35:21Z
Hi (again),

Some other things to mention:
  • Don't use the 'var' declaration if you know the actual classname: JobClass and String
  • Don't understand the usage of: connTest, use the s = c.Connect(conn,true) and put it in a try catch block because it can fail.


Regards,
Erik
Uses Visualcron since 2006.
Scroll to Top