1 |
|
2 |
|
3 | import Modem = require('docker-modem')
|
4 |
|
5 | /**
|
6 | * Class representing a task
|
7 | */
|
8 | export class Task {
|
9 | modem: Modem
|
10 | id: String
|
11 | data: Object = {}
|
12 |
|
13 | /**
|
14 | * Create a task
|
15 | * @param {Modem} modem Modem to connect to the remote service
|
16 | * @param {string} id Id of the task (optional)
|
17 | */
|
18 | constructor (modem: Modem, id: String) {
|
19 | this.modem = modem
|
20 | this.id = id
|
21 | }
|
22 |
|
23 | /**
|
24 | * Get low-level information on a task
|
25 | * https://docs.docker.com/engine/reference/api/docker_remote_api_v1.24/#/inspect-a-task
|
26 | * The reason why this module isn't called inspect is because that interferes with the inspect utility of task.
|
27 | * @param {Object} opts Query params in the request (optional)
|
28 | * @param {String} id ID of the task to inspect, if it's not set, use the id of the object (optional)
|
29 | * @return {Promise} Promise return the task
|
30 | */
|
31 | status (opts?: Object) {
|
32 | const call = {
|
33 | path: `/tasks/${this.id}?`,
|
34 | method: 'GET',
|
35 | options: opts,
|
36 | statusCodes: {
|
37 | 200: true,
|
38 | 404: 'no such task',
|
39 | 500: 'server error'
|
40 | }
|
41 | }
|
42 |
|
43 | return new Promise((resolve, reject) => {
|
44 | this.modem.dial(call, (err, conf) => {
|
45 | if (err) return reject(err)
|
46 | const task = new Task(this.modem, this.id)
|
47 | task.data = conf
|
48 | resolve(task)
|
49 | })
|
50 | })
|
51 | }
|
52 | }
|
53 |
|
54 | export default class {
|
55 | modem: Modem
|
56 |
|
57 | /**
|
58 | * Create a task
|
59 | * @param {Modem} modem Modem to connect to the remote service
|
60 | * @param {string} id Id of the task (optional)
|
61 | */
|
62 | constructor (modem: Modem) {
|
63 | this.modem = modem
|
64 | }
|
65 |
|
66 | /**
|
67 | * Get a Task object
|
68 | * @param {id} string ID of the secret
|
69 | * @return {Task}
|
70 | */
|
71 | get (id: String): Task {
|
72 | return new Task(this.modem, id)
|
73 | }
|
74 |
|
75 | /**
|
76 | * Get the list of tasks
|
77 | * https://docs.docker.com/engine/reference/api/docker_remote_api_v1.24/#/list-tasks
|
78 | * @param {Object} opts Query params in the request (optional)
|
79 | * @return {Promise} Promise returning the result as a list of tasks
|
80 | */
|
81 | list (opts?: Object): Promise<Array<Task>> {
|
82 | const call = {
|
83 | path: '/tasks?',
|
84 | method: 'GET',
|
85 | options: opts,
|
86 | statusCodes: {
|
87 | 200: true,
|
88 | 500: 'server error'
|
89 | }
|
90 | }
|
91 |
|
92 | return new Promise((resolve, reject) => {
|
93 | this.modem.dial(call, (err, result) => {
|
94 | if (err) return reject(err)
|
95 | if (!result.Tasks || !result.Tasks.length) return resolve([])
|
96 | resolve(result.Tasks.map((conf) => {
|
97 | const task = new Task(this.modem, conf.ID)
|
98 | task.data = conf
|
99 | return task
|
100 | }))
|
101 | })
|
102 | })
|
103 | }
|
104 | }
|