Skip to content
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
110 changes: 110 additions & 0 deletions packages/app-sdk-playground/src/plugins/declarations/tasks.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,110 @@
// Tasks SDK type declarations.
export const TASKS_DECLARATIONS = `
type SdkTaskStatus =
| "pending"
| "running"
| "completed"
| "failed"
| "aborted"
| "stopped";

interface SdkTaskDefinition {
/** Unique task definition ID. */
id: string;
/** Human-readable task title. */
title: string;
/** Task description. */
description?: string;
}

interface SdkTaskRun {
/** Unique task run ID. */
id: string;
/** ISO timestamp when the task started. */
startedOn?: string;
/** ISO timestamp when the task finished. */
finishedOn?: string;
/** Task name. */
name?: string;
/** The task definition ID this run belongs to. */
definitionId: string;
/** Number of iterations the task has completed. */
iterations?: number;
/** Parent task run ID, if this is a child task. */
parentId?: string;
/** Step Functions execution name. */
executionName?: string;
/** Raw event response from the task runner. */
eventResponse?: unknown;
/** Current task status. */
taskStatus: SdkTaskStatus;
/** Input data passed to the task. */
input?: unknown;
/** Output data produced by the task. */
output?: unknown;
}

interface SdkTaskLogItem {
/** Log message. */
message: string;
/** ISO timestamp of the log entry. */
createdOn: string;
/** Log level or type (e.g. "info", "error"). */
type: string;
/** Additional structured data. */
data?: unknown;
/** Error details, if this is an error log entry. */
error?: unknown;
}

interface SdkTaskLog {
/** Unique log ID. */
id: string;
/** ISO timestamp when the log was created. */
createdOn: string;
/** Step Functions execution name. */
executionName?: string;
/** Iteration number this log belongs to. */
iteration?: number;
/** Individual log entries within this log. */
items: SdkTaskLogItem[];
}

interface SdkListLogsParams {
where?: {
/** Filter logs by task run ID. */
task?: string;
};
}

interface SdkTriggerTaskParams {
/** The task definition ID to trigger. */
definition: string;
/** Input data to pass to the task. */
input?: Record<string, unknown>;
}

interface SdkAbortTaskParams {
/** The task run ID to abort. */
id: string;
/** Optional reason for aborting the task. */
message?: string;
}

interface SdkTasks {
/** List all registered task definitions. */
listDefinitions(): Promise<SdkResult<SdkTaskDefinition[], SdkError>>;

/** List all task runs with their status and I/O data. */
listTasks(): Promise<SdkResult<SdkTaskRun[], SdkError>>;

/** List execution logs, optionally filtered by task run ID. */
listLogs(params?: SdkListLogsParams): Promise<SdkResult<SdkTaskLog[], SdkError>>;

/** Trigger a task and start an async execution. */
triggerTask(params: SdkTriggerTaskParams): Promise<SdkResult<SdkTaskRun, SdkError>>;

/** Abort a running task at its next safe checkpoint. */
abortTask(params: SdkAbortTaskParams): Promise<SdkResult<SdkTaskRun, SdkError>>;
}
`;
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import { CMS_DECLARATIONS } from "./declarations/cms.js";
import { TENANT_MANAGER_DECLARATIONS } from "./declarations/tenantManager.js";
import { FILE_MANAGER_DECLARATIONS } from "./declarations/fileManager.js";
import { LANGUAGES_DECLARATIONS } from "./declarations/languages.js";
import { TASKS_DECLARATIONS } from "./declarations/tasks.js";

