【问题标题】:Google Cloud Functions Cron Job for API Call用于 API 调用的 Google Cloud Functions Cron 作业
【发布时间】:2020-04-08 07:14:03
【问题描述】:

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

但是,它不起作用,我不确定为什么。代码如下:

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 || '',
        })
      })
    })
  })

知道我需要做什么才能让它工作吗?

【问题讨论】:

  • 你可以查看这个答案stackoverflow.com/questions/35737708/…
  • 什么不起作用?你试过什么?什么错误(如果有的话)?什么症状?
  • 下次请不要重新发布相同的question,而是编辑您的原始问题(其下方有一个链接)以包含其他信息。
  • @FrankvanPuffelen 我没有转发同样的问题。这个问题是问为什么我不能使用firebase serve 命令运行计划的云功能。另一个问题是询问如何使用 api 调用运行计划的云功能。我仍然想要我的另一个问题的答案(即使这个问题得到了正确回答)。
  • 糟糕,好点子。我想我可能两次错误地点击了你的同一个问题。对于那个很抱歉。我重新打开了your other question,并且可能会回答你正在尝试做的事情很遗憾(还)不可能。

标签: firebase google-cloud-firestore google-cloud-functions google-cloud-pubsub


【解决方案1】:

您的云函数可能存在三个问题:

1。您正在将调度函数的代码与 HTTPS 函数之一混合

查看Schedule FunctionsHTTPS Functions 的文档。在 Schedule Function 中,您不应该使用 app.get() 而只是这样做,例如:

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。

因此,在您的情况下,您需要按如下方式使用Promise.all(),因为您正在并行执行多个(异步)写入:

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。您可能需要升级定价计划

您需要使用“Flame”或“Blaze”定价计划。

事实上,免费的“Spark”计划“只允许向 Google 拥有的服务发出出站网络请求”。请参阅https://firebase.google.com/pricing/(将鼠标悬停在“云功能”标题后面的问号上)

由于 Feedly API 不是 Google 拥有的服务,您可能需要切换到“Flame”或“Blaze”计划。

【讨论】:

  • 谢谢 - 这是一个很好的答案(非常详细且切中要害)。非常感激。只是为了记录,我已经注册了 Blaze 定价计划。我的问题只是关于如何将预定的云功能与外部 api 调用相结合。再次,非常感谢。
猜你喜欢
  • 2020-04-08
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-12-03
  • 2021-07-17
  • 2020-03-15
  • 2020-09-03
  • 1970-01-01
相关资源
最近更新 更多