axios超过延迟

    科技2025-03-03  28

    axios超过延迟

    问题(Problem)

    Building a task scheduler is a common problem. Some use cases from my work include:

    构建任务计划程序是一个常见问题。 我工作中的一些用例包括:

    Notifications - Scheduling emails, push, and SMS notifications to send at particular times.

    通知-安排在特定时间发送的电子邮件,推送和SMS通知。 E-Commerce Price Changes - Scheduling price changes to happen at a particular date for a flash sale.

    电子商务价格更改-安排价格更改在特定日期进行一次快速销售。 Retries - If a job fails, you may need to schedule a retry for later.

    重试-如果作业失败,则可能需要安排重试以备后用。

    Typically, engineers implement task processing using a message queue — with Amazon SQS as the most popular option. However, if you want to delay posting a message to simulate a scheduled task, Amazon only offers support up to a 15 minute delay!

    通常,工程师使用消息队列来实现任务处理-Amazon SQS是最受欢迎的选项。 但是,如果您想延迟发布消息以模拟预定任务,那么Amazon仅提供最多15分钟的支持!

    Anything beyond 15 minutes requires you to build your own service to handle 15分钟以外的任何时间都需要您构建自己的服务来处理

    This means SQS works well for tasks that need immediate processing, but anything beyond that requires you to build your own service to handle the delays.

    这意味着SQS可以很好地处理需要立即处理的任务,但除此之外,您需要构建自己的服务来处理延迟。

    When we researched potential solutions, no options worked out of the box, with long delays, good documentation, and production level SLA’s.

    当我们研究潜在的解决方案时,没有任何选择是可行的,而且会出现长时间的延迟,良好的文档记录以及生产级别的SLA。

    潜在解决方案 (Potential Solutions)

    Some alternatives we looked at, but eventually abandoned include:

    我们研究了但最终放弃的一些替代方案包括:

    DynamoDB TTL

    DynamoDB TTL

    Description: This solution involves storing queue messages in a DynamoDB table with a TTL set on each entry. Then when the item expires, a lambda function puts the scheduled message on a queue.

    说明:此解决方案涉及将队列消息存储在DynamoDB表中,并在每个条目上设置TTL。 然后,当项目过期时,lambda函数会将计划的消息放入队列中。

    Problem: According to the official documentation, the lambda functions are triggered up to 48 hours after expiration. This margin of error is too large.

    问题:根据官方文档,lambda函数在到期后最多48小时被触发。 此误差幅度太大。

    2. AWS Step Functions

    2. AWS步骤功能

    Description: Step functions are state machines with a visual workflow. You can use their “Wait” state which allows you to delay publishing a message to a queue for a set amount of time.

    说明:步骤功能是具有可视工作流程的状态机。 您可以使用它们的“等待”状态,使您可以将消息延迟发布到队列一段时间。

    Using the Wait Step in Step Functions 在逐步功能中使用等待步骤

    Problems:

    问题:

    Scaling limits. Step functions have a max limit of 1 million waiting tasks.

    缩放限制。 步进功能的最大限制为一百万个等待任务。 Cost. A simple state machine for a scheduled task requires 3 state transitions. So a million scheduled tasks would cost $75 with step functions, which doesn’t include the cost of Lambda or other peripheral services like CloudWatch logs.

    成本。 用于计划任务的简单状态机需要3个状态转换。 因此,一百万个计划任务的步进功能成本为75美元,其中不包括Lambda或其他外围服务(如CloudWatch日志)的成本。 Testing. Overall development cycle requires 30 seconds to package, upload, and deploy a new stack. This is followed by a number of clicks on the UI to find and debug your latest execution. Also many issues you face are in setting configuration settings of the state machine, which can’t be executed locally.

    测试。 整个开发周期需要30秒才能打包,上载和部署新堆栈。 接下来,在UI上单击几次以查找和调试最新执行。 您还面临着许多问题,这些问题在于状态机的配置设置,这些设置无法在本地执行。

    3. Cron Polling

    3. Cron轮询

    Description: Create a new cron job for every task you want to delay.

    说明:为要延迟的每个任务创建一个新的cron作业。

    Problems:

    问题:

    Scalability: Cron works well if it’s just a few scripts you need to automate. But if you’re dealing with millions of requests, all with different scheduled times, you can’t spin up millions of Cron jobs.

    可扩展性:如果仅需要少量脚本来自动化,Cron就会很好地工作。 但是,如果您要处理数百万个具有不同计划时间的请求,则无法启动数百万个Cron作业。 Integration: Most tasks take the form of messages on a queue like Amazon SQS or Kafka. Cron doesn’t integrate with message queues. Furthermore, cron jobs aren’t really a service, and don’t fit well within a microservice architecture.

    集成:大多数任务都采用队列中消息的形式,例如Amazon SQS或Kafka。 Cron不与消息队列集成。 此外,cron作业并不是真正的服务,也不适合微服务架构。

    4. Open Source

    4.开源

    There are a number of open source task schedulers. Some examples include Celery, Quartz, and BigBen.

    有许多开源任务计划程序。 一些示例包括Celery , Quartz和BigBen 。

    The two most common open source alternatives: celery and quartz 两种最常见的开源替代品:芹菜和石英

    Problems:

    问题:

    Difficult integration. These solutions require you to spin up your own servers that run the open source. We want something that works out-of-the-box — no cryptic errors, no servers to manage.

    整合困难。 这些解决方案要求您启动运行开源的自己的服务器。 我们需要开箱即用的东西-没有隐秘的错误,没有要管理的服务器。 Lack of support. Documentation can be spotty and outdated. If you get anything wrong in your configuration settings, there could be little to no feedback as to what the problem could be. When we posted an issue on Github about an error we had spent weeks debugging, no one could help.

    缺乏支持。 文档可能不完整且过时。 如果您在配置设置中遇到任何问题,那么对于可能出现的问题可能几乎没有反馈。 当我们在Github上发布有关错误的问题时,我们花了数周的时间进行调试,没有人能提供帮助。

    介绍Scheduler API (Introducing Scheduler API)

    Dissatisfied with the alternatives above, we set out to build a cloud task scheduler. This solution is the solution we wished we had — Scheduler API. Scheduler API is a message queue scheduler. It consists of four API’s:

    对上述替代方案不满意,我们开始构建云任务计划程序。 该解决方案是我们希望拥有的解决方案-Scheduler API。 计划程序API是消息队列计划程序。 它包含四个API:

    schedule() — schedules the queue message (task)

    schedule() —计划队列消息(任务)

    cancel() — cancels a previously scheduled queue message

    cancel() —取消先前计划的队列消息

    update() — changes the scheduled time of a previously scheduled queue message

    update() —更改先前计划的队列消息的计划时间

    status() — checks the status of a scheduled message

    status() —检查预定消息的状态

    We aimed to solve a couple problems:

    我们旨在解决一些问题:

    Serverless — We didn’t want to manage our own brokers or do our own scheduler deployments. We wanted a clean API that just worked. Make the call, and don’t worry about the rest.

    无服务器-我们不想管理自己的代理或自己进行调度程序部署。 我们想要一个干净的API可以正常工作。 拨打电话,别担心。 High Precision — Messages have to be delivered with precision. The 48 hour margin of error with DynamoDB is too much.

    高精度-消息必须精确传递。 DynamoDB的48小时错误余量太大。 Production Level SLA’s — The API should have no scalability limits. It should handle high throughput traffic with no additional work.

    生产级SLA-API应该没有可扩展性限制。 它应该处理高吞吐量流量,而无需其他工作。 Fast integration — Getting the first task scheduled should not take weeks of endless configuration and debugging. The API should “just work” out of the box.

    快速集成-安排第一个任务的安排无需花费数周的无休止的配置和调试。 API应该开箱即用。 High level of support — We are engineers too. We get the frustration of integrating software with poor documentation. We aimed to make sure that developers always get the support they need — well-maintained API docs and fast response times to inquiries.

    高水平的支持-我们也是工程师。 我们对于将软件与不良文档集成在一起感到沮丧。 我们旨在确保开发人员始终获得所需的支持-维护良好的API文档和对查询的快速响应时间。

    Scheduler API入门 (Getting Started with Scheduler API)

    We’ve bundled the API in an easy to use SDK. In this section we will show you how to get a test call up and running by delaying tasks to Amazon SQS in Node. Using the API involves 3 steps:

    我们将API捆绑在一个易于使用的SDK中。 在本节中,我们将向您展示如何通过延迟到Node中的Amazon SQS的任务来启动和运行测试呼叫。 使用API​​涉及3个步骤:

    Create a Scheduler API account

    创建一个Scheduler API帐户 Grant permissions

    授予权限Import the SDK and test the call

    导入SDK并测试通话

    1) Creating a Scheduler API Account

    1)创建一个Scheduler API帐户

    First you have to create a Scheduler API account to get API keys. You can do this by signing up on the main website: www.schedulerapi.com and clicking “Sign Up”.

    首先,您必须创建一个Scheduler API帐户来获取API密钥。 您可以通过在主要网站www.schedulerapi.com上进行注册并单击“注册”来完成此操作。

    Homepage for Scheduler API Scheduler API主页

    Or you can go straight to account creation here: app.schedulerapi.com:

    或者,您可以直接在此处创建帐户: app.schedulerapi.com :

    Signup Page for Scheduler API Scheduler API的注册页面

    Once your account is created, the admin console will have a button to create your first API key. Click the button to create the API key, as you will need this in the test call later.

    创建帐户后,管理控制台将具有一个用于创建您的第一个API密钥的按钮。 单击按钮创建API密钥,因为稍后在测试调用中将需要此密钥。

    2) Granting Permissions

    2)授予权限

    The next step is to create an IAM role to represent Scheduler API with “write” permissions to the queue you want to schedule messages to. This is necessary or else the API can’t publish your scheduled messages. We walk you through this in our blog post here.

    下一步是创建一个IAM角色,以表示“计划程序” API,该程序对要向其计划消息的队列具有“写”权限。 这是必要的,否则API无法发布您计划的消息。 我们将在此处的博客文章中引导您完成此过程。

    3) Importing the Scheduler SDK

    3)导入Scheduler SDK

    Now that we’ve gotten the configuration finished, we can get coding!

    现在我们已经完成了配置,我们可以开始编码了!

    The recommended way to install the Scheduler API SDK is through npm or Yarn.

    建议使用npm或Yarn安装Scheduler API SDK。

    npm:

    npm:

    npm install schedulerapi-js

    yarn:

    纱:

    yarn add schedulerapi-js

    At this point, you can make a call by adding this line of code to where you want the task scheduled:

    此时,您可以通过将以下代码行添加到计划任务的位置来进行调用:

    const s = new Scheduler({ key: SCHEDULER_API_KEY });const results = await s.scheduleSqs({when: new Date('2020-08-24 20:13:00'),url: YOUR_SQS_QUEUE_URL,body: ‘THE_BODY_OF_YOUR_SQS_MESSAGE’});console.log(results);

    A sample response looks like this:

    样本响应如下所示:

    {“id”: “cLzxqmLKAEc2Tf2YzKRZW”,“when”: “2020–08–24 20:13:00”,“now”: “2020–08–24 20:11:35”,“user”: “CkM2xwzjvxjGhWeiMFWy9s”}

    The calls to update(), cancel(), and status() are similar and documented in the API docs below.

    对update(),cancel()和status()的调用与此类似,并记录在下面的API文档中。

    Documentation

    文献资料

    You can see a full working example that you can pull in and test at this github repo here — https://github.com/schedulerapi/schedulerapi-cra-typescript-example

    您可以在此处查看完整的工作示例,并可以在此github存储库中进行测试— https://github.com/schedulerapi/schedulerapi-cra-typescript-example

    You can also see the full API docs and NPM and PHP packages here:

    您还可以在此处查看完整的API文档以及NPM和PHP软件包:

    NPM package — https://www.npmjs.com/package/schedulerapi-js

    NPM软件包-https: //www.npmjs.com/package/schedulerapi-js

    PHP SDK — https://packagist.org/packages/schedulerapi/schedulerapi-php

    PHP SDK- https://packagist.org/packages/schedulerapi/schedulerapi-php

    API Documentation — https://apidocs.schedulerapi.com/#schedulerapi

    API文档-https: //apidocs.schedulerapi.com/#schedulerapi

    What’s next?

    下一步是什么?

    Our next areas of focus involve:

    我们接下来的重点领域包括:

    Building a Scheduler API SDK for languages beyond just Javascript and PHP.

    为不只是Javascript和PHP的语言构建Scheduler API SDK。 Supporting other messages queues beyond Amazon SQS (Kafka, ActiveMQ, Rabbit MQ, and others).

    支持Amazon SQS以外的其他消息队列(Kafka,ActiveMQ,Rabbit MQ等)。

    Contact

    联系

    If you need Scheduler API for any other use cases, we’d love to hear about it!

    如果您在其他任何用例中都需要Scheduler API,我们很乐意听到!

    Follow us on Twitter: https://twitter.com/SchedulerAPI

    在Twitter上关注我们: https : //twitter.com/SchedulerAPI

    Or contact us at info@schedulerapi.com for questions and requests.

    或通过info@schedulerapi.com与我们联系以提出问题和要求。

    Feel free to leave comments below — we’d love to hear what you think. We guarantee a response to all inquiries within 24 hours. Happy hacking!

    随时在下面留下评论-我们很想听听您的想法。 我们保证在24小时内回复所有询问。 骇客骇客!

    翻译自: https://medium.com/swlh/how-to-get-past-the-15-minute-delay-limit-in-amazon-sqs-fba3c50daf0b

    axios超过延迟

    相关资源:微信小程序源码-合集6.rar
    Processed: 0.011, SQL: 8