יצירת הפעלה

יצירת הפעלה של תהליך עבודה.

דוגמת קוד

Go

לפני שמנסים את הדוגמה הזו, צריך לפעול לפי הוראות ההגדרה של Go במדריך למתחילים בנושא תהליכי עבודה באמצעות ספריות לקוח. מידע נוסף מופיע במאמרי העזרה של Workflows Go API.

כדי לבצע אימות ב-Workflows, צריך להגדיר את Application Default Credentials. למידע נוסף, ראו הגדרת אימות לסביבת פיתוח מקומית.


import (
	"context"
	"fmt"
	"io"

	workflows "cloud.google.com/go/workflows/executions/apiv1"
	executionspb "cloud.google.com/go/workflows/executions/apiv1/executionspb"
)

// createExecution starts a new execution of a workflow.
func createExecution(ctx context.Context, w io.Writer, projectID string, locationID string, workflowID string) error {
	parent := fmt.Sprintf("projects/%s/locations/%s/workflows/%s", projectID, locationID, workflowID)
	execution := &executionspb.Execution{
		Argument: `{"message": "example-input"}`,
	}

	client, err := workflows.NewClient(ctx)
	if err != nil {
		return fmt.Errorf("workflows.NewClient: %w", err)
	}
	defer client.Close()

	req := &executionspb.CreateExecutionRequest{
		Parent:    parent,
		Execution: execution,
	}

	resp, err := client.CreateExecution(ctx, req)
	if err != nil {
		return fmt.Errorf("CreateExecution: %w", err)
	}

	fmt.Fprintf(w, "Created execution: %s\n", resp.GetName())
	fmt.Fprintf(w, "Execution state: %v\n", resp.GetState())

	return nil
}

Node.js

לפני שמנסים את הדוגמה הזו, צריך לפעול לפי הוראות ההגדרה של Node.js במדריך למתחילים בנושא תהליכי עבודה באמצעות ספריות לקוח. מידע נוסף מופיע במאמרי העזרה של Workflows Node.js API.

כדי לבצע אימות ב-Workflows, צריך להגדיר את Application Default Credentials. למידע נוסף, ראו הגדרת אימות לסביבת פיתוח מקומית.

const {ExecutionsClient} = require('@google-cloud/workflows');
const client = new ExecutionsClient();

async function createExecution(projectId, location, name) {
  /**
   * TODO(developer): Uncomment these variables before running the sample.
   */
  // const projectId = 'my-project';
  // const location = 'us-central1';
  // const name = 'my-test-workflow';

  const [resp] = await client.createExecution({
    parent: client.workflowPath(projectId, location, name),
  });
  console.info(`name: ${resp.name}`);
}

createExecution(projectId, location, name).catch(err => {
  console.error(err.message);
  process.exitCode = 1;
});

Node.js

לפני שמנסים את הדוגמה הזו, צריך לפעול לפי הוראות ההגדרה של Node.js במדריך למתחילים של Workflows באמצעות ספריות לקוח. מידע נוסף מופיע במאמרי העזרה של Workflows Node.js API.

כדי לבצע אימות ב-Workflows, צריך להגדיר את Application Default Credentials. למידע נוסף, ראו הגדרת אימות לסביבת פיתוח מקומית.

import {ExecutionsClient} from '@google-cloud/workflows';
const client: ExecutionsClient = new ExecutionsClient();

async function createExecution(
  projectId: string,
  location: string,
  name: string
) {
  /**
   * TODO(developer): Uncomment these variables before running the sample.
   */
  // const projectId = 'my-project';
  // const location = 'us-central1';
  // const name = 'my-test-workflow';

  const [resp] = await client.createExecution({
    parent: client.workflowPath(projectId, location, name),
  });
  console.info(`name: ${resp.name}`);
}

createExecution(projectId, location, name).catch((err: Error) => {
  console.error(err.message);
  process.exitCode = 1;
});

המאמרים הבאים

כדי לחפש ולסנן דוגמאות קוד למוצרים אחרים של Google Cloud , ראו דפדפן לדוגמה שלGoogle Cloud .