Skip to content

Commit

Permalink
Merge pull request #2809 from PrefectHQ/dylan/enhancement/worker-util…
Browse files Browse the repository at this point in the history
…ities

Enhancement: useWorker
  • Loading branch information
dylanbhughes authored Nov 4, 2024
2 parents bdd2663 + d18049d commit 29e7c30
Show file tree
Hide file tree
Showing 5 changed files with 49 additions and 1 deletion.
1 change: 1 addition & 0 deletions src/compositions/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ export * from './useTaskRunsHistory'
export * from './useThemeTokens'
export * from './useVariable'
export * from './useVariables'
export * from './useWorker'
export * from './useWorkPool'
export * from './useWorkPoolById'
export * from './useWorkPoolLastPolled'
Expand Down
39 changes: 39 additions & 0 deletions src/compositions/useWorker.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
import { useSubscriptionWithDependencies } from '@prefecthq/vue-compositions'
import { computed, MaybeRefOrGetter, toRef, toValue } from 'vue'
import { useCan } from '@/compositions/useCan'
import { useWorkspaceApi } from '@/compositions/useWorkspaceApi'
import { WorkspaceWorkPoolWorkersApi } from '@/services'
import { Getter } from '@/types/reactivity'
import { UseEntitySubscription } from '@/types/useEntitySubscription'

export type UseWorker = UseEntitySubscription<WorkspaceWorkPoolWorkersApi['getWorker'], 'worker'>

export function useWorker(workPoolName: MaybeRefOrGetter<string | null | undefined>, workerId: MaybeRefOrGetter<string | null | undefined>): UseWorker {
const api = useWorkspaceApi()
const can = useCan()

const getter: Getter<[string, string] | null> = () => {
if (!can.read.worker) {
return null
}

const name = toValue(workPoolName)
const id = toValue(workerId)

if (!name || !id) {
return null
}

return [name, id]
}

const parameters = toRef(getter)

const subscription = useSubscriptionWithDependencies(api.workPoolWorkers.getWorker, parameters)
const worker = computed(() => subscription.response)

return {
subscription,
worker,
}
}
1 change: 0 additions & 1 deletion src/models/api/Filters.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import { ServerWorkPoolWorkerStatus } from '@/models/WorkPoolWorkerStatus'
import { ArtifactSortValues, DeploymentSortValues, FlowRunSortValues, FlowSortValues, LogSortValues, TaskRunSortValues, VariableSortValues, WorkPoolWorkerSortValues } from '@/types/SortOptionTypes'

/** A list where results will be returned only if they match all the values in the list */
Expand Down
7 changes: 7 additions & 0 deletions src/services/WorkspaceWorkPoolWorkersApi.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,13 @@ export class WorkspaceWorkPoolWorkersApi extends WorkspaceApi {
return mapper.map('WorkPoolWorkersPaginationResponse', data, 'PaginatedWorkPoolWorkers')
}

public async getWorker(workPoolName: string, workerId: string): Promise<WorkPoolWorker> {
const encodedWorkPoolName = encodeURI(workPoolName)
const { data } = await this.get<WorkPoolWorkerResponse>(`/${encodedWorkPoolName}/workers/${workerId}`)

return mapper.map('WorkPoolWorkerResponse', data, 'WorkPoolWorker')
}

public deleteWorker(arg: WorkerDeleteArg): Promise<void> {
const encodedWorkPoolName = encodeURI(arg.workPoolName)
const encodedWorkerName = encodeURI(arg.workerName)
Expand Down
2 changes: 2 additions & 0 deletions src/services/can.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ export const workspacePermissions = [
'delete:work_queue',
'delete:work_pool_queue',
'delete:work_pool',
'delete:worker',
'delete:workspace_bot_access',
'delete:workspace_user_access',
'delete:variable',
Expand All @@ -54,6 +55,7 @@ export const workspacePermissions = [
'read:work_queue',
'read:work_pool_queue',
'read:work_pool',
'read:worker',
'read:workspace_bot_access',
'read:workspace_settings',
'read:workspace_user_access',
Expand Down

0 comments on commit 29e7c30

Please sign in to comment.