无服务器Cron Job触发两次 [英] Serverless Cron Job firing twice

查看:89
本文介绍了无服务器Cron Job触发两次的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我不确定为什么,但是我的Webhook在我的Cron工作中被解雇了两次。因此,该cron作业应该每15分钟运行一次,但它会触发两次。我将发布日志,处理程序和yml文件来提供帮助。

I am not sure why but my webhook is being fired twice in my cron job. So this cron job is suppose to run once every 15 min which it does, but it is firing off twice. I will post the logs, handler and yml file to help out.

基本上,我的cron作业会向salsify api发出一些请求,以将URL存储在mongodb中。一旦该文件完成并在下次运行cron作业时生成,它将触发nethify的webhook。然后,该过程重新开始。

Basically my cron job will make some request to a salsify api to store a url inside a mongodb. Once that file has been completed and built the next time the cron job runs it should trigger the webhook for netlify. Then the process starts all over again.

在我的netlify中,我注意到该构建已运行了两次,并且将源指向了无服务器的cron作业。

In my netlify I noticed the build was being ran twice and have pin pointed the source to the serverless cron job.

编辑:我应该在此处添加的内容是,即使我的cron作业运行两次,它仍然应该仅在技术上调用webhook一次,如果MongoDB中有一个文件。但是它仍然以某种方式调用它两次,这导致我的netlify构建失败,因为它需要该文件才能构建。

Something I should add in here is that even if my cron job runs twice it still should still only technically call the webhook once if there is a file in the MongoDB. Yet it is still calling it twice somehow which is causing my netlify build to fail because it needs that file in order to build.

serverless.yml的功能部分:

function part of serverless.yml:

functions:
  salsifyCron:
    handler: src/handler.salsifyCron
    events:
      - schedule: 
          rate: cron(*/15 * * * ? *)
          enabled: true

日志:

2018-05-17 10:00:41.121 (-05:00)    10d87735-59e3-11e8-be56-69e06899fa1f    Trigger Webhook
2018-05-17 10:01:45.941 (-05:00)    10d87735-59e3-11e8-be56-69e06899fa1f    Trigger Webhook

处理程序:

require('envdotjs').load();
import fetch from 'isomorphic-fetch';
import axios from 'axios';
import middy from 'middy';
import { jsonBodyParser, httpErrorHandler, cors } from 'middy/middlewares';

import { connectToDatabase } from '../utils/db';
import Sheet from '../models/Sheet';
import config from '../utils/config';

module.exports.salsifyCron = middy(async (event, context, callback) => {
  context.callbackWaitsForEmptyEventLoop = false;
  let sheetId;
  const options = {
    url: `https://app.salsify.com/api/orgs/${
      process.env.SALSIFY_ORG_ID
    }/export_runs`,
    headers: {
      Authorization: `Bearer ${process.env.SALSIFY_API_KEY}`,
      'Content-Type': 'application/json'
    }
  };
  await connectToDatabase();

  const storedData = await Sheet.find({});
  if (
    storedData.length > 0 &&
    storedData[0] &&
    storedData[0].status === 'completed' &&
    storedData[0].url !== null
  ) {
    console.log('Trigger WebHook');
    axios.post('https://api.netlify.com/build_hooks/*****************');
    process.exit(0);
    return;
  }
  if (storedData[0]) {
    sheetId = storedData[0].sheetId;
  }
  if (storedData.length === 0) {
    const res = await fetch(options.url, {
      method: 'POST',
      headers: options.headers,
      body: JSON.stringify(config)
    }).then(res => res.json());
    if (res.id && res.status) {
      await Sheet.create({
        sheetId: res.id,
        url: null,
        status: res.status
      });
      sheetId = res.id;
    } else {
      console.log(res);
      process.exit(1);
    }
  }

  const resWithId = await fetch(`${options.url}/${sheetId}`, {
    method: 'GET',
    headers: options.headers
  }).then(res => res.json());

  if (resWithId.status === 'running') {
    console.log('running cron job');
    console.log(resWithId.estimated_time_remaining);
  }
  if (resWithId.status === 'completed') {
    console.log('completed cron job');
    await Sheet.findByIdAndUpdate(
      storedData[0],
      { status: resWithId.status, url: resWithId.url },
      { new: true }
    );
  }
})
  .use(cors())
  .use(jsonBodyParser())
  .use(httpErrorHandler());


推荐答案

Lambda超时。在您的情况下,这可能不是问题,但这是导致此结果的常见问题。

Lambda timeout. This might not have been the problem in your case, but it is a common problem that causes this result.

您的lambda不会同时执行,但有一点延迟。

Your lambdas are not getting executed simultaneously but with a bit of a delay. This is a clue that it is not just getting a duplicate execution.

我想您的lambda首先会以错误终止(例如超时-默认lambda),这是一个线索。

I would guess that your lambda is first terminating with an error (for example timing out - the default lambda timeout is quite small) and the lambda is being rerun after it fails.

我在超时方面遇到了这个问题,如果您没有注意到lambda已超时。

I have had this problem with timeouts and it is quite confusing if you don't notice that the lambda has timed out.

这篇关于无服务器Cron Job触发两次的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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