new jobs take options again, update schema to match index, add max_attempts, update tests

This commit is contained in:
2016-04-25 11:01:46 -07:00
parent 73e302fdf0
commit d9e19bccf8
2 changed files with 22 additions and 15 deletions

View File

@@ -7,17 +7,20 @@ import createIndex from './helpers/create_index';
const debug = logger('job');
export default class Job extends events.EventEmitter {
constructor(client, index, type, payload, timeout = 10000) {
constructor(client, index, type, payload, options = {}) {
if (typeof type !== 'string') throw new Error('Type must be a string');
if (!isPlainObject(payload)) throw new Error('Payload must be a plain object');
super();
const timeout = options.timeout || 10000;
const maxAttempts = options.max_attempts || 3;
const priority = Math.max(Math.min(options.priority || 10, 20), -20);
this.client = client;
this.index = index;
this.type = type;
this.payload = payload;
this.timeout = timeout;
this.ready = createIndex(client, index)
.then(() => {
@@ -26,9 +29,11 @@ export default class Job extends events.EventEmitter {
type: this.type,
body: {
payload: this.payload,
timeout: this.timeout,
priority: priority,
timeout: timeout,
created_at: new Date(),
attempts: 0,
max_attempts: maxAttempts,
status: JOB_STATUS_PENDING,
}
})