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:

  1. Create a job entity without any targets, and set its state to Created.

  2. Create the job description entity.

  3. Create all job targets using the Jobs API. Targets will be automatically linked to the job.

  4. Update the job state to Pending.

Use these steps for jobs with one target or multiple targets.

Note

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.

Note

To use the Web SDK instead, replace MClient with client in the following code.

RequestResponse
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);
}

Do you have some feedback for us?

If you have suggestions for improving this article,