forked from mpneuried/rsmq-worker
-
Notifications
You must be signed in to change notification settings - Fork 0
/
advanced_example.js
54 lines (48 loc) · 1.36 KB
/
advanced_example.js
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
var fs = require( "fs" );
var RSMQWorker = require( "./." );
var fnCheck = function( msg ){
// check function to not exceed the message if the content is `createmessages`
if( msg.message === "createmessages" ){
return true
}
return false
}
var worker = new RSMQWorker( "myqueue", {
interval: [ .1, 1 ], // wait 100ms between every receive and step up to 1,3 on empty receives
invisibletime: 2, // hide received message for 5 sec
maxReceiveCount: 2, // only receive a message 2 times until delete
autostart: true, // start worker on init
customExceedCheck: fnCheck // set the custom exceed check
});
//
worker.on( "message", function( message, next, id ){
console.log( "message", message )
if( message === "createmessages" ){
next( false )
worker.send( JSON.stringify( { type: "writefile", filename: "./test.txt", txt: "Foo Bar" } ) );
worker.send( JSON.stringify( { type: "deletefile", filename: "./test.txt" } ) );
return
}
var _data = JSON.parse( message )
switch( _data.type ){
case "writefile":
fs.writeFile( _data.filename, _data.txt, function( err ){
if( err ){
console.error( err )
}else{
next()
}
});
break;
case "deletefile":
fs.unlink( _data.filename, function( err ){
if( err ){
console.error( err )
}else{
next()
}
});
break;
}
});
worker.send( "createmessages" );