UNPKG

3.67 kBJavaScriptView Raw
1/*
2
3 ----------------------------------------------------------------------------
4 | ewd-document-store: Persistent JavaScript Objects and Document Database |
5 | using Global Storage |
6 | |
7 | Copyright (c) 2017 M/Gateway Developments Ltd, |
8 | Reigate, Surrey UK. |
9 | All rights reserved. |
10 | |
11 | http://www.mgateway.com |
12 | Email: rtweed@mgateway.com |
13 | |
14 | |
15 | Licensed under the Apache License, Version 2.0 (the "License"); |
16 | you may not use this file except in compliance with the License. |
17 | You may obtain a copy of the License at |
18 | |
19 | http://www.apache.org/licenses/LICENSE-2.0 |
20 | |
21 | Unless required by applicable law or agreed to in writing, software |
22 | distributed under the License is distributed on an "AS IS" BASIS, |
23 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
24 | See the License for the specific language governing permissions and |
25 | limitations under the License. |
26 ----------------------------------------------------------------------------
27
28 7 September 2017
29
30*/
31
32// ewd-qoper8 Worker Module example which connects to a Cache database
33
34'use strict';
35
36var DocumentStore = require('ewd-document-store');
37var Cache = require('cache').Cache;
38
39module.exports = function () {
40
41 this.on('start', function (isFirst) {
42 // establish the connection to Cache database
43
44 this.db = new Cache();
45
46 var ok = this.db.open({
47 path: process.env.CACHE_MGR_PATH || '/opt/cache/mgr',
48 username: process.env.CACHE_USERNAME || '_SYSTEM',
49 password: process.env.CACHE_PASSWORD || 'SYS',
50 namespace: process.env.CACHE_NAMESPACE || 'USER'
51 });
52
53 console.log('ok: ' + JSON.stringify(ok));
54
55 this.documentStore = new DocumentStore(this.db);
56
57 // Example of handler for the afterSet event which is fired every time a GlobalNode value changes:
58 this.documentStore.on('afterSet', function (node) {
59 console.log('afterSet: ' + JSON.stringify(node));
60 });
61
62 // Clear down the requests global when ewd-qoper8 first started:
63 if (isFirst) {
64 var glob = new this.documentStore.DocumentNode('requests');
65 glob.delete();
66 }
67 });
68
69 this.on('message', function(messageObj, send, finished) {
70 // For example - save every incoming message object to the requests global
71 var glob = new this.documentStore.DocumentNode('requests', [process.pid]);
72 var ix = glob.increment();
73
74 glob.$(ix).setDocument(messageObj);
75
76 var results = {
77 hello: 'from worker ' + process.pid,
78 time: new Date().toString(),
79 message: messageObj
80 };
81 finished(results);
82 });
83
84 this.on('stop', function() {
85 // Make sure the connection to Cache is closed before the child process closes;
86 console.log('Worker ' + process.pid + ' closing database');
87 this.db.close();
88 });
89
90};