Switch from the Entities client to the Jobs client
With the introduction of a Jobs client, you need to replace the use of the EntitiesClient
and the QueryingClient
with the JobClient
when working with M.Target
and M.JobDescription
entities.
To help you update your code to take advantage of the Jobs client, we provide more detailed examples.
The following examples illustrate how to replace the use of the EntitiesClient
and the QueryingClient
with the JobClient
when using the Scripting SDK to work with M.Target
and M.JobDescription
entities.
Fetch a Job description
Here is an example that retrieves a job description using the Entities client.
// by id
IEntity jobDescription = await MClient.Entities.GetAsync(jobDescriptionId);
// by identifier
IEntity jobDescription = await MClient.Entities.GetAsync(jobDescriptionIdentifier);
// by querying
Query query = Query.CreateQuery(entities =>
from e in entities
where e.DefinitionName == "M.JobDescription"
select e);
IEntityQueryResult result = await MClient.Querying.QueryAsync(query);
IList<IEntity> entities = result.Items;
To use the Jobs client, update the code as follows:
IEntity entity = await MClient.Jobs.GetDescriptionAsync(jobId);
Save a job description
Here is an example that uses the Entities client.
// create
IEntity entity = await MClient.EntityFactory.CreateAsync("M.JobDescripton");
entity.SetPropertyValue("Job.Configuration", JToken.Parse("{'some': 'new configuration'}"));
entity.GetRelation<IChildToOneParentRelation>("JobToJobDescription").Parent = jobId;
var entityId = await MClient.Entities.SaveAsync(entity);
// update
IEntity entity = await MClient.Entities.GetAsync(jobDescriptionId);
entity.SetPropertyValue("Job.Configuration", JToken.Parse("{'some': 'configuration'}"));
var entityId = await MClient.Entities.SaveAsync(entity);
To use the Jobs client, update the code as follows:
// create
IEntity entity = await MClient.EntityFactory.CreateAsync("M.JobDescripton");
entity.SetPropertyValue("Job.Configuration", JToken.Parse("{'some': 'configuration'}"));
var entityId = await MClient.Jobs.SaveDescriptionAsync(jobId, entity);
// update
IEntity entity = await MClient.Jobs.GetDescriptionAsync(jobId);
entity.SetPropertyValue("Job.Configuration", JToken.Parse("{'some': 'new configuration'}"));
var entityId = await MClient.Jobs.SaveDescriptionAsync(jobId, entity);
Delete a job description
Here is an example that uses the Entities client.
await _entitiesClient.DeleteAsync(jobDescriptionId);
To use the Jobs client, update the code as follows:
await _jobsClient.DeleteDescriptionAsync(jobId);
Fetch a job target
Here is an example that uses the Entities client.
// by id
IEntity entity = await MClient.Entities.GetAsync(jobTargetId);
// by identifier
IEntity entity = await MClient.Entitites.GetAsync(jobTargetIdentifier);
// by querying
Query query = Query.CreateQuery(entities =>
from e in entities
where e.DefinitionName == "M.Target"
&& e.Parent("JobToTarget") == jobId
&& e.Property("Target.State") == "Waiting"
orderby e.CreatedOn descending
select e);
IEntityQueryResult result = await MClient.Querying.QueryAsync(query);
IList<Entity> entities = result.Items;
To use the Jobs client, update the code as follows:
JobTargetQuery request = new JobTargetQuery(jobId)
{
State = "Waiting",
Skip = 0,
Take = 50,
Cultures = new[] { new CultureInfo("en-US") }
};
IEntityCollectionResult result = await MClient.Jobs.GetTargetsAsync(request);
IList<IEntity> entities = result.Items;
Save a job target
Here is an example that uses the Entities client.
// create
IEntity entity = await MClient.EntityFactory.CreateAsync("M.Target");
entity.SetPropertyValue("Target.Location", "some location");
entity.SetPropertyValue("Target.Data", JToken.FromObject(new
{
some = "data"
}));
entity.GetRelation<IChildToOneParentRelation>("JobToTarget").Parent = jobId;
await MClient.Entities.SaveAsync(entity);
// update
IEntity entity = await MClient.Entities.GetAsync(targetId);
entity.SetPropertyValue("Target.Location", "new location");
await MClient.Entities.SaveAsync(entity);
To use the Jobs client, update the code as follows:
// create
IEntity entity = await MClient.EntityFactory.CreateAsync("M.Target");
entity.SetPropertyValue("Target.Location", "some location");
entity.SetPropertyValue("Target.Data", JToken.FromObject(new
{
some = "data"
}));
var entityId = await MClient.Jobs.SaveTargetAsync(jobId, entity);
// update
IEntityCollectionResult result =
await MClient.Jobs.GetTargetsAsync(new JobTargetQuery(jobId));
IEntity entity = result.Items.First();
entity.SetPropertyValue("Target.Location", "new location");
var entityId = await MClient.Jobs.SaveTargetAsync(jobId, entity);
Delete a job target
Here is an example that uses the Entities client.
await _entitiesClient.DeleteAsync(targetId);
To use the Jobs client, update the code as follows:
await _jobsClient.DeleteTargetAsync(jobId, targetId);