Manage jobs
To manage jobs (that is, create, update, and delete operations for jobs and their associated targets) using the Scripting SDK, follow these steps:
-
Create a job entity without any targets, and set its state to
Created. -
Create the job description entity.
-
Create all job targets using the Jobs API. Targets will be automatically linked to the job.
-
Update the job state to
Pending.
Use these steps for jobs with one target or multiple targets.
These steps also apply when using the Web SDK or the REST API to manage jobs.
The following snippet provides an example of how to use the Scripting SDK to manage jobs.
To use the Web SDK instead, replace MClient with client in the following code.
var jobId = await CreateJobEntityAsync();
var jobDescriptionId = await CreateJobDescriptionEntityAsync(jobId);
var assets = [12345, 67890];
foreach (var assetd in asssets)
{
var targetId = await CreateTargetEntityAsync(asset, jobId);
}
await UpdateJobEntityAsync(jobdId);
async Task<long> CreateJobEntityAsync()
{
var newJobEntity = await MClient.EntityFactory.CreateAsync("M.Job");
newJobEntity.SetPropertyValue("Job.State", "Created");
... // other properties setup
return await MClient.Entities.SaveAsync(newJobEntity);
}
async Task<long> UpdateJobEntityAsync(long jobId)
{
var job = await MClient.Enties.GetAsync(jobId);
newJobEntity.SetPropertyValue("Job.State", "Pending");
return await MClient.Entities.SaveAsync(job);
}
async Task<long> CreateJobDescriptionEntityAsync(long jobId)
{
var newjobDescriptionEntity = await MClient.EntityFactory.CreateAsync("M.JobDescription");
newjobDescriptionEntity.SetPropertyValue("Job.ConfigurationJob.Configuration", JToken.FromObject(new
{
some = "configuration"
}));
... // other properties setup
return await MClient.Jobs.SaveDescriptionAsync(jobId, newjobDescriptionEntity);
}
async Task<long> CreateTargetEntityAsync(long assetId, long jobId)
{
var newTargetEntity = await MClient.EntityFactory.CreateAsync("M.Target");
newTargetEntity.SetPropertyValue("Target.Location", assetId.ToString());
newTargetEntity.SetPropertyValue("Target.State", "Pending");
newTargetEntity.SetPropertyValue("Target.Condition", "Pending");
... // other properties setup
return await MClient.Jobs.SaveTargetAsync(jobId, newTargetEntity);
}