Entries Tagged 'ruby' ↓
November 18th, 2008 — open source, plugins, rails, ruby
backup_fu is a nice Rails plugin to backup all your data to an S3 account. Sadly, it currently doesn’t work on windows.
Two bugs prevent it to run smoothly, first it depends on tar and gzip commands, which are not always available on that platform. Even though there are some native implementations, I’ve never found a tar one that I could actually use, most of them just fail at archiving big files. I’ve fixed this using the minitar gem to generate a tar.gz file from the sql dump.
On the other hand, there is an aws-s3 bug that prevents files to be directly passed to the store() method to upload the files. Fixing this was just a matter to read the file using ‘rb’ parameters for File.open().
You can see this and this commit with the changes, a pull request has already been sent.
November 13th, 2008 — open source, plugins, rails, ruby
The need for executing long task process is a common thing in large-scale rails projects these days. This is something any serious developer should be aware of. Make no mistake, long tasks can consume large amounts of servers resources, and they can provoke servers to crash when you request them via http.
A great solution for this problem is the delayed_job plugin by Tobias Lütke. This plugin is very easy to install, is very well documented, and provides you with everything you’ll need to execute task in background. I had the oportunity to try it out on burdastyle.com, and it worked just fine. So, congratulations Tobi for this great work and thanks for sharing.
So, let’s try delayed_job plugin out.
First thing we need to do after we install this plugin is to generate a migration to add delayed_jobs table into our database. This migration should look like this:
class CreateDelayedJobs < ActiveRecord::Migration
def self.up
create_table :delayed_jobs, :force => true do |table|
table.integer :priority, :default => 0
table.integer :attempts, :default => 0
table.text :handler
table.string :last_error
table.datetime :run_at
table.datetime :locked_at
table.datetime :failed_at
table.string :locked_by
table.timestamps
end
end
def self.down
end
end
Then, we’ll need to define our jobs. A job is just a ruby class with a method called perform. Let’s say we need to send a very simple email notification to our entire users database. We can use this little class:
class NotificationJob < Struct.new(:message)
def perform
total = User.count
limit = 100
(0..total-1).step(limit) do |offset|
User.find(:all, :limit => limit, :offset => offset).each do |u|
Notifier.deliver_message_to_user(u, message)
end
end
end
end
Now, every time you need to send these notifications out, you just enqueue your job like this:
Delayed::Job.enqueue NotificationJob.new("hello world!")
Finally, delayed_job plugin provides you with a set of rake tasks to put enqueue jobs to work:
rake jobs:work
And that’s all folks! Have fun with delayed_job.
Happy hacking!
September 7th, 2008 — open source, ruby, rweather
Well, I have to say that the main motivation to write this gem was just to write a gem. I’ve never done it before. That being said, here’s rweather with you.
What does it do ?
It’s a ruby wrapper for the Weather XML Data Feed from weather.com.
It currently has two features, searching locations to get their location_id and getting the weather current conditions for a given location.
Getting the code and Installing
The rweather code is of course available at github and is released with a MIT license.
To install it just do the usual:
sudo gem install ckozus-rweather --source http://gems.github.com
How to Use It
This is a simple script using rweather taken from the README. It gets a list of matching locations and shows the current temperature and what it feels like.