Class: RestWorker([options])¶
The base object, which is mixed in the custom worker. When any worker is loaded an instance of that worker is mixed-in with RestWorker, not inherited. The restnoded framework makes calls to functions in RestWorker. If the function is defined in the custom user worker then that function is called rather then the function implemented on this object.
Name | Required | Type | Default Value | Description |
---|---|---|---|---|
options | N | Object | collection of properties to be applied to RestWorker | |
options.eventChannel | N | EventEmitter | pub/sub event channel | |
options.availabilityMonitor | N | AvailabilityMonitor | object used to verify other workers are available | |
options.restOperationFactory | N | RestOperationFactory | factory class used to create RestOperations to send http | |
options.restRequestSender | N | Object | module:restRequestSender | object with functions to help send restOperations |
options.restHelper | N | Object | object with commonly used restWorker functions | |
options.wellKnownPorts | N | Object | object with commonly used constant values | |
options.restUtil | N | Object | object with commonly used functions | |
options.url | N | Object | nodejs url object @see {@link https://nodejs.org/api/url.html} |
RestWorker Members¶
(instance) -> API_STATUS¶
Set of values allowed by the apiStatus property of a rest worker when marked as public. Optionally, use to mark the readiness status of an API. This method is used by F5 to generate self-describing APIs, and enable developers to check the status of an API
// mark this particular myWorker API as 'internal only'
myWorker.onStart = function(success) {
this.apiStatus = this.API_STATUS.INTERNAL_ONLY;
}
(instance) -> logger¶
logger implements logging function
// log an informational and a severe message
MyWorker.prototype.OnGet = function(success, error) {
this.logger.info("info message");
this.logger.severe("severe message");
}
(instance) -> this¶
User configuation properties of the RestWorker instance.
Name | Type | Value | Description |
---|---|---|---|
isPersisted | boolean | false | Enable persistence on RestWorker state. Persisted workers should use completeRestOperation with body set to state to update the storage. Persisted state should include selfLink and generation. When it is not included, framework will initialize selfLink based on worker URI and generation to 0. |
isInMemory | boolean | false | Stores persisted data to in memory only. Requires isPersisted to be true. |
isStateRequiredOnStart | boolean | false | Enable loading persisted state from storage during start-up. This is applicable only for persisted workers. Persisted workers that does not load state during start-up can load the state on demand using loadState function. Replication and indexing of persisted state is not yet supported. |
isPassThrough | boolean | false | Enable worker URL routing as passthrough. |
isPublic | boolean | false | Boolean flag indicating whether the worker has public visibility. |
isPostIdempotent | boolean | false | Allow idempotent POST for collections. |
apiStatus | RestWorker#API_STATUS | RestWorker.API_STATUS.NO_STATUS | The F5 API LifeCycle state for this resource. |
isHelperWorker | boolean | false | Boolean flag indicating whether the worker is designated as a helper module. |
uri | String | The url the resource is to be accessed at. | |
dependencies | Array | false | Array containing external worker dependencies. |
isSingleton | boolean | false | Indicates if the worker is a single document. |
odataAdapterLink | String | this.wellKnownPorts.ODATA_NODE_ADAPTER_URI | Set the default adapter for odata workers |
function MyWorker (){
this.isPersisted = true;
this.isPassThrough = true;
this.apiStatus = this.API_STATUS.GA;
}
RestWorker Methods¶
(instance) -> onStart(success, error)¶
First event fired as worker is being loaded. At this point the worker is “unavailable”. The worker will become available after dependent workers are available, and state has been loaded (if worker is persisted)
Name | Required | Type | Default Value | Description |
---|---|---|---|---|
success | Y | function | Function called when onStart is successfully complete. | |
error | Y | function | Function called when onStart encounters an error. |
(instance) -> onStartCompleted(success, error, state, errMsg)¶
Second event fired after dependencies are met and state has been loaded from /shared/storage. If the worker.isPersisted or worker.isStateRequiredOnStart is false state will be undefined. User need not implement this function unless custom worker wants to use loaded state or need to run specific logic when dependencies are available.
Name | Required | Type | Default Value | Description |
---|---|---|---|---|
success | Y | function | Function called when onStartCompleted successfully completes. The helper worker /available is registered after this and the url for this worker will handle HTTP events. | |
error | Y | function | Function called when onStartCompleted encounters an error. | |
state | Y | Object | State object loaded from storage. | |
errMsg | Y | (Object|null) | Error message from loading state. |
MyWorker.prototype.onStartCompleted = function(success, error, loadedState, errorMsg) {
if (errorMsg) {
error("EchoWorker error loading state on start: ", errorMsg);
return;
}
}
(instance) -> onShutDown()¶
The onShutDown function andles situation when server shuts this worker down. The following actions take place when called - - Unregisters all the helper path, such as /available - Unregisters sits subscribers - Unregisters from index worker - Unregisters the public uri
(instance) -> loadState(workerUri, callback)¶
Loads state.
Name | Required | Type | Default Value | Description |
---|---|---|---|---|
workerUri | Y | url | path: /shared/echo-js, optional, default is this.getUri(). | |
callback | Y | function | function (err, state) { if (err) … }, mandatory. |
// load state on start of the get operation (set into this.state previosuly)
SkeletonWorker.prototype.onGet = function(restOperation) {
var oThis = this;
// Instead of returning what is in memory manually load the state
// from storage using helper provided by restWorker and send that
// in response
this.loadState(null,
function (err, state) {
if (err) {
oThis.logger.warning("[SkeletonWorker] error loading state: %s", err.message);
restOperation.fail(err);
return;
}
restOperation.setBody(state);
oThis.completeRestOperation(restOperation);
}
);
};
(instance) -> saveState(workerUri, state, callback)¶
Saves state. Sets metadata if missing, bumps up generation, and set lastUpdateMicros to now.
Name | Required | Type | Default Value | Description |
---|---|---|---|---|
workerUri | Y | url | path `/shared/echo-js`, optional, default is this.getUri(). | |
state | Y | Object | state to be saved, mandatory. Metadata gets updated. | |
callback | Y | function | function (err) { if (err) … }, mandatory. |
(instance) -> deleteState(workerUri, callback)¶
Deletes state.
Name | Required | Type | Default Value | Description |
---|---|---|---|---|
workerUri | Y | external:url | path: /shared/echo-js, optional, default is this.getUri(). | |
callback | Y | function | function (err) { if (err) … }, mandatory. |
(instance) -> isHelper()¶
returns true if worker instance is a helper worker a helper worker is typically a uri attached to WORKER_URI an example of a helper worker are /available or /example
Type | Description |
---|---|
Boolean | true if is a helper worker |
(instance) -> completeRestOperation(restOperation, [callback])¶
helper method to help complete the restOperation or initiate state modifying workflow this is normally done before returning a response to a REST request
Name | Required | Type | Default Value | Description |
---|---|---|---|---|
restOperation | Y | RestOperation | restOperation request to be completed. | |
callback | N | function | function invoked once restOperation is completed. |
MyWorker.prototype.onGet = function(restOperation) {
var oThis = this;
if (this.state.unknown) { // some condition
restOperation.setBody({Unknown: "Yes"); // set the body to a fixed state
this.completeRestOperation(restOperation); // and return this body
return;
}
(instance) -> completeRequest(request, statusCode)¶
Completes the incoming request with the give status code
Name | Required | Type | Default Value | Description |
---|---|---|---|---|
request | Y | RestOperation | The RestOperation object used to complete the request. | |
statusCode | Y | integer | The HTTP status code to use for the completed RestOperation. |
MyWorker.prototype.onGet = function(restOperation) {
var oThis = this;
if (this.state.acceptOnly) { // some condition
// return STATUS_ACCEPTED
this.completeRequest(restOperation, this.wellKnownPorts.STATUS_ACCEPTED);
return;
}
}
(instance) -> getExampleState()¶
Called when the /example helper is invoked. To use, call the function return with desired example JSON.
Type | Description |
---|---|
(null|*) | |
boolean | something nonsesnical. |
RestWorker.prototype.getExampleState = function() {
return {
content: "sample data",
integerContent: 1,
stage: stageEnumValues[0]
};
};
(instance) -> setPresentationHtmlFilepath(htmlFilepath)¶
Sets the presentation HTML file path. All presentation files are located under a directory with the installed package’s name. Example: /f5-iAppsLX-hello-world/hi.html
Name | Required | Type | Default Value | Description |
---|---|---|---|---|
htmlFilepath | Y | String | Path to the html file |
function MyWorker(options) {
this.setPresentationHtmlFilepath(this.wellKnwonPorts.DEFAULT_PRESENTATION_HTML);
}