Menggunakan pengendali tugas untuk memicu Cloud Functions
Tetap teratur dengan koleksi
Simpan dan kategorikan konten berdasarkan preferensi Anda.
Menggunakan Cloud Tasks untuk memicu Cloud Functions.
Mempelajari lebih lanjut
Untuk dokumentasi mendetail yang menyertakan contoh kode ini, lihat artikel berikut:
Contoh kode
Kecuali dinyatakan lain, konten di halaman ini dilisensikan berdasarkan Lisensi Creative Commons Attribution 4.0, sedangkan contoh kode dilisensikan berdasarkan Lisensi Apache 2.0. Untuk mengetahui informasi selengkapnya, lihat Kebijakan Situs Google Developers. Java adalah merek dagang terdaftar dari Oracle dan/atau afiliasinya.
[[["Mudah dipahami","easyToUnderstand","thumb-up"],["Memecahkan masalah saya","solvedMyProblem","thumb-up"],["Lainnya","otherUp","thumb-up"]],[["Sulit dipahami","hardToUnderstand","thumb-down"],["Informasi atau kode contoh salah","incorrectInformationOrSampleCode","thumb-down"],["Informasi/contoh yang saya butuhkan tidak ada","missingTheInformationSamplesINeed","thumb-down"],["Masalah terjemahan","translationIssue","thumb-down"],["Lainnya","otherDown","thumb-down"]],[],[],[],null,["# Use task handler to trigger Cloud Functions\n\nUses Cloud Tasks to trigger Cloud Functions.\n\nExplore further\n---------------\n\n\nFor detailed documentation that includes this code sample, see the following:\n\n- [Trigger Cloud Run functions using Cloud Tasks](/tasks/docs/tutorial-gcf)\n\nCode sample\n-----------\n\n### Node.js\n\n\nTo learn how to install and use the client library for Cloud Tasks, see\n[Cloud Tasks client libraries](/tasks/docs/reference/libraries).\n\n\nTo authenticate to Cloud Tasks, set up Application Default Credentials.\nFor more information, see\n\n[Set up authentication for a local development environment](/docs/authentication/set-up-adc-local-dev-environment).\n\n const sendgrid = require('@sendgrid/mail');\n\n /**\n * Responds to an HTTP request from Cloud Tasks and sends an email using data\n * from the request body.\n *\n * @param {object} req Cloud Function request context.\n * @param {object} req.body The request payload.\n * @param {string} req.body.to_email Email address of the recipient.\n * @param {string} req.body.to_name Name of the recipient.\n * @param {string} req.body.from_name Name of the sender.\n * @param {object} res Cloud Function response context.\n */\n exports.sendEmail = async (req, res) =\u003e {\n // Get the SendGrid API key from the environment variable.\n const key = process.env.SENDGRID_API_KEY;\n if (!key) {\n const error = new Error(\n 'SENDGRID_API_KEY was not provided as environment variable.'\n );\n error.code = 401;\n throw error;\n }\n sendgrid.setApiKey(key);\n\n // Get the body from the Cloud Task request.\n const {to_email, to_name, from_name} = req.body;\n if (!to_email) {\n const error = new Error('Email address not provided.');\n error.code = 400;\n throw error;\n } else if (!to_name) {\n const error = new Error('Recipient name not provided.');\n error.code = 400;\n throw error;\n } else if (!from_name) {\n const error = new Error('Sender name not provided.');\n error.code = 400;\n throw error;\n }\n\n // Construct the email request.\n const msg = {\n to: to_email,\n from: 'postcard@example.com',\n subject: 'A Postcard Just for You!',\n html: postcardHTML(to_name, from_name),\n };\n\n try {\n await sendgrid.send(msg);\n // Send OK to Cloud Task queue to delete task.\n res.status(200).send('Postcard Sent!');\n } catch (error) {\n // Any status code other than 2xx or 503 will trigger the task to retry.\n res.status(error.code).send(error.message);\n }\n };\n\nWhat's next\n-----------\n\n\nTo search and filter code samples for other Google Cloud products, see the\n[Google Cloud sample browser](/docs/samples?product=cloud_tasks)."]]