简体   繁体   中英

How do you use cron jobs using Elastic Beanstalk and Java?

I want to run cron jobs and use the same code base. I found a few solutions, but they don't appear ideal. For example, with Heroku, you can add a Scheduler element and fill in the commands to run in a web page.

  • http://blog.rotaready.com/scheduled-tasks-elastic-beanstalk-cron/
    • It seems overly complicated for load-balanced instances.
    • It makes use of require('async') in Node, but what would be a Java Spring Boot equivalent?
  • https://docs.aws.amazon.com/elasticbeanstalk/latest/dg/using-features-managing-env-tiers.html
    • There doesn't appear to be any security. Any one the net could access the /path to POST and execute the job, causing a denial-of-service attack.
    • it mentions cron.yaml which doesn't make sense as the app is deployed via a WAR/ZIP file to a Tomcat instance (Spring Boot).
    • It mentions Amazon DynamoDB, which we don't use. We use MySQL.
    • It doesn't specify whether the load balancer connection draining timeout is in effect for these jobs (10s).
    • It mentions "Worker Configuration card on the Configuration page in the environment management console" but there is no Worker Configuration card under Configuration page.
  • Running a cron job in Elastic Beanstalk
    • For Python/Django - uses cron.yaml .
  • I thought of just having a dedicated EC2 instance, but how can I deploy the latest code changes there?

This may also belong on SoftwareEngineering.StackExchange.

There is an easy way to do this using other AWS systems.

You can use CloudWatch to set scheduled events ( https://docs.aws.amazon.com/AmazonCloudWatch/latest/events/WhatIsCloudWatchEvents.html ). You can set a rule to set the event on a set schedule.

You then have at least two options:

  • set the event to publish an SNS message and use that SNS to call a web hook on your server. Many examples on how to do this but you will have to make sure you check the signature to ensure the web API is called from the signed SNS. But this would use a public API and may not be something you are comfortable with.

  • set the event to publish an SQS message. Then set an elastic beanstalk worker to process the SQS message or just run a background script on your main server, which is basically on an infinite loop polling SQS for work to do.

Not sure how familiar you are with these systems so not sure if it will be clear what I am talking about, but there is no way to give a detail solution so hope this is enough to give you ideas.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM