Examples

To help you update your code to take advantage of the new Jobs client, here are some examples.

Loop through import jobs and load job description

Here is an example that uses the Entities client to loop through all Import jobs, and load the job descriptions:

RequestResponse
...
var query = Query.CreateQuery(entities =>
  from e in entities
  where e.DefinitionName == "M.Job"
    && e.Property("Job.Type") == "Import"
  orderby e.CreatedOn descending
  select e);
var jobs = await MClient.Querying.QueryAsync(query);
foreach (var job in jobs.Items)
{
  var jobDescriptionRelation = await job.GetRelationAsync("JobToJobDescription");
  var jobDescriptionId = jobDescriptionRelation?.GetIds()?.FirstOrDefault();
  if (jobDescriptionId is not null)
  {
    var jobDescription = await MClient.Entities.GetAsync(jobDescriptionId.Value);
    ...
  }
}
...

To use the Jobs client, update the code as follows:

RequestResponse
...
var query = Query.CreateQuery(entities =>
  from e in entities
  where e.DefinitionName == "M.Job"
    && e.Property("Job.Type") == "Import"
  orderby e.CreatedOn descending
  select e);
var jobs = await MClient.Querying.QueryAsync(query);
foreach (var job in jobs.Items)
{
  var jobDescription = await MClient.Jobs.GetDescriptionAsync(job.Id);
  ...
}
...

Load a job and its targets

Here is an example that uses the Entities client to load a job and its targets:

RequestResponse
...
var job = await MClient.Entities.GetAsync(jobId);
var jobTargetsRelation = await job.GetRelationAsync("JobToTarget");
var targetIds = jobTargetsRelation?.GetIds();
if (targetIds is not null && targetIds.Any())
{
  var targets = await MClient.Entities.GetManyAsync(targetIds);
  ...
}
...

To use the Jobs client, update the code as follows:

RequestResponse
...
var result = await MClient.Jobs.GetTargetsAsync(jobId);
var targets = result.Items;
...

Query the targets with the condition Failure for a specific job

Here is an example that uses the Entities client to query targets with the condition Failure where the job has a specific job ID.

RequestResponse
...
var query = Query.CreateQuery(entities =>
    from e in entities
    where e.DefinitionName == "M.Target"
      && e.Parent("JobToTarget") == jobId
      && e.Property("Target.Condition") == "Failure"
    select e);
var result = await MClient.Querying.QueryAsync(query);
var targets = result.Items;
...

To use the Jobs client, update the code as follows:

RequestResponse
...
var query = new JobTargetQuery(jobId)
{
    Condition = "Failure"
};
var result = await MClient.Jobs.GetTargetsAsync(query);
var targets = result.Items;
...

Create a description for a job

Here is an example that uses the Entities client to create a description for a job.

RequestResponse
...
var description = await _client.EntityFactory.CreateAsync("M.JobDescription");
description.SetPropertyValue("Job.Data", JToken.FromObject(new
{
    some = "data"
}));
description.GetRelation<IChildToOneParentRelation>("JobToJobDescription").Parent = jobId;

var descriptionId = await MClient.Entities.SaveAsync(description);
...

To use the Jobs client, update the code as follows:

RequestResponse
...
var description = await _client.EntityFactory.CreateAsync("M.JobDescription");
description.SetPropertyValue("Job.Data", JToken.FromObject(new
{
    some = "data"
}));

var descriptionId = await _client.Jobs.SaveDescriptionAsync(jobId, description);
...

Create a job target

Here is an example that uses the Entities client to create a job target.

RequestResponse
...
IEntity target = await _client.EntityFactory.CreateAsync("M.Target");
target.SetPropertyValue("Target.Data", JToken.FromObject(new
{
    same = "data"
}));
target.GetRelation<IChildToOneParentRelation>("JobToTarget").Parent = jobId;

var targetId = await _client.Entities.SaveAsync(target);
...

To use the Jobs client, update the code as follows:

RequestResponse
...
IEntity target = await _client.EntityFactory.CreateAsync("M.Target");
target.SetPropertyValue("Target.Data", JToken.FromObject(new
{
    same = "data"
}));

var targetId = await _client.Jobs.SaveTargetAsync(jobId, target);
...

Do you have some feedback for us?

If you have suggestions for improving this article,