2.1.0 • Published 6 years ago

gearman v2.1.0

Weekly downloads
188
License
MIT
Repository
github
Last release
6 years ago

Gearman Client and Worker

Greenkeeper badge

Build Status

pros:

  • full implementation of worker and client
  • lean abstraction over raw gearman protocol
  • lots of unit tests
  • fast
  • small
  • fully interoperable with gearman clients and workers written in other languages

cons:

  • lacks elegant high level abstractions for doing work. A bit more boilerplate to write
  • only supports 1 server connection per client/worker

usage

NPM

examples

create a client, create 1 job, and handle it's completion

const gearman = require('gearman')

let client = gearman("localhost", 4730 , {timeout: 3000})  // timeout in milliseconds. 

// handle timeout 
client.on('timeout', function() {
	console.log('Timeout occurred')
	client.close()
})


// handle finished jobs
client.on('WORK_COMPLETE', function(job) {
	console.log('job completed, result:', job.payload.toString())
	client.close()
})

// connect to the gearman server
client.connect(function() {
	// submit a job to uppercase a string with normal priority in the foreground
	client.submitJob('upper', 'Hello, World!')
})
	

create a worker, register a function, and handle jobs

const gearman = require('gearman')

let worker = gearman('127.0.0.1', 4730)

// handle jobs assigned by the server
worker.on('JOB_ASSIGN', function(job) {
	console.log(job.func_name + ' job assigned to this worker')
	let result = job.payload.toString().toUpperCase()
	// notify the server the job is done
	worker.sendWorkComplete(job.handle, result)

	// go back to sleep, telling the server we're ready for more work
	worker.preSleep()
});

// grab a job when the server signals one is available
worker.on('NOOP', function() {  worker.grabJob() })

// connect to the gearman server	
worker.connect(function(){
	// register the functions this worker is capable of
	worker.addFunction('upper')

	// tell the server the worker is going to sleep, waiting for work
	worker.preSleep()
});
2.1.0

6 years ago

2.0.5

6 years ago

2.0.4

7 years ago

2.0.3

7 years ago

2.0.2

8 years ago

2.0.1

8 years ago

2.0.0

8 years ago

1.0.1

9 years ago

1.0.0

10 years ago

0.3.0

13 years ago

0.2.0

13 years ago

0.1.0

13 years ago