用于API调用的Google Cloud Functions Cron Job [英] Google Cloud Functions Cron Job for API Call

查看:71
本文介绍了用于API调用的Google Cloud Functions Cron Job的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试设置一个Firebase云函数,该函数定期对Feedly API进行api调用.

I am trying to set up a firebase cloud function that regularly makes an api call to the Feedly API.

但是,它不起作用,我不确定为什么.这是代码:

However, it is not working and I'm not sure why. Here is the code:

const functions = require('firebase-functions')
const express = require('express')
const fetch = require('node-fetch')
const admin = require('firebase-admin')

admin.initializeApp()
const db = admin.firestore()

const app = express()

exports.getNewsArticles = functions.pubsub
  .schedule('every 5 minutes')
  .onRun(() => {
    app.get('/feedly', async (request, response) => {

      const apiUrl = `https://cloud.feedly.com/v3/streams/contents?streamId=user/[USER_ID_NUMBER]/category/global.all&count=100&ranked=newest&newThan=300000`

      const fetchResponse = await fetch(apiUrl, {
        headers: {
          Authorization: `Bearer ${functions.config().feedly.access}`
        }
      })

      const json = await fetchResponse.json()

      json.items.forEach(item => {
        db.collection('news').add({
          status: 'pending',
          author: item.author || '',
          content: item.content || '',
          published: item.published || '',
          summary: item.summary || '',
          title: item.title || '',
        })
      })
    })
  })

您知道我需要做什么才能使其正常工作吗?

Any idea what I need to do to get this to work?

推荐答案

您的Cloud Function中可能存在三个问题:

There are potentially three problems in your Cloud Function:

查看计划功能 HTTPS函数.在计划功能中,您不应使用app.get()而是直接使用它,例如:

Have a look at the doc for Schedule Functions and HTTPS Functions. In a Schedule Function, you should not use app.get() and just do, for example:

exports.scheduledFunction = functions.pubsub.schedule('every 5 minutes').onRun((context) => {
  console.log('This will be run every 5 minutes!');
  return null;
});

2.您必须返回一个Promise(或一个值)

您必须在Cloud Function中返回一个Promise(或一个值),以向平台指示Cloud Function已完成.如果Cloud Function中的任务是同步的,则只需返回一个值即可,例如return null;如上例所示.如果一个或多个任务是异步的,则必须返回一个Promise.

2. You must return a Promise (or a value)

You must return a Promise (or a value) in your Cloud Function, to indicate to the platform that the Cloud Function has completed. In case the tasks in the Cloud Function are synchronous you can just return a a value, e.g. return null; as shown in the example above. In case one or more tasks are asynchronous you must return a Promise.

因此,在您的情况下,您需要使用

So, in your case you need to use Promise.all() as follows, since you are executing several (asynchronous) writes in parallel:

exports.getNewsArticles = functions.pubsub
  .schedule('every 5 minutes')
  .onRun((context) => {

      const apiUrl = `https://cloud.feedly.com/v3/streams/contents?streamId=user/[USER_ID_NUMBER]/category/global.all&count=100&ranked=newest&newThan=300000`

      const fetchResponse = await fetch(apiUrl, {
        headers: {
          Authorization: `Bearer ${functions.config().feedly.access}`
        }
      })

      const json = await fetchResponse.json()

      const promises = [];

      json.items.forEach(item => {
        promises.push(
          db.collection('news').add({
            status: 'pending',
            author: item.author || '',
            content: item.content || '',
            published: item.published || '',
            summary: item.summary || '',
            title: item.title || '',
          }))
      })

      return Promise.all(promises)
  })

3.您可能需要升级价格计划

您需要加入火焰"或烈火"定价计划.

3. You may need to upgrade your pricing plan

You need to be on the "Flame" or "Blaze" pricing plan.

事实上,免费的"Spark"计划仅允许对Google拥有的服务的出站网络请求".请参见 https://firebase.google.com/pricing/(将鼠标悬停在问号上位于云功能"标题之后)

As a matter of fact, the free "Spark" plan "allows outbound network requests only to Google-owned services". See https://firebase.google.com/pricing/ (hover your mouse on the question mark situated after the "Cloud Functions" title)

由于Feedly API不是Google拥有的服务,因此您 可能需要切换到火焰"或大火"计划.

Since the Feedly API is not a Google-owned service, you may need to switch to the "Flame" or "Blaze" plan.

这篇关于用于API调用的Google Cloud Functions Cron Job的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