Visualizzazione post con etichetta web app. Mostra tutti i post
Visualizzazione post con etichetta web app. Mostra tutti i post

venerdì 14 novembre 2014

Quickly add delayed job to your rails app

In some of my rails app I need to have delayed/background jobs as a consequence of a controller's action call

Below quick instructions on how to add delayed jobs to any rails app:


1- add the following to Gemfile

gem 'delayed_job'
gem 'delayed_job_active_record'
gem 'daemons'


2- execute bundle install

3- create the delayed jobs tables
rails generate delayed_job:active_record

4- migrate the DB
rake db:migrate

5- in your rails root create a directory called jobs

6- add the following lines in config/application.rb
config.autoload_paths += %W(#{config.root}/jobs) #autoload delayed_jobs job
Delayed::Worker.destroy_failed_jobs = false
Delayed::Worker.sleep_delay = 60
Delayed::Worker.read_ahead = 5 #default value

7- in the jobs directory creare delayed_task.rb
DelayedTask = Struct.new(:x, :y) do

def task_worker
    begin
        res = self.x + self.y
        Rails.logger.info "Task calculating x + y = " + res.to_s
   end
end

def perform
    task_worker
end
end

8- add somewhere in your controller

Delayed::Job.enqueue(DelayedTask.new(2,3), {:priority => 0, :run_at =>  Time.now + 10.seconds})
9- start the jobs worker
rake jobs:work
or
bin/delayed_job start
10- start your raisl app and when your controller method will be called a new delayed job (DelayedTask) will be schedule and execute at the proper time

martedì 26 agosto 2014

How to hot deploy a RedHat Openshift application

Some time when you make a change to your application (in my case is a rails application) you would like to deploy the code change to your running app without restarting the cartridge on Openshift.

By default every time you perform a git push to your Openshift application, the framework execute the entire stop, build, deploy and restart activities.
This might take quite some time and your online application will not be available for few minutes.

However if you want, you can instruct Openshift to Hot Deploy your application, which means git push your code changes without restarting the application.

This will definitely reduce the downtime of your application (in most of the cases only the web server will be restarted and not the entire application cartridge like Ruby or PHP).

Enabling Hot Deploy is easy.
Add an empty hot_deploy file in the .openshift/markers directory.
Deploy this change and Openshift will understand that it should not restart the entire application when you git push your code changes


# touch .openshift/markers/hot_deploy
# git add .
# git commit -m "implementing hot deploy"
# git push


If everything is correct, next time you will push code changes to your Openshift apps, you should see something similar

remote: Not stopping cartridge ruby because hot deploy is enabled

Now your code change will be online in a quite short amount of time and especially with a very limited app downtime.