All files Scheduler.ts

75.24% Statements 76/101
83.33% Branches 15/18
42.85% Functions 9/21
98.61% Lines 71/72

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324  1x 1x   1x                                                                                                             1x                                                                                                                                                                                                                                                 44x 26x 26x 1x     25x 25x 25x 25x 25x 25x 25x 25x 25x 25x 25x 25x 25x 25x 25x 25x 25x 25x 25x 25x 25x 25x 25x 25x     1x 4x 4x 1x   3x 3x 1x   2x     1x 26x 26x 26x     1x 248x 248x 28x 26x       1x 8x   1x 8x                                                                             1x 26x 26x 26x           26x     26x   26x 26x 26x   26x 1x 1x             27x 25x 50x   26x           26x       1x  
import Node from "./Node";
import {execute} from "./Edge";
import {ConnectorEvent, LoadEvent, Graph, newId, Logger, nullLogger,
    SchedulerEvent, ExecutionResult, Warning, EdgeError, NodeSetEvent} from "./Shared";
import Loader from "./Loader";
/**
 * # Scheduler
 *
 * This is the graph execution engine.  To create graphs, see [Plastic-IO Graph Editor](https://github.com/plastic-io/graph-editor).
 *
 * The Plastic-IO Graph Editor uses this engine to test and execute graphs in the browser.
 *
 * If you're trying to execute graphs in an AWS environment, you might want to check out the [Plastic-IO Graph Server](https://github.com/plastic-io/graph-server).
 *
 * If you want to implement the scheduler yourself and run graphs in your own way, then you have come to the right place!
 *
 * To begin the execution of a graph, you must first instantiate your graph in the Scheduler.
 * See the {@link default.constructor} for more information on instantiation.  After you instantiate
 * the scheduler, you can call {@link default.url} to directly invoke a node's edge (set function).
 *
 * ## Graph
 * {@link Graph}s contain arrays of {@link Node}s.  Nodes are executable units of code and can be local to the graph, or reference other nodes or graphs.
 *
 * # Scheduler Execution Digram
 *
 *                   1                         2                            3
 *             +-------------+         +---------------+         +--------------------+
 *             |             |         |               |         |                    |
 *             |  Scheduler  |         |  Node Edge    |         |   Edge Connector   |
 *             |             |         |               |         |                    |
 *             +-------------+         +---------------+         +--------------------+
 *                    |                        |                            |
 *                    |                        |                            |
 *                    |                   +-----------------------------------------+
 *                   +-+                  |   +-+        Graph Loop        +-+      |
 *                   | +-Scheduler.url()----->+ |                          | |      |
 *                   | |                  |   | +------edges[field]------->+ |      |
 *                   | |                  |   +-+                          | |      |
 *                   | |                  |    |                           | |      |
 *                   | |                  |   +-+                          | |      |
 *                   | |                  |   | |                          | |      |
 *                   | +<----End--------------+ +<-------------------------+ |      |
 *                   | |                  |   +-+                          | |      |
 *                   | |                  |    |                           +-+      |
 *                   | |                  +-----------------------------------------+
 *                   | |                       |                            |
 *                   | |                       |                            |
 *                   +-+                       |                            |
 *                    |                        |                            |
 *                    +                        +                            +
 *
 *  1. Scheduler.url() calls a node's edge (set function) based on the node.url.
 *  2. Node Edge calls `edges[field]` which invokes n connectors referencing other node edges
 *  3. Node edge is invoked just like step one, creating a graph loop.
 *
 *
 *
 *
 */
export default class Scheduler {
    /**
     * Occurs when the graph begins to navigate to a node's URL via the {@link default.url} function.
     * @event
     */
    begin: (e: SchedulerEvent) => void;
    /**
     * Occurs when a connector value enters into a node's edge (input).
     * @event
     */
    beginedge: (e: SchedulerEvent) => void;
    /**
     * Occurs after all all edge promises are completed.  Note: If your nodes do not return promises.
     * @event
     */
    endedge: (e: SchedulerEvent) => void;
    /**
     * Occurs when an error occurred during the invocation of a node.
     * @event
     */
    error: (e: EdgeError) => void;
    /**
     * Occurs when an external resource is loaded.  You can invoke `setValue` in the event argument to override the default loader with your own.
     * @event
     */
    load: (e: LoadEvent) => void;
    /**
     * Occurs when a warning is logged.
     * @event
     */
    warning: (e: Warning) => void;
    /**
     * Occurs at the end of the initial graph promise chain.  The graph may continue to run as asynchronous functions return.
     * @event
     */
    end: (e: SchedulerEvent) => void;
    /**
     * Occurs just before set is called.  You can use `setContext` to alter the `this` context object of the node just before the node's set function is invoked.
     * @event
     */
    set: (e: NodeSetEvent) => void;
    /**
     * Occurs as a node's set function has called `edges[field] = <value>;`.
     * @event
     */
    beginconnector: (e: ConnectorEvent) => void;
    /**
     * Occurs when a connector promise chain has completed after a node's set function has called `edges[field] = <value>;`.
     * @event
     */
    endconnector: (e: ConnectorEvent) => void;
    /**
     * Occurs after a node's set function has completed.  Contains the return value of the function.  Although this return value is present, using it is considered an anti-pattern and is only included for debugging.
     * @event
     */
    afterSet: (e: NodeSetEvent) => void;
    /** The base graph */
    graph: Graph;
    /** Logging functions */
    logger: Logger;
    /** Loader for loading graphs */
    graphLoader: Loader<Graph>;
    /** Loader for loading nodes */
    nodeLoader: Loader<Node>;
    /** Edge traversal counter */
    sequence: number;
    /** The domain specific context object.  User defined and used by set methods. */
    context: object;
    /** Mutable state object.  This object can be changed and examined later. */
    state: object;
    /** Node specific runtime cache object.  Used in set functions for any reason the author sees fit. */
    nodeCache: {
        [key: string]: {};
    };
    /** The parameterized path to linked graph documents */
    graphPath: string;
    /** The parameterized path to linked node documents */
    nodePath: string;
    /** Holds events in the event bus */
    events: {
        [key: string]: Function[]; // tslint:disable-line
    };
    /**
     * @param graph Graph to execute.
     * @param context What will appear as the `this` object for any node set function that executes on your graph.
     * @param state Object that will be available to node set functions during graph execution.  You can alter this object before or after instantiating the scheduler.  See {@link NodeInterface}.
     * @param logger Optional logging function to listen to info and debug messages coming out of the scheduler.  Functional messages are emitted via the event emitter.
     *
     * ### Basic Usage
     *
     * To start graph execution, instantiate the scheduler and pass your graph to it.
     *
     *   ```TypeScript
     *   const scheduler = new Scheduler(myGraphJSON);
     *   scheduler.url("my-graph-url");
     *   ```
     *
     * ### With Context
     *
     * You can also execute the graph and pass context variables to the nodes.
     * In the example below, nodes executing on myGraph will have access to `{foo: "bar"}` by accessing `this.foo` at runtime.
     * This allows you to attach properties like server response functions, database connections, client side component references, and many other things
     * use full to the node's set function on runtime.
     *   ```TypeScript
     *   const scheduler = new Scheduler(myGraphJSON, {foo: "bar"});
     *   ```
     *
     * ### With State Defined
     * Nodes now have a mutable object with which they can share long running objects, data caches
     * or other resources that are expensive to instantiate per edge invocation.  State is whatever you pass into
     * before you instantiate the scheduler or at run time.  The scheduler has no opinion.
     *   ```TypeScript
     *   const scheduler = new Scheduler(myGraphJSON, {}, {foo: bar});
     *   ```
     *
     * ### Using the Logger
     * You can listen to info and debug messages emitted from the scheduler by attaching an object or class
     * that matches the {@link Logger} interface.  Which happens to be almost exactly like console.
     *   ```TypeScript
     *   const scheduler = new Scheduler(myGraphJSON, {}, {}, console);
     */
    constructor(graph: Graph, context: object = {}, state: object = {}, logger: Logger = nullLogger) {
        logger.debug("Scheduler started");
        if (!graph) {
            throw new Error("No graph was passed to the scheduler constructor.");
        }
        // the following empty methods are here for documentation of events only
        this.begin = (e: SchedulerEvent): void => { e;return; }; // tslint:disable-line
        this.beginedge = (e: SchedulerEvent): void => { e;return; }; // tslint:disable-line
        this.endedge = (e: SchedulerEvent): void => { e;return; }; // tslint:disable-line
        this.error = (e: SchedulerEvent): void => { e;return; }; // tslint:disable-line
        this.load = (e: SchedulerEvent): void => { e;return; }; // tslint:disable-line
        this.begin = (e: SchedulerEvent): void => { e;return; }; // tslint:disable-line
        this.warning = (e: SchedulerEvent): void => { e;return; }; // tslint:disable-line
        this.end = (e: SchedulerEvent): void => { e;return; }; // tslint:disable-line
        this.set = (e: SchedulerEvent): void => { e;return; }; // tslint:disable-line
        this.beginconnector = (e: SchedulerEvent): void => { e;return; }; // tslint:disable-line
        this.endconnector = (e: SchedulerEvent): void => { e;return; }; // tslint:disable-line
        this.afterSet = (e: SchedulerEvent): void => { e;return; }; // tslint:disable-line
        this.graph = graph;
        this.sequence = 0;
        this.context = context;
        this.state = state;
        this.events = {};
        this.nodeCache = {};
        this.graphPath = "artifacts/graph/{id}.{version}";
        this.nodePath = "artifacts/nodes/{id}.{version}";
        this.logger = logger;
        this.graphLoader = new Loader<Graph>(this);
        this.nodeLoader = new Loader<Node>(this);
        this.logger.debug("Startup parameters set");
    }
    /** Removes an event listener */
    removeEventListener(eventName: string, listener: () => void): void {
        this.logger.debug("Scheduler: Remove event " + eventName);
        if (!this.events[eventName]) {
            return;
        }
        const idx = this.events[eventName].indexOf(listener);
        if (idx === -1) {
            return;
        }
        this.events[eventName].splice(idx, 1);
    }
    /** Adds an event listener */
    addEventListener(eventName: string, listener: (eventData: ConnectorEvent| SchedulerEvent | LoadEvent | EdgeError | NodeSetEvent | Error | Warning) => void): void {
        this.logger.debug("Scheduler: Add event " + eventName);
        this.events[eventName] = this.events[eventName] || [];
        this.events[eventName].push(listener);
    }
    /** Dispatches an event */
    dispatchEvent(eventName: string, eventData: SchedulerEvent): void {
        this.logger.debug("Scheduler: Dispatch event " + eventName);
        if (this.events[eventName]) {
            for (const listener of this.events[eventName]) {
                listener.call(this, eventData);
            }
        }
    }
    getNodePath(id: string, version: number): string {
        return this.nodePath.replace("{id}", id).replace("{version}", version.toString());
    }
    getGraphPath(id: string, version: number): string {
        return this.graphPath.replace("{id}", id).replace("{version}", version.toString());
    }
    /**
     *
     *    ### Simple Node Edge Invocation
     *
     * Invoke a Node's Edge (set function).
     *
     *   ```TypeScript
     *   const scheduler = new Scheduler(myGraphJSON);
     *   scheduler.url("my-graph-url");
     *   ```
     *
     *    ### Invoke Edge with Value
     *
     * Invoke a Node's Edge (set function), and send a value to the edge.
     *
     *   ```TypeScript
     *   const scheduler = new Scheduler(myGraphJSON);
     *   scheduler.url("my-graph-url", "some value");
     *   ```
     *
     *    ### Invoke Edge with Value, and Field
     *
     * Invoke a Node's Edge (set function), and send a value to the edge.
     *
     *   ```TypeScript
     *   const scheduler = new Scheduler(myGraphJSON);
     *   scheduler.url("my-graph-url", "some value", "some_field");
     *   ```
     *
     *    ### Advanced: Pass Previous Execution Context
     *
     * By passing a node instance, you can invoke nodes embedded in inner graphs.
     *
     *   ```TypeScript
     *   const scheduler = new Scheduler(myGraphJSON);
     *   scheduler.url("my-graph-url", "some value", "some_field", myInnerNodeInstance);
     */
    async url(url: string, value: any, field: string, currentNode: Node): Promise<ExecutionResult> {
        this.logger.debug("Scheduler: Set URL " + url);
        const start = Date.now();
        this.dispatchEvent("begin", {
            url,
            time: start,
            id: newId(),
        } as SchedulerEvent);
        let graph;
        Iif (currentNode && currentNode.linkedGraph && currentNode.linkedGraph.graph) {
            graph = currentNode.linkedGraph.graph;
        } else {
            graph = this.graph;
        }
        const pattern = new RegExp(url);
        const node = graph.nodes.find((vec: Node) => {
            return pattern.test(vec.url);
        }) as Node;
        if (!node && url) {
            this.logger.warn("Scheduler: Cannot find URL " + url);
            this.dispatchEvent("warning", {
                url,
                time: Date.now(),
                id: newId(),
                message: "Cannot find node at the specified URL.",
            } as Warning);
        }
        if (node) {
            this.logger.info("Executing node at URL " + url);
            await execute(this, graph, node, field, value);
        }
        this.dispatchEvent("end", {
            url,
            time: Date.now(),
            id: newId(),
            duration: Date.now() - start,
        } as SchedulerEvent);
        return {
            nodes: []
        };
    }
}