export const SDK_GLOBAL_DECLARATION = `
${COMMON_DECLARATIONS}
Expand All @@ -27,6 +28,8 @@ ${FILE_MANAGER_DECLARATIONS}

${LANGUAGES_DECLARATIONS}

${TASKS_DECLARATIONS}

// ============================================================================
// MAIN SDK INTERFACE
// ============================================================================
Expand All @@ -43,6 +46,9 @@ interface SdkWebiny {

/** Languages operations: list enabled languages. */
readonly languages: SdkLanguages;

/** Tasks operations: trigger, abort, list tasks and logs. */
readonly tasks: SdkTasks;
}

declare const sdk: SdkWebiny;
Expand Down
6 changes: 3 additions & 3 deletions packages/pulumi-sdk/src/Pulumi.ts
Original file line number Diff line number Diff line change
Expand Up @@ -199,12 +199,11 @@ export class Pulumi {
}

const pluginsDir = path.join(this.pulumiFolder, "plugins");
// Pulumi names plugin directories with a "v" prefix (e.g. resource-aws-v7.25.0).
const requiredPluginDir = `resource-aws-v${pulumiAwsVersion}`;

const pluginBinary =
process.platform === "win32" ? "pulumi-resource-aws.exe" : "pulumi-resource-aws";
const pluginExists = fs.pathExistsSync(
path.join(pluginsDir, requiredPluginDir, pluginBinary)
path.join(pluginsDir, requiredPluginDir, "pulumi-resource-aws")
);

if (!pluginExists) {
Expand All @@ -231,6 +230,7 @@ export class Pulumi {
// lock file are matched by the same prefix check.
const baseName = entry.endsWith(".lock") ? entry.slice(0, -5) : entry;
if (baseName !== requiredPluginDir) {
console.log("DERI", entry);
fs.removeSync(path.join(pluginsDir, entry));
}
}
Expand Down
50 changes: 50 additions & 0 deletions packages/sdk/src/TasksSdk.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
import type { WebinyConfig } from "./types.js";
import type { HttpError, GraphQLError, NetworkError } from "./errors.js";
import type { Result } from "./Result.js";
import type { TaskDefinition, TaskRun, TaskLog } from "./methods/tasks/taskTypes.js";
import type { ListLogsParams } from "./methods/tasks/listLogs.js";
import type { TriggerTaskParams } from "./methods/tasks/triggerTask.js";
import type { AbortTaskParams } from "./methods/tasks/abortTask.js";
import { listDefinitions as listDefinitionsFn } from "./methods/tasks/listDefinitions.js";
import { listTasks as listTasksFn } from "./methods/tasks/listTasks.js";
import { listLogs as listLogsFn } from "./methods/tasks/listLogs.js";
import { triggerTask as triggerTaskFn } from "./methods/tasks/triggerTask.js";
import { abortTask as abortTaskFn } from "./methods/tasks/abortTask.js";

export class TasksSdk {
private config: WebinyConfig;
private fetchFn: typeof fetch;

constructor(config: WebinyConfig) {
this.config = config;
this.fetchFn = config.fetch || fetch;
}

async listDefinitions(): Promise<
Result<TaskDefinition[], HttpError | GraphQLError | NetworkError>
> {
return listDefinitionsFn(this.config, this.fetchFn);
}

async listTasks(): Promise<Result<TaskRun[], HttpError | GraphQLError | NetworkError>> {
return listTasksFn(this.config, this.fetchFn);
}

async listLogs(
params?: ListLogsParams
): Promise<Result<TaskLog[], HttpError | GraphQLError | NetworkError>> {
return listLogsFn(this.config, this.fetchFn, params);
}

async triggerTask(
params: TriggerTaskParams
): Promise<Result<TaskRun, HttpError | GraphQLError | NetworkError>> {
return triggerTaskFn(this.config, this.fetchFn, params);
}

async abortTask(
params: AbortTaskParams
): Promise<Result<TaskRun, HttpError | GraphQLError | NetworkError>> {
return abortTaskFn(this.config, this.fetchFn, params);
}
}
6 changes: 6 additions & 0 deletions packages/sdk/src/Webiny.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,14 @@ import { CmsSdk } from "./CmsSdk.js";
import { TenantManagerSdk } from "./TenantManagerSdk.js";
import { FileManagerSdk } from "./FileManagerSdk.js";
import { LanguagesSdk } from "./LanguagesSdk.js";
import { TasksSdk } from "./TasksSdk.js";

export class Webiny {
public readonly cms: CmsSdk;
public readonly tenantManager: TenantManagerSdk;
public readonly fileManager: FileManagerSdk;
public readonly languages: LanguagesSdk;
public readonly tasks: TasksSdk;

constructor(config: WebinyConfig) {
this.cms = new CmsSdk({
Expand All @@ -27,6 +29,10 @@ export class Webiny {
...config,
tenant: config.tenant || "root"
});
this.tasks = new TasksSdk({
...config,
tenant: config.tenant || "root"
});
}
}

Expand Down
17 changes: 17 additions & 0 deletions packages/sdk/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ export * from "./CmsSdk.js";
export * from "./TenantManagerSdk.js";
export * from "./FileManagerSdk.js";
export * from "./LanguagesSdk.js";
export * from "./TasksSdk.js";
export * from "./types.js";
export { Result } from "./Result.js";
export { HttpError, GraphQLError, NetworkError } from "./errors.js";
Expand Down Expand Up @@ -92,3 +93,19 @@ export type { CompleteMultiPartUploadParams } from "./methods/fileManager/comple

// Export Languages types.
export type { Language } from "./methods/languages/listLanguages.js";

// Export Tasks types.
export type {
TaskStatus,
TaskDefinition,
TaskRun,
TaskLog,
TaskLogItem
} from "./methods/tasks/taskTypes.js";

// Export types from tasks methods.
export type { ListLogsParams } from "./methods/tasks/listLogs.js";

export type { TriggerTaskParams } from "./methods/tasks/triggerTask.js";

export type { AbortTaskParams } from "./methods/tasks/abortTask.js";
70 changes: 70 additions & 0 deletions packages/sdk/src/methods/tasks/abortTask.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
import type { WebinyConfig } from "../../types.js";
import { Result } from "../../Result.js";
import type { HttpError, GraphQLError, NetworkError } from "../../errors.js";
import type { TaskRun } from "./taskTypes.js";

export interface AbortTaskParams {
/** The task run ID to abort. */
id: string;
/** Optional reason for aborting the task. */
message?: string;
}

export async function abortTask(
config: WebinyConfig,
fetchFn: typeof fetch,
params: AbortTaskParams
): Promise<Result<TaskRun, HttpError | GraphQLError | NetworkError>> {
const { id, message } = params;

const { executeGraphQL } = await import("../executeGraphQL.js");

const query = `
mutation AbortTask($id: ID!, $message: String) {
backgroundTasks {
abortTask(id: $id, message: $message) {
data {
id
createdOn
savedOn
startedOn
finishedOn
definitionId
iterations
name
input
output
taskStatus
executionName
eventResponse
parentId
}
error {
message
code
}
}
}
}
`;

const result = await executeGraphQL(config, fetchFn, query, { id, message });

if (result.isFail()) {
return Result.fail(result.error);
}

const responseData = result.value;

if (responseData.backgroundTasks.abortTask.error) {
const { GraphQLError } = await import("../../errors.js");
return Result.fail(
new GraphQLError(
responseData.backgroundTasks.abortTask.error.message,
responseData.backgroundTasks.abortTask.error.code
)
);
}

return Result.ok(responseData.backgroundTasks.abortTask.data);
}
49 changes: 49 additions & 0 deletions packages/sdk/src/methods/tasks/listDefinitions.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
import type { WebinyConfig } from "../../types.js";
import { Result } from "../../Result.js";
import type { HttpError, GraphQLError, NetworkError } from "../../errors.js";
import type { TaskDefinition } from "./taskTypes.js";

export async function listDefinitions(
config: WebinyConfig,
fetchFn: typeof fetch
): Promise<Result<TaskDefinition[], HttpError | GraphQLError | NetworkError>> {
const { executeGraphQL } = await import("../executeGraphQL.js");

const query = `
query ListTaskDefinitions {
backgroundTasks {
listDefinitions {
data {
id
title
description
}
error {
message
code
}
}
}
}
`;

const result = await executeGraphQL(config, fetchFn, query, {});

if (result.isFail()) {
return Result.fail(result.error);
}

const responseData = result.value;

if (responseData.backgroundTasks.listDefinitions.error) {
const { GraphQLError } = await import("../../errors.js");
return Result.fail(
new GraphQLError(
responseData.backgroundTasks.listDefinitions.error.message,
responseData.backgroundTasks.listDefinitions.error.code
)
);
}

return Result.ok(responseData.backgroundTasks.listDefinitions.data);
}
Loading
Loading