Skip to content

Commit aed5a26

Browse files
committed
add job/task wait() method
1 parent 4c79f48 commit aed5a26

5 files changed

Lines changed: 38 additions & 35 deletions

File tree

README.md

Lines changed: 24 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ import CloudConvert from 'cloudconvert';
3232

3333
const cloudConvert = new CloudConvert('api_key');
3434

35-
const job = await cloudConvert.jobs.create({
35+
let job = await cloudConvert.jobs.create({
3636
'tasks': {
3737
'import-my-file': {
3838
'operation': 'import/url',
@@ -53,6 +53,29 @@ const job = await cloudConvert.jobs.create({
5353
```
5454
You can use the [CloudConvert Job Builder](https://cloudconvert.com/api/v2/jobs/builder) to see the available options for the various task types.
5555

56+
## Downloading Files
57+
58+
CloudConvert can generate public URLs for using `export/url` tasks. You can use these URLs to download output files.
59+
60+
```js
61+
job = await cloudConvert.jobs.wait(job.id); // Wait for job completion
62+
63+
const exportTask = job.tasks.filter(task => task.operation === 'export/url')[0];
64+
const file = exportTask.result.files[0];
65+
66+
const writeStream = fs.createWriteStream('./my-output.ext');
67+
68+
const response = await axios(file.url, {
69+
responseType: 'stream'
70+
});
71+
72+
response.data.pipe(writeStream);
73+
74+
await new Promise((resolve, reject) => {
75+
writeStream.on('finish', resolve);
76+
writeStream.on('error', reject);
77+
});
78+
```
5679

5780
## Uploading Files
5881

@@ -76,28 +99,6 @@ await cloudConvert.tasks.upload(uploadTask, inputFile);
7699
```
77100

78101

79-
## Downloading Files
80-
81-
CloudConvert can generate public URLs for using `export/url` tasks. You can use these URLs to download output files.
82-
83-
```js
84-
const exportTask = job.tasks.filter(task => task.operation === 'export/url')[0];
85-
const file = exportTask.result.files[0];
86-
87-
const writeStream = fs.createWriteStream('./my-output.ext');
88-
89-
const response = await cloudConvert.axios(file.url, {
90-
responseType: 'stream'
91-
});
92-
93-
response.data.pipe(writeStream);
94-
95-
await new Promise((resolve, reject) => {
96-
writeStream.on('finish', resolve);
97-
writeStream.on('error', reject);
98-
});
99-
```
100-
101102
## Websocket Events
102103

103104
The node SDK can subscribe to events of the [CloudConvert socket.io API](https://cloudconvert.com/api/v2/socket#socket).

lib/JobsResouce.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,11 @@ export default class JobsResource {
1313
return response.data.data;
1414
}
1515

16+
async wait(id) {
17+
const response = await this.cloudConvert.axios.get('jobs/' + id + '/wait');
18+
return response.data.data;
19+
}
20+
1621
async all(query = null) {
1722
const response = await this.cloudConvert.axios.get('jobs', {
1823
params: query || {}

lib/TasksResouce.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,11 @@ export default class TasksResource {
1313
return response.data.data;
1414
}
1515

16+
async wait(id) {
17+
const response = await this.cloudConvert.axios.get('tasks/' + id + '/wait');
18+
return response.data.data;
19+
}
20+
1621
async all(query = null) {
1722
const response = await this.cloudConvert.axios.get('tasks', {
1823
params: query || {}

tests/integration/JobsResourceTest.js

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import {assert} from "chai";
33
import * as fs from 'fs';
44
import * as os from 'os';
55
import apiKey from './ApiKey';
6+
import axios from "axios";
67

78

89
describe('JobsResouce', () => {
@@ -41,15 +42,10 @@ describe('JobsResouce', () => {
4142

4243
await this.cloudConvert.tasks.upload(uploadTask, stream);
4344

44-
// wait for job finished
45-
while (job.status !== 'finished' && job.status !== 'error') {
46-
await new Promise(done => setTimeout(done, 1000));
47-
job = await this.cloudConvert.jobs.get(job.id);
48-
}
45+
job = await this.cloudConvert.jobs.wait(job.id);
4946

5047
assert.equal(job.status, 'finished');
5148

52-
5349
// download export file
5450

5551
const exportTask = job.tasks.filter(task => task.name === 'export-it')[0];
@@ -59,7 +55,7 @@ describe('JobsResouce', () => {
5955

6056
const writer = fs.createWriteStream(this.tmpPath);
6157

62-
const response = await this.cloudConvert.axios(file.url, {
58+
const response = await axios(file.url, {
6359
responseType: 'stream'
6460
});
6561

tests/integration/TasksResourceTest.js

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -24,11 +24,7 @@ describe('TasksResouce', () => {
2424

2525
await this.cloudConvert.tasks.upload(task, stream);
2626

27-
// wait for task finished
28-
while(task.status !== 'finished' && task.status !== 'error') {
29-
await new Promise(done => setTimeout(done, 1000));
30-
task = await this.cloudConvert.tasks.get(task.id);
31-
}
27+
task = await this.cloudConvert.tasks.wait(task.id);
3228

3329
assert.equal(task.status, 'finished');
3430
assert.equal(task.result.files[0].filename, 'input.png');

0 commit comments

Comments
 (0)