מחיקת משרות

בדף הזה מוסבר מתי ואיך למחוק משימות Batch.

כשמוחקים משימה, הפרטים וההיסטוריה שלה שמופיעים כשמציגים משימה ואת המשימות שלה מוסרים מ-Batch. אם רוצים להסיר את כל המידע והמשאבים שמשויכים למשימה, צריך גם למחוק פריטים מכלGoogle Cloud מוצר נוסף שהפעלתם, כמו נושאים ב-Pub/Sub, טבלאות ב-BigQuery או יומנים ב-Cloud Logging.

Google Cloud מחיקה אוטומטית של עבודה 60 ימים אחרי שהיא בוטלה, הסתיימה בהצלחה או נכשלה. לפני שמשימה נמחקת באופן אוטומטי, אתם יכולים לבצע את אחת מהפעולות הבאות:

  • ייצוא פרטי העבודה: אם רוצים לשמור את המידע מהעבודה למשך יותר מ-60 יום, אפשר לייצא את פרטי העבודה ל-BigQuery באמצעות Workflows. מידע נוסף זמין במאמר בנושא ייצוא פרטי משימות.

  • מחיקת משימה: כמו שמוסבר במסמך הזה, אפשר למחוק משימה באופן ידני כשרוצים להסיר אותה מרשימת המשימות בפרויקט וכבר לא צריך את ההיסטוריה שלה. אם מוחקים משימה לפני שהיא מתחילה או בזמן שהיא פועלת, היא מבוטלת ונמחקת.

לפני שמתחילים

  1. אם עוד לא השתמשתם ב-Batch, כדאי לעיין במאמר תחילת העבודה עם Batch ולהפעיל את Batch על ידי השלמת הדרישות המוקדמות לפרויקטים ולמשתמשים.
  2. כדי לקבל את ההרשאות שנדרשות למחיקת משימה, צריך לבקש מהאדמין להקצות לכם את תפקיד ה-IAM‏ Batch Job Editor (roles/batch.jobsEditor) בפרויקט. כדי לקרוא הסבר על מתן תפקידים, ראו איך מנהלים את הגישה ברמת הפרויקט, התיקייה והארגון.

    יכול להיות שאפשר לקבל את ההרשאות הנדרשות גם באמצעות תפקידים בהתאמה אישית או תפקידים מוגדרים מראש.

מחיקת משימה

אפשר למחוק עבודה באמצעות Google Cloud המסוף, ה-CLI של gcloud,‏ Batch API,‏ Go,‏ Java,‏ Node.js,‏ Python או C++.

המסוף

כדי למחוק משימה באמצעות מסוף Google Cloud :

  1. נכנסים לדף Job list במסוף Google Cloud .

    מעבר לרשימת המשימות

  2. לוחצים על השם של העבודה שיצרתם. ייפתח הדף פרטי המשרה.

  3. לוחצים על מחיקה.

  4. בתיבת הדו-שיח Delete batch job? (למחוק את משימה באצווה?), מזינים Delete בשדה.

  5. לוחצים על Delete.

    בדף רשימת המשרות מוצג שהמשרה נמחקה.

gcloud

כדי למחוק עבודה באמצעות ה-CLI של gcloud, משתמשים בפקודה gcloud batch jobs delete.

gcloud batch jobs delete JOB_NAME --location LOCATION

מחליפים את מה שכתוב בשדות הבאים:

  • JOB_NAME: שם המשימה.
  • LOCATION: המיקום של המשרה.

API

כדי למחוק עבודה באמצעות Batch API, משתמשים ב-method‏ jobs.delete:

DELETE https://batch.googleapis.com/v1/projects/PROJECT_ID/locations/LOCATION/jobs/JOB_NAME

מחליפים את מה שכתוב בשדות הבאים:

המשך

Go

מידע נוסף מופיע במאמרי העזרה של Batch Go API.

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

import (
	"context"
	"fmt"
	"io"

	batch "cloud.google.com/go/batch/apiv1"
	"cloud.google.com/go/batch/apiv1/batchpb"
)

// Deletes the specified job
func deleteJob(w io.Writer, projectID, region, jobName string) error {
	// projectID := "your_project_id"
	// region := "us-central1"
	// jobName := "some-job"

	ctx := context.Background()
	batchClient, err := batch.NewClient(ctx)
	if err != nil {
		return fmt.Errorf("NewClient: %w", err)
	}
	defer batchClient.Close()

	req := &batchpb.DeleteJobRequest{
		Name: fmt.Sprintf("projects/%s/locations/%s/jobs/%s", projectID, region, jobName),
	}

	response, err := batchClient.DeleteJob(ctx, req)
	if err != nil {
		return fmt.Errorf("unable to delete job: %w", err)
	}

	fmt.Fprintf(w, "Job deleted: %v\n", response)

	return nil
}

Java

Java

מידע נוסף מופיע במאמרי העזרה של Batch Java API.

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

import com.google.cloud.batch.v1.BatchServiceClient;
import java.io.IOException;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.TimeoutException;

public class DeleteJob {

  public static void main(String[] args)
      throws IOException, ExecutionException, InterruptedException, TimeoutException {
    // TODO(developer): Replace these variables before running the sample.
    // Project ID or project number of the Cloud project you want to use.
    String projectId = "YOUR_PROJECT_ID";

    // Name of the region hosts the job.
    String region = "europe-central2";

    // The name of the job that you want to delete.
    String jobName = "JOB_NAME";

    deleteJob(projectId, region, jobName);
  }

  // Triggers the deletion of a Job.
  public static void deleteJob(String projectId, String region, String jobName)
      throws IOException, ExecutionException, InterruptedException, TimeoutException {
    // Initialize client that will be used to send requests. This client only needs to be created
    // once, and can be reused for multiple requests. After completing all of your requests, call
    // the `batchServiceClient.close()` method on the client to safely
    // clean up any remaining background resources.
    try (BatchServiceClient batchServiceClient = BatchServiceClient.create()) {

      // Construct the parent path of the job.
      String name = String.format("projects/%s/locations/%s/jobs/%s", projectId, region, jobName);

      batchServiceClient.deleteJobAsync(name).get(5, TimeUnit.MINUTES);
      System.out.printf("Delete the job: %s", jobName);
    }
  }
}

Node.js

Node.js

מידע נוסף מופיע במאמרי העזרה של Batch Node.js API.

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

/**
 * TODO(developer): Uncomment and replace these variables before running the sample.
 */
// const projectId = 'YOUR_PROJECT_ID';
/**
 * The region that hosts the job.
 */
// const region = 'us-central-1';
/**
 * The name of the job you want to delete.
 */
// const jobName = 'YOUR_JOB_NAME';

// Imports the Batch library
const batchLib = require('@google-cloud/batch');

// Instantiates a client
const batchClient = new batchLib.v1.BatchServiceClient();

async function callDeleteJob() {
  // Construct request
  const request = {
    name: `projects/${projectId}/locations/${region}/jobs/${jobName}`,
  };

  // Run request
  const [operation] = await batchClient.deleteJob(request);
  const [response] = await operation.promise();
  console.log(response);
}

await callDeleteJob();

Python

Python

מידע נוסף מופיע במאמרי העזרה של Batch Python API.

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

from google.api_core.operation import Operation

from google.cloud import batch_v1


def delete_job(project_id: str, region: str, job_name: str) -> Operation:
    """
    Triggers the deletion of a Job.

    Args:
        project_id: project ID or project number of the Cloud project you want to use.
        region: name of the region hosts the job.
        job_name: the name of the job that you want to delete.

    Returns:
        An operation object related to the deletion. You can call `.result()`
        on it to wait for its completion.
    """
    client = batch_v1.BatchServiceClient()

    return client.delete_job(
        name=f"projects/{project_id}/locations/{region}/jobs/{job_name}"
    )

C++‎

C++

מידע נוסף מופיע במאמרי העזרה של Batch C++ API.

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

#include "google/cloud/batch/v1/batch_client.h"

  [](std::string const& project_id, std::string const& location_id,
     std::string const& job_id) {
    auto const name = "projects/" + project_id + "/locations/" + location_id +
                      "/jobs/" + job_id;
    google::cloud::batch::v1::DeleteJobRequest request;
    request.set_name(name);
    // Initialize a client and issue the request.
    auto client = google::cloud::batch_v1::BatchServiceClient(
        google::cloud::batch_v1::MakeBatchServiceConnection());
    auto future = client.DeleteJob(request);
    // Wait until the long-running operation completes.
    auto success = future.get();
    if (!success) throw std::move(success).status();
    std::cout << "Job " << name << " successfully deleted\n";
  }

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