Please note that VisualCron support is not actively monitoring this community forum. Please use our contact page for contacting the VisualCron support directly.


rleatherwood
2010-05-05T17:33:34Z
I am having difficulty running a single task via the API. I am currently running VC 5.5.5. My logic is below. I'm looping through all my Jobs, and selecting anything that has the name "Test". I then loop through each 'Active' task for that "Test" Job, and if the status has the word "Fail" in it, or a Non-Zero ExitCode, I then want to rerun just that single Task. But the Task is not running. I can rerun the Job as a whole using the logic (vcServer.Jobs.Run(jc.Id, true)). Any help or insight would be greatly appreciated. Thanks.

foreach (JobClass jc in vcServer.Jobs.GetAll())
{
     string jname = jc.Name.Trim();
     if (jname.ToString().ToUpper().Contains("TEST"))
     {
         foreach (TaskClass tc in jc.Tasks)
         {
              string tcname = tc.Name.Trim();
              if (tc.Stats.Active)
              {
                   if (tc.Stats.Status.ToString().ToUpper().Contains("FAIL") || (tc.Stats.ExitCode != 0))
                   {
                        vcServer.Jobs.Tasks.Run(jc.Id, tc.Id, true);
                   }
              }
         }
     }
}
Sponsor
Forum information
Support
2010-05-05T17:38:03Z
Hmm.. the Status that you are comparing against FAIL is an enum that can either have value "Running" or "Waiting".
Henrik
Support
http://www.visualcron.com 
Please like  VisualCron on facebook!
Support
2010-05-05T17:39:06Z
Perhaps you are looking for tc.Stats.Result
Henrik
Support
http://www.visualcron.com 
Please like  VisualCron on facebook!
rleatherwood
2010-05-05T17:52:48Z
Sorry, yeah, I meant to have the Result status. But even when I skip over that piece of logic, I still can't manually execute the Task Run to run the individual task.



if (tc.Stats.Result.ToString().ToUpper().Contains("FAIL") || (tc.Stats.ExitCode != 0))
{
     vcServer.Jobs.Tasks.Run(jc.Id, tc.Id, true);
}
Support
2010-05-05T19:15:03Z
Perhaps you are using any Conditions preventing? What does the server log say?
Henrik
Support
http://www.visualcron.com 
Please like  VisualCron on facebook!
rleatherwood
2010-05-05T20:09:04Z
I have one variable parameter that is empty. I looked at the log, and I am connecting. Then when I run the Task.Run command, nothing happens, but when I run it again, it then launches the task. Almost as if something is not being initilized the first time through, but is the second and third time, and so on. I can run it as many times as I want after I get past the first attempt.
Support
2010-05-05T20:19:57Z
Perhaps you are not connected properly before running?

The VisualCron Client uses the API itself and we do run the same function when right clicking on a Task.
Henrik
Support
http://www.visualcron.com 
Please like  VisualCron on facebook!
rleatherwood
2010-05-05T20:50:14Z
I'm connecting remotely.

But still what's weird, is that if I want to launch just the Job, it will launch the first time without any problems. So I don't think it is a connection issue. But if I only want to launch just an individual task, I have to run the Task Command twice.



vcClient = new VisualCronAPI.Client();
vcClient.LogToFile = false;

vcConnection = new Connection();
vcConnection.UserName = "admin";
vcConnection.PassWord = "";
vcConnection.Port = 16444;
vcConnection.ConnectionType = Connection.ConnectionT.Remote;
vcConnection.Address = "SvrName";
vcConnection.TimeOut = 600;

Server RemoteServer = new Server();

return RemoteServer;
rleatherwood
2010-05-05T21:34:33Z
So, I added some logic to test the status of the Task to see if it is running, and it not, attempt to run it again. What was weird, is that while stepping through it, as soon as I checked the Status of the task to see if it was running, the Task executed without me having to attempt to run it again. It is almost as if the API was waiting for another command/response from VC before executing the Task.


boolean TaskRunning = false;
vcServer.Jobs.Tasks.Run(jc.Id, tc.Id, true);
if (!tc.Stats.Status.ToString().ToUpper().Equals("RUNNING"))
     TaskRunning = true;
if (!TaskRunning)
     vcServer.Jobs.Tasks.Run(jc.Id, tc.Id, true);


Thus, I can work with this, since I have to wait for the Task to finish executing before checking the status of the next task within the job, and running it if it failed as well.

Support
2010-05-06T11:24:09Z
Please follow the connection sample from the API project in the API folder. It seems like your example is not waiting for the connection to be complete.

It should be something like this:

Quote:

Server s;
// create a client
Client c = new Client();
// we do not log to file at this time
c.LogToFile = false;
// create a new connection
Connection conn = new Connection();
// specify that it should be local - you may want to specify username/password but currently we are using default values
conn.ConnectionType = Connection.ConnectionT.Local;
// connect - which returns a Server object
try
{
s = c.Connect(conn, true);
}
catch (VisualCronAPI.Client.ClientLoginFailedException ex)
{
MessageBox.Show(ex.Message);
}


Henrik
Support
http://www.visualcron.com 
Please like  VisualCron on facebook!
rleatherwood
2010-05-06T15:50:15Z
I think the connection logic is fine, otherwise I wouldn't be able to loop through the jobs and tasks to begin with. Here's my new logic of running the task, and waiting for it to complete before proceeding to the next task:


vcServer.Jobs.Tasks.Run(jc.Id, tc.Id, true);
TaskRunning = true;
do
{
     System.Threading.Thread.Sleep(1000);
     if (!tc.Stats.Result.ToString().ToUpper().Equals("RUNNING"))
          TaskRunning = false;
}
while (TaskRunning);
Scroll to Top