This sample activity demonstrates how to setup a basic activity. It simply writes a job name and a known job attribute to disk. In this case, you may wish to signal some other third party application that something needs to be done with this information - for example, telling a bespoke system the job has reached the Completed status.
using System;
using System.Collections.Generic;
using System.IO;
using System.Net;
using System.Runtime.Serialization;
using System.Text;
using Contracts.Data;
using Contracts.Databases;
using System.Linq;
namespace ScriptExample
{
/// <summary>
/// A simple activity to run when we want to write some json to signal to some middleware something has been done, such as a job status update.
/// This can be used to tell a third party service that some work could be done
///
/// Other options might include sending this information to a webservice
/// </summary>
public class WriteJSONActivity : SynergyServerScripting.Workflows.ActivityInstance
{
private const string INPUT_LOCATION = "Output location";
private const string JOB_STATUS_ATTRIBUTE = "JobStatus";
[DataContract]
public class Data
{
[DataMember]
public string job_name;
[DataMember]
public string job_status;
}
/// <summary>
/// Activity to contact a webservice
/// </summary>
public WriteJSONActivity()
: base(Guid.Parse("{95BDDB3E-CBDD-44B7-8DFB-04CEC3F63880}"))
{
}
/// <summary>
/// Perform the activity
/// </summary>
/// <param name="workflow_definition">the definition of the workflow</param>
/// <param name="details">details of the current workflow instance</param>
/// <param name="script_inputs">the inputs</param>
/// <param name="performer_id">who is performing the script</param>
/// <param name="db">an active database connection</param>
/// <param name="error">any errors that result from running the operation</param>
/// <returns>whether the activity was successful</returns>
public override bool Perform(Workflow workflow_definition, IWorkflowInstanceDetails details, Dictionary<string, ScriptInput> script_inputs, EntityID performer_id, IDatabase db, out string error)
{
error = null;
// get our project info
var project_id = details.GetProjectID(db);
var project_info = SynergyServerScripting.Entities.Project.GetProjectInfo(project_id, true, db);
if (project_info == null)
{
error = "Unable to retrieve job information!";
return (false);
}
// validate inputs
var expression_engine = SynergyServerScripting.Entities.Workflow.CreateExpressionEngine(db);
Dictionary<string, string> all_vals = new Dictionary<string, string>();
// make sure we have all our inputs, and they all have values
var expected = GetInputs();
foreach (var exp in expected)
{
if (!exp.is_optional && !script_inputs.ContainsKey(exp.name))
{
error = "Missing expected input - " + exp.name;
return (false);
}
var val = script_inputs[exp.name] as PrimitiveScriptInput;
if (!exp.is_optional)
{
if (val == null)
{
error = "Missing expected input - " + exp.name;
return (false);
}
if (string.IsNullOrWhiteSpace(val.type_value))
{
error = "Missing expected input - " + exp.name;
return (false);
}
}
else
{
if (val == null || string.IsNullOrWhiteSpace(val.type_value))
{
all_vals[exp.name] = "";
continue;
}
}
// process the input for any variables
// this allows { } substitutions inside the path, if it's allowed
all_vals[exp.name] = SynergyServerScripting.Entities.Workflow.ProcessInputForVariables(val.type_value, details, expression_engine, db);
}
var location = all_vals[INPUT_LOCATION];
string file_name = System.IO.Path.Combine(location, DateTime.UtcNow.ToFileTime().ToString());
// build our data object
Data d = new Data();
d.job_name = project_info.name;
// find the status attribute
var job_status_attrib = project_info.attributes.FirstOrDefault(x => x.name == JOB_STATUS_ATTRIBUTE);
if (job_status_attrib == null || job_status_attrib.value == null)
{
error = "No job status attribute found!";
return (false);
}
d.job_status = job_status_attrib.value.Value.ToString();
// convert to json and write to disk
var json = this.ConvertToJson(d, db);
System.IO.File.WriteAllText(file_name, json);
return true;
}
/// <summary>
/// Convert something to json
/// </summary>
/// <param name="data"></param>
/// <param name="current_db"></param>
/// <returns></returns>
private string ConvertToJson(Data data, IDatabase current_db)
{
if (data == null) return "";
System.Runtime.Serialization.Json.DataContractJsonSerializer serializer = new System.Runtime.Serialization.Json.DataContractJsonSerializer(data.GetType());
using (MemoryStream ms = new MemoryStream())
{
serializer.WriteObject(ms, data);
return System.Text.Encoding.UTF8.GetString(ms.ToArray());
}
}
// Get inputs
public override List<ScriptInput> GetInputs()
{
return new List<ScriptInput>
{
new PrimitiveScriptInput(INPUT_LOCATION, "The location to write files to", false, PrimitiveScriptInputType.String),
};
}
// Get general info - used in the UI
public override WorkflowActivityInfo GetGeneralInfo()
{
return new WorkflowActivityInfo()
{
activity_category = "General",
activity_description = "Write job status to disk for middleware",
activity_name = "Export Job Status",
target_type = WorkflowActivityTarget.Job,
};
}
}
}
This language is not supported or no code example is available.