run background jobs with elastic beanstalk

  • Last Update :
  • Techknowledgy :
{
   "Version": "2012-10-17",
   "Statement": [{
      "Effect": "Allow",
      "Action": ["ses:SendEmail"],
      "Resource": ["*"]
   }, {
      "Effect": "Allow",
      "Action": ["sqs:*"],
      "Resource": ["*"]
   }, {
      "Effect": "Allow",
      "Action": ["cloudwatch:PutMetricData"],
      "Resource": ["*"]
   }]
}
{
   "name": "John Smith",
   "email": "john@example.com"
}

Suggestion : 2

Use Amazon CloudWatch for automatic scaling in worker environment tiers,You can manage a worker environment's configuration by editing the Worker category on the Configuration page in the environment management console.,Worker environments run a daemon process provided by Elastic Beanstalk. This daemon is updated regularly to add features and fix bugs. To get the latest version of the daemon, update to the latest platform version.,You can define periodic tasks in a file named cron.yaml in your source bundle to add jobs to your worker environment's queue automatically at a regular interval.

version: 1
cron:
   -name: "backup-job"
url: "/backup"
schedule: "0 */12 * * *" -
   name: "audit"
url: "/audit"
schedule: "0 23 * * *"

Suggestion : 3

Run Rails background jobs or periodic tasks (cron jobs) in Amazon Elastic Beanstalk worker environments. No need for customised container commands. , Run Rails background jobs or periodic tasks (cron jobs) in Amazon Elastic Beanstalk worker environments. No need for customised container commands. ,Elastic beanstalk worker environments support the execution of periodic tasks similar to cron jobs. We recommend you to make yourself familiar with Elastic Beanstalks' official doumentation first.,It makes your application ready for worker environments that are highly integrated in the Elastic Beanstalk landscape.

 gem 'active_elastic_job'
class YourJob < ActiveJob::Base
queue_as: name_of_your_queue
end
# config / application.rb
module YourApp
class Application < Rails::Application
config.action_mailer.deliver_later_queue_name =: name_of_your_queue
end
end
# config / application.rb
module YourApp
class Application < Rails::Application
config.active_job.queue_adapter =: active_elastic_job
end
end
version: 1
cron:
   -name: "PeriodicTaskJob"
url: "/periodic_tasks"
schedule: "0 23 * * *"
Rails.application.configure do
      config.active_elastic_job.process_jobs = ENV['PROCESS_ACTIVE_ELASTIC_JOBS'] == 'true'
   config.active_elastic_job.aws_credentials = lambda {
      Aws::InstanceProfileCredentials.new
   }
# allows lambdas
for lazy loading
config.active_elastic_job.aws_region # no
default
config.active_elastic_job.secret_key_base = Rails.application.secrets[: secret_key_base]
config.active_elastic_job.periodic_tasks_route = '/periodic_tasks'.freeze
end

Suggestion : 4

The PHP script which should be executed on the post deploy hook as a background task, never stops running. This effectively clogs the pipeline.,Since the script that is supposed to run in the background actually never finished, the health status of the application always remains at "info". This is a problem since it requires to be at "OK" for an application to be deployed successfully.,The post install hook in opt/elasticbeanstalk/hooks/appdeploy/post does run the PHP script after the deployment,It turns out that this is the wrong way to use elastic beanstalk. The app would be running, however it would be a hack rather than a correct solution.

Here is the code to the .ebextensions configuration file.

container_commands:
   01 - create_post_hook:
   command: "mkdir -p /opt/elasticbeanstalk/hooks/appdeploy/post"
ignoreErrors: true
files:
   "/opt/elasticbeanstalk/hooks/appdeploy/post/start.sh":
   mode: "000770"
owner: root
group: root
content: | #!/bin/bash
echo "--------Starting Runner------------"
php /
   var / app / current / runner.php &