0.0.7 • Published 8 years ago

fastjob v0.0.7

Weekly downloads
1
License
MIT
Repository
github
Last release
8 years ago

FastJob Node

Minimalistic task performer based on AWS SQS service with a simple API. It uses cron to read messages from SQS queue and perform jobs, defined in those messages. FIFO queues are preferred.

This library is compatible with node 6 and higher. An AWS account should be set up as well.

Important: AWS credentials (access_key_id and secret_access_key) are either picked up automatically from ~/.aws/config & ~/.aws/credentials or could be provided via env variables: AWS_ACCESS_KEY_ID and AWS_SECRET_ACCESS_KEY.

How to use

$ npm install fastjob
$ AWS_ACCESS_KEY_ID=<your access key> AWS_SECRET_ACCESS_KEY=<secret key> node your_server.js

Then:

const FastJob = require('fastjob');
...
FastJob.init({
	account: <YOUR_AWS_ACCOUNT_NUMBER> //i.e. 298774526974
	region: <AWS_REGION>, //i.e. us-east-1
	queue_name: <SQS_QUEUE_NAME>, //i.e. `my_job_queue.fifo`
	/* other config parameters*/
});

In order for FastJob to recognize SQS messages and perform the corresponding jobs, one should add at least one handler:

FastJob.registerJobHandler({ 
	name: 'myHandler', 
	action: (data, message, callback) => {
		//method logic here
	}
});

Start:

FastJob.start((err, res) => {/*...*/});

The callback inside start is optional. It will be called after every iteration.

Now, if a message is sent to the AWS <SQS_QUEUE_NAME> queue, FastJob will read it and forward to myHandler action to perform the job. For example, if you send a message

const msg_data = {
	"handler":"myHandler",
	"data": {
        "key1": "val1",
        "key2": 222,
        "key3": true
    },
    "testkey": "testvalue"
}

to the queue, FastJob will read it and call the corresponding action defined in registerJobHandler:

(data, message, callback) => {
	//method logic here
}

where data is msg_data, message is the full sqs message and callback is the action's callback.

SQS message is either deleted after job is successfully finished OR returned back to the queue if a handler returns an error.

Config

TODO: describe config

Add new jobs

TODO: describe adding new jobs

Using before/after middleware

TODO: describe middleware

Environment variables

TODO

Running tests

$ AWS_ACCOUNT=<your_account_id> AWS_REGION=<aws_region> SQS_QUEUE_NAME=<aws_sqs_queue_name> npm run test

Example

TODO: add an example

TODO

  • Ensure SQS queue exists
  • Add Promises suppoer
  • Add async/await support
  • Add custom polling intervals for cron

License

Contributions

All contributions are welcome. Just open a pull request.

Feel free to ask me questions on Twitter @invibot