Difference between revisions of "LavishScript:Task Types"

From Lavish Software Wiki
Jump to navigation Jump to search
Line 10: Line 10:
 
== Creating a Task Type ==
 
== Creating a Task Type ==
 
Task Types can be defined by [[LavishScript:Scripts]].
 
Task Types can be defined by [[LavishScript:Scripts]].
 +
 +
To add a Task Type requires a globally-accessible LavishScript Object, e.g. defined by an '''objectdef''' and instantiated with '''variable(global)''', with a method to handle a recurring "pulse" to process the Task. The method needs no parameters, as the '''Context''' [[LavishScript:TLO|TLO]] will be a [[ObjectType:taskpulseargs|taskpulseargs]].
  
 
; Here we provide a template you can follow! Also found at [https://gist.github.com/LaxLacks/5874db4f10499bad2fd9457d83f523b0 gist.github.com]
 
; Here we provide a template you can follow! Also found at [https://gist.github.com/LaxLacks/5874db4f10499bad2fd9457d83f523b0 gist.github.com]

Revision as of 23:13, 10 March 2019

A Task Type defines a routine to process a Task.

For a Task defined as...

{
 "type": "ls1.echo",
 "output":"Hello World!"}
}

... the "ls1.echo" corresponds to a Task Type that is pre-defined by LavishScript. The Task Type is responsible for interpreting the rest of the Task, which in this case indicates that it should output "Hello World!".

Creating a Task Type

Task Types can be defined by LavishScript:Scripts.

To add a Task Type requires a globally-accessible LavishScript Object, e.g. defined by an objectdef and instantiated with variable(global), with a method to handle a recurring "pulse" to process the Task. The method needs no parameters, as the Context TLO will be a taskpulseargs.

Here we provide a template you can follow! Also found at gist.github.com
objectdef mytasks_Controller
{
  variable taskmanager TaskManager=${LMAC.NewTaskManager["mytaskstest"]}

	method Initialize()
	{
		This:AddTaskType["{\"name\":\"my first task type\",\"object\":\"MyTasks\",\"method\":\"Task_Template\"}"]
	}

	method Shutdown() 
	{ 
    		LMAC.TaskManager["mytaskstest"]:Destroy
	}

	method AddTaskType(string jsonData)
	{
		variable int64 id
		id:Set[${LMAC.NewTaskType["${jsonData.Escape}"].ID}]
		if ${id}
		{
			echo "LMAC: Task Type ${id} added: ${LMAC.TaskType[${id}].Name.Escape}"
		}
	}

	method Task_Template()
	{		
		switch ${Context.TaskState}
		{
		case Start
			echo ${Context(type)} ${Context.Timestamp} ${Context.ElapsedMS} ${Context.Task} ${Context.TaskState} instant=${Context.Task.IsInstant} ${Context.Task.Args}
			break
		case Continue
; 			echo ${Context(type)} ${Context.Timestamp} ${Context.ElapsedMS} ${Context.Task} ${Context.TaskState} instant=${Context.Task.IsInstant} ${Context.Task.Args}
			break
		case Stop
			echo ${Context(type)} ${Context.Timestamp} ${Context.ElapsedMS} ${Context.Task} ${Context.TaskState} instant=${Context.Task.IsInstant} ${Context.Task.Args}
			break
		}
	}
  
  ; begin a test!
 method Start()
 {
    TaskManager:BeginTask["{\"type\":\"my first task type\",\"duration\":1.0,\"my setting\":\"my setting value\"}"]
 }
}

variable(global) mytasks_Controller MyTasks

function main()
{
	MyTasks:Start
	while 1
	{
		waitframe
	}
}

atom atexit()
{
}

This example can be saved as, for example, mytasks.iss.

Output

Running the script will produce approximately this output (your timestamps and IDs will likely differ from this example):

LMAC: Task Type 161 added: my first task type
taskpulseargs 271000984 0 164 Start instant=FALSE {"duration":1.000000,"my setting":"my setting value","type":"my first task type"}
taskpulseargs 271001984 1000 164 Stop instant=FALSE {"duration":1.000000,"my setting":"my setting value","type":"my first task type"}

Implementing a Task Type

In our example using the Task_Template method, the Task itself does nothing other than output its state information for testing purposes.

The "ls1.echo" Task could be implemented as follows:

method Task_Template()
{
  echo ${Context.Task.Args[output]}
  Context.Task:Stop
} 

The Task -- to echo the "output" value -- is performed, and then the Task is immediately Stopped. This Task Type is then effectively instant, ignoring any specified duration.