diff --git a/.github/workflows/scheduler.yaml b/.github/workflows/scheduler.yaml new file mode 100644 index 0000000000..16d199d06e --- /dev/null +++ b/.github/workflows/scheduler.yaml @@ -0,0 +1,67 @@ +name: scheduler +on: + push: + branches: + - main + paths: + - 'scheduler/**' + pull_request: + paths: + - 'scheduler/**' + pull_request_target: + types: [labeled] + schedule: + - cron: '0 0 * * 0' +jobs: + test: + if: ${{ github.event.action != 'labeled' || github.event.label.name == 'actions:force-run' }} + runs-on: ubuntu-latest + timeout-minutes: 60 + permissions: + contents: 'write' + pull-requests: 'write' + id-token: 'write' + steps: + - uses: actions/checkout@v3.1.0 + with: + ref: ${{github.event.pull_request.head.ref}} + repository: ${{github.event.pull_request.head.repo.full_name}} + - uses: 'google-github-actions/auth@v1.0.0' + with: + workload_identity_provider: 'projects/1046198160504/locations/global/workloadIdentityPools/github-actions-pool/providers/github-actions-provider' + service_account: 'kokoro-system-test@long-door-651.iam.gserviceaccount.com' + create_credentials_file: 'true' + access_token_lifetime: 600s + - uses: actions/setup-node@v3.5.1 + with: + node-version: 16 + - run: npm install + working-directory: scheduler + - run: npm test + working-directory: scheduler + env: + MOCHA_REPORTER_SUITENAME: scheduler + MOCHA_REPORTER_OUTPUT: scheduler_sponge_log.xml + MOCHA_REPORTER: xunit + - if: ${{ github.event.action == 'labeled' && github.event.label.name == 'actions:force-run' }} + uses: actions/github-script@v6 + with: + github-token: ${{ secrets.GITHUB_TOKEN }} + script: | + try { + await github.rest.issues.removeLabel({ + name: 'actions:force-run', + owner: 'GoogleCloudPlatform', + repo: 'nodejs-docs-samples', + issue_number: context.payload.pull_request.number + }); + } catch (e) { + if (!e.message.includes('Label does not exist')) { + throw e; + } + } + - if: ${{ github.event_name == 'schedule'}} + run: | + curl https://github.com/googleapis/repo-automation-bots/releases/download/flakybot-1.1.0/flakybot -o flakybot -s -L + chmod +x ./flakybot + ./flakybot --repo GoogleCloudPlatform/nodejs-docs-samples --commit_hash ${{github.sha}} --build_url https://github.com/${{github.repository}}/actions/runs/${{github.run_id}} diff --git a/.github/workflows/workflows.json b/.github/workflows/workflows.json index 919d5a5cb3..09b61a0e87 100644 --- a/.github/workflows/workflows.json +++ b/.github/workflows/workflows.json @@ -54,6 +54,7 @@ "datacatalog/cloud-client", "datacatalog/quickstart", "datastore/functions", + "scheduler", "talent", "contact-center-insights", "workflows" diff --git a/scheduler/.eslintrc.yml b/scheduler/.eslintrc.yml new file mode 100644 index 0000000000..282535f55f --- /dev/null +++ b/scheduler/.eslintrc.yml @@ -0,0 +1,3 @@ +--- +rules: + no-console: off diff --git a/scheduler/createJob.js b/scheduler/createJob.js new file mode 100644 index 0000000000..2b29031351 --- /dev/null +++ b/scheduler/createJob.js @@ -0,0 +1,67 @@ +// Copyright 2018 Google LLC +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +// sample-metadata: +// title: Create Job +// description: Create a job that posts to /log_payload on an App Engine service. +// usage: node createJob.js [project-id] [location-id] [app-engine-service-id] + +/** + * Create a job with an App Engine target via the Cloud Scheduler API + */ +async function createJob(projectId, locationId, serviceId) { + // [START cloudscheduler_create_job] + const scheduler = require('@google-cloud/scheduler'); + + // Create a client. + const client = new scheduler.CloudSchedulerClient(); + + // TODO(developer): Uncomment and set the following variables + // const projectId = "PROJECT_ID" + // const locationId = "LOCATION_ID" + // const serviceId = "my-serivce" + + // Construct the fully qualified location path. + const parent = client.locationPath(projectId, locationId); + + // Construct the request body. + const job = { + appEngineHttpTarget: { + appEngineRouting: { + service: serviceId, + }, + relativeUri: '/log_payload', + httpMethod: 'POST', + body: Buffer.from('Hello World'), + }, + schedule: '* * * * *', + timeZone: 'America/Los_Angeles', + }; + + const request = { + parent: parent, + job: job, + }; + + // Use the client to send the job creation request. + const [response] = await client.createJob(request); + console.log(`Created job: ${response.name}`); + // [END cloudscheduler_create_job] +} + +const args = process.argv.slice(2); +createJob(...args).catch(err => { + console.error(err.message); + process.exitCode = 1; +}); diff --git a/scheduler/package.json b/scheduler/package.json new file mode 100644 index 0000000000..b089b4cf84 --- /dev/null +++ b/scheduler/package.json @@ -0,0 +1,25 @@ +{ + "name": "nodejs-scheduler-samples", + "private": true, + "main": "quickstart.js", + "engines": { + "node": ">=12.0.0" + }, + "files": [ + "*.js" + ], + "license": "Apache-2.0", + "scripts": { + "start": "node app.js", + "test": "mocha --timeout 10000 --exit" + }, + "dependencies": { + "@google-cloud/scheduler": "^3.0.5", + "body-parser": "^1.18.3", + "express": "^4.16.4" + }, + "devDependencies": { + "chai": "^4.2.0", + "mocha": "^8.0.0" + } +} diff --git a/scheduler/test/test.samples.js b/scheduler/test/test.samples.js new file mode 100644 index 0000000000..0479a4708f --- /dev/null +++ b/scheduler/test/test.samples.js @@ -0,0 +1,47 @@ +// Copyright 2018 Google LLC +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// https://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +'use strict'; + +const {CloudSchedulerClient} = require('@google-cloud/scheduler'); +const {assert} = require('chai'); +const {describe, it, before} = require('mocha'); +const cp = require('child_process'); + +const client = new CloudSchedulerClient(); +const execSync = cmd => cp.execSync(cmd, {encoding: 'utf-8'}); +const LOCATION_ID = process.env.LOCATION_ID || 'us-central1'; +const SERVICE_ID = 'my-service'; + +describe('Cloud Scheduler Sample Tests', () => { + let PROJECT_ID, stdout; + + before(async () => { + PROJECT_ID = await client.getProjectId(); + }); + + after(async () => { + const jobName = stdout && stdout.trim().split(' ').slice(-1); + if (jobName) { + await client.deleteJob({name: jobName}); + } + }); + + it('should create a scheduler job', async () => { + stdout = execSync( + `node createJob.js ${PROJECT_ID} ${LOCATION_ID} ${SERVICE_ID}` + ); + assert.match(stdout, /Created job/); + }); +});