diff --git a/__tests__/main.spec.ts b/__tests__/main.spec.ts index 7bde0f4..f2e3be1 100644 --- a/__tests__/main.spec.ts +++ b/__tests__/main.spec.ts @@ -74,8 +74,9 @@ describe('rsconnect', () => { .then(async (poller: rsconnect.ClientTaskPoller) => { for await (const result of poller.poll()) { expect(result).not.toBeNull() - expect(result.status).not.toBeNull() - expect(result.status.length).toBeGreaterThan(-1) + expect(result.output).not.toBeNull() + expect(result.output.length).toBeGreaterThan(-1) + // console.log(result.output) } }) .catch((err: any) => { @@ -97,8 +98,9 @@ describe('rsconnect', () => { .then(async (poller: rsconnect.ClientTaskPoller) => { for await (const result of poller.poll()) { expect(result).not.toBeNull() - expect(result.status).not.toBeNull() - expect(result.status.length).toBeGreaterThan(-1) + expect(result.output).not.toBeNull() + expect(result.output.length).toBeGreaterThan(-1) + // console.log(result.output) } }) .catch((err: any) => { diff --git a/src/APIClient.ts b/src/APIClient.ts index 4d3d02d..2331c83 100644 --- a/src/APIClient.ts +++ b/src/APIClient.ts @@ -7,7 +7,9 @@ import qs from 'qs' import { debugLog, debugEnabled } from './debugLog' import { Application, - ClientTaskResponse, + ClientTaskV0Response, + ClientTaskV1Params, + ClientTaskV1Response, ExtendedBundleResponse, ListApplicationsParams, ListApplicationsResponse, @@ -117,7 +119,7 @@ export class APIClient { ).then((resp: AxiosResponse) => keysToCamel(resp.data)) } - public async deployApp (appID: number, bundleID: number): Promise { + public async deployApp (appID: number, bundleID: number): Promise { return await this.client.post( `applications/${appID}/deploy`, { bundle: bundleID } @@ -152,13 +154,13 @@ export class APIClient { ) } - public async getTask (taskId: string, status?: number): Promise { - return await this.client.get( - `tasks/${taskId}`, - status !== null && status !== undefined - ? { params: { first_status: status } } - : undefined - ).then((resp: AxiosResponse) => keysToCamel(resp.data)) + public async getTask (taskId: string, first?: number, wait?: number): Promise { + const params: ClientTaskV1Params = { + first: (first !== null && first !== undefined) ? first : undefined, + wait: (wait !== null && wait !== undefined) ? wait : undefined + } + return await this.client.get(`v1/tasks/${taskId}`, { params }) + .then((resp: AxiosResponse) => keysToCamel(resp.data)) } public async getBundle (bundleId: number): Promise { diff --git a/src/ClientTaskPoller.ts b/src/ClientTaskPoller.ts index 5eea953..01af6e8 100644 --- a/src/ClientTaskPoller.ts +++ b/src/ClientTaskPoller.ts @@ -1,20 +1,16 @@ import { debugLog } from './debugLog' import { APIClient } from './APIClient' -import { ClientTaskResponse } from './api-types' +import { ClientTaskV1Response } from './api-types' export class ClientTaskPoller { private readonly client: APIClient private readonly taskId: string - private readonly sleepInterval: number + private readonly wait: number - constructor (client: APIClient, taskId: string, sleepInterval?: number) { + constructor (client: APIClient, taskId: string, wait?: number) { this.client = client this.taskId = taskId - this.sleepInterval = ( - sleepInterval !== null && sleepInterval !== undefined - ? sleepInterval - : 500 - ) + this.wait = (wait !== null && wait !== undefined) ? wait : 1 } public async * poll (timeout?: number): AsyncGenerator { @@ -23,28 +19,26 @@ export class ClientTaskPoller { ? ((Date.now() / 1000 | 0) + timeout) : Infinity ) - let lastStatus: number | undefined + let first: number | undefined while ((Date.now() / 1000 | 0) < pollTimeout) { if (this.taskId === '') { debugLog(() => 'ClientTaskPollResult: returning due to empty task id') return } - debugLog(() => `ClientTaskPollResult: sleeping ${JSON.stringify(this.sleepInterval)}`) - await this.sleepTick() - debugLog(() => [ 'ClientTaskPollResult: getting', `task=${JSON.stringify(this.taskId)}`, - `lastStatus=${JSON.stringify(lastStatus)}` + `first=${JSON.stringify(first)}`, + `wait=${JSON.stringify(this.wait)}` ].join(' ')) - const curTask: ClientTaskResponse = await this.client.getTask( - this.taskId, lastStatus + const curTask: ClientTaskV1Response = await this.client.getTask( + this.taskId, first, this.wait ) const res = { - status: curTask.status, + output: curTask.output, type: curTask.result?.type, data: curTask.result?.data } @@ -56,23 +50,17 @@ export class ClientTaskPoller { yield res - lastStatus = curTask.lastStatus + first = curTask.last if (curTask.finished) { debugLog(() => 'ClientTaskPollResult: returning due to finished') return } } } - - private async sleepTick (): Promise { - return await new Promise((resolve: any) => { - setTimeout(() => resolve(), this.sleepInterval) - }) - } } export interface ClientTaskPollResult { - status: string[] + output: string[] type?: string data?: any } diff --git a/src/Deployer.ts b/src/Deployer.ts index db2df0b..3742b23 100644 --- a/src/Deployer.ts +++ b/src/Deployer.ts @@ -5,7 +5,7 @@ import { debugLog } from './debugLog' import { APIClient } from './APIClient' import { Bundle } from './Bundle' import { Bundler } from './Bundler' -import { Application, ClientTaskResponse, ListApplicationsResponse } from './api-types' +import { Application, ClientTaskV0Response, ListApplicationsResponse } from './api-types' import { ApplicationPather } from './ApplicationPather' export interface DeployManifestParams { @@ -208,7 +208,7 @@ export class Deployer { ].join(' ')) return await this.client.deployApp(app.id, uploadedBundle.id) - .then((ct: ClientTaskResponse) => { + .then((ct: ClientTaskV0Response) => { return { appGuid: app.guid, appId: app.id, diff --git a/src/api-types.ts b/src/api-types.ts index 2fb5d28..3f4ee0c 100644 --- a/src/api-types.ts +++ b/src/api-types.ts @@ -47,7 +47,7 @@ export interface Application { git?: AppGitRecord } -export interface ClientTaskResponse { +export interface ClientTaskV0Response { id: string userId: number status: string[] @@ -58,6 +58,21 @@ export interface ClientTaskResponse { lastStatus: number } +export interface ClientTaskV1Params { + first?: number + wait?: number +} + +export interface ClientTaskV1Response { + id: string + output: string[] + result?: ClientTaskResult + finished: boolean + code: number + error: string + last: number +} + export interface ClientTaskResult { type: string data: any