1000 多个 API 调用与 1 个 cron 作业? [英] 1000+ API calls with 1 cron job?

查看:19
本文介绍了1000 多个 API 调用与 1 个 cron 作业?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们有以下潜在情况:

一个可以拥有 1000 多个用户的网络应用.

A web app that could have 1000+ Users.

我们想设置一个 cronjob,通过一个外部服务 API 为所有 1000 多个用户使用一个 cronjob 调用获取数据(注意:每个用户都有自己的 API 凭据,使用该外部 API 服务)!

We want to setup a cronjob to fetch data via an external-service API for ALL 1000+ users with one cronjob call (NOTE: each user has their own API credentials with that external API service)!

什么是明智的做法?

信息:

  • 使用凭据调用一次 API 最多可能需要 5(!) 秒才能取回数据.

可能的脚本:

Cronjob 调用本地 php 脚本 (cronjobcall.php),循环遍历所有 1000 个用户.对于每个用户,此脚本通过 curl (localfile_calls_api.php) 调用另一个本地脚本,该脚本进行实际的 API 调用并将返回的数据保存到 MySQL 数据库中.

Cronjob calls a local php script (cronjobcall.php), that loops through all 1000 users. For each user this script calls another local script via curl (localfile_calls_api.php) that makes the actual API call and saves the returned data into MySQL database.

cronjobcall.php

cronjobcall.php

foreach($ThousandsOfUsers as $UserId => $UserCredentials)
{

$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "localfile_calls_api.php?UserId=$UserId&UserCredentials=$UserCredentials");
curl_setopt($ch, CURLOPT_HEADER, 0);
$result=curl_exec($ch);

}

localfile_calls_api.php

localfile_calls_api.php

// !!! this could take up to 5(!) seconds to return result

$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "http://external_api_url.php?UserId=$UserId&UserCredentials=$UserCredentials");
curl_setopt($ch, CURLOPT_HEADER, 0);
$result=curl_exec($ch);

if($result)
{
save_to_MySql($result,$UserId);
}

所以,这就是为什么我想到将整个过程拆分为两个不同的 php 文件的原因,因为 API 调用本身可能需要长达 5 秒才能返回数据.

So, that's why I think of splitting the whole procedure into two different php files, because the API call itself could take up to 5 seconds to return data.

这是正确的方法吗?

有更好的方法吗?

非常感谢!

推荐答案

如果您确实需要定期为每个用户进行 API 调用,我会进行不同的设置:

If you really need to make that API call for each user periodically, I would set it up differently:

  • 向表中添加两列:lastUpdatedisBeingProcessed(或类似内容);
  • 使用 cron 制作一个每 X (1?) 分钟运行一次的脚本;
  • 在您的脚本中,获取 lastUpdated 日期最早且未被处理的 XX (10?) 条记录,并设置 isBeingProcessed 标志;
  • 在每个 API 调用完成后,更新用户信息,包括 lastUpdated 日期或时间,并取消设置 isBeingProcessed 标志;
  • Add two columns to your table: lastUpdated and isBeingProcessed (or something similar);
  • Make a script that runs every X (1?) minutes using cron;
  • In your script, get the XX (10?) records with the oldest lastUpdated date and that not being processed and set the isBeingProcessed flag;
  • As each API call finishes, update the user information including the lastUpdated date or time an unset the isBeingProcessed flag;

根据您的服务器可以处理的内容以及 API 允许的内容,您甚至可以将其设置为同时/重叠运行多个作业,从而减少大量更新的总时间.

Depending on what your server can handle and what the API allows, you can even set it up to have multiple jobs run simultaneously / overlapping, reducing the total time to update a lot.

这篇关于1000 多个 API 调用与 1 个 cron 作业?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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