-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add examples to test the zeromq flow
- Loading branch information
1 parent
5e7b864
commit e92a382
Showing
3 changed files
with
50 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,3 +2,4 @@ src/ | |
.DS_Store | ||
.git* | ||
*.md | ||
examples |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
# tasker | ||
# Connects to PULL socket on tcp://*:5555 | ||
# Pushes a flurry of jobs | ||
|
||
port = process.env.PORT or 5555 | ||
jobs = process.env.NUM or 1000 | ||
sleep = process.env.SLEEP or 0 | ||
|
||
context = require 'zeromq' | ||
socket = context.createSocket 'push' | ||
socket.connect "tcp://127.0.0.1:#{port}" | ||
|
||
payload = (new Buffer 100).toString("base64") | ||
socket.send "start:#{jobs}:#{sleep}" | ||
for num in [1..jobs] | ||
socket.send "job:#{num}:#{payload}" | ||
|
||
socket.close() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
# worker | ||
# Binds PULL socket on tcp://*:5555 | ||
# Receives a flurry of jobs from the tasker. | ||
|
||
port = process.env.PORT or 5555 | ||
|
||
context = require 'zeromq' | ||
socket = context.createSocket 'pull' | ||
|
||
countdown = 0 | ||
sleep = 0 | ||
socket.on 'message', (buf) -> | ||
[command,num,extra] = buf.toString().split(":") | ||
if command == 'start' | ||
countdown = parseInt num | ||
sleep = parseInt extra | ||
console.time "#{num} messages" | ||
else | ||
setTimeout -> | ||
if countdown < 2 | ||
console.timeEnd "#{num} messages" | ||
else | ||
countdown -= 1 | ||
, sleep | ||
|
||
socket.bindSync "tcp://*:#{port}" | ||
console.log "Listening..." | ||
|
||
process.on 'SIGINT', -> | ||
console.log "Hanging it up..." | ||
socket.close() |