Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Execution failure due to port number. #70

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ Updates definition of a job category
* Data: same as POST `/api/job`

### **DELETE `/api/job/:jobName`**
Deletes job definition and cancels occurrences
Deletes job definition and cancels occurrences

* Method: DELETE

Expand All @@ -78,8 +78,8 @@ Schedule a job for single or multiple occurrences
interval, // Interval in which job should be invoked (human-interval, can also be a date string for 'once')
data: { // (optional) default: {}
headers, // Http headers, e.g. { Authorization: '<token>' }
params, // Path parameters, to replace `:param` notations in job definition's url
query, // Query parameters (?foo=bar&baz=qux)
params, // An object i.e. { param1: 'value1' } used to replace path parameters `http://mydommain.com:3333/test/:param1` => `http://mydommain.com:3333/test/value1` notations in the job definition's url.
query, // An object i.e. { foo: 'bar', baz: 'qux' } used to create query parameters (http://mydommain.com:3333/test/value1?foo=bar&baz=qux)
body // Accompanying data sent along the request
}
}
Expand Down
40 changes: 25 additions & 15 deletions src/job.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,34 +32,44 @@ const defineJob = async (job, jobs, agenda) => {
agenda.define(name, (job, done) => {
const {attrs: {data}} = job;
let uri = url;
// http://example.com/foo/:param1/:param2
// http://example.com:8888/foo/:param1/:param2
// =>
// http://example.com/foo/value1/value2
for (const [key, value] of items(data.params)) {
uri = uri.replace(`:${key}`, value);
// http://example.com:8888/foo/value1/value2
if (url.indexOf('/:') > 0 && data.params) {
const protoDomain = url.slice(0, url.indexOf('/:'));
let path = url.slice(url.indexOf('/:'));
for (const [key, value] of items(data.params)) {
path = path.replace(`:${key}`, value);
}

uri = `${protoDomain}${path}`;
}

// http://example.com/foo
// =>
// http://example.com/foo?query1=value1&query2=value2
const query = querystring.stringify(data.query);
if (query !== '') {
uri += `?${query}`;
if (data.query) {
const query = querystring.stringify(data.query);
if (query !== '') {
uri += `?${query}`;
}
}

const options = {
method: method || 'POST',
uri,
body: data.body,
headers: data.headers || {},
json: true
};

// Error if no response in timeout
Promise.race([
new Promise((resolve, reject) => setTimeout(() => reject(new Error('TimeOutError')), settings.timeout)),
rp({
method: method || 'POST',
uri,
body: data.body,
headers: data.headers || {},
json: true
})
rp(options)
])
.catch(err => {
job.fail(err.message);
job.fail(`options: ${JSON.stringify(options)} message: ${err.message}`);
return {error: err.message};
})
.then(result => {
Expand Down