Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

create a basic redis transport

  • Loading branch information...
commit 19ea0d5294f2b43c553f735347f323bdf805897f 1 parent 260b2b4
@jro authored
Showing with 50 additions and 0 deletions.
  1. +50 −0 lib/transports/redis_transport.rb
View
50 lib/transports/redis_transport.rb
@@ -0,0 +1,50 @@
+require 'redis'
+require 'json'
+
+module Flapjack
+
+ class RedisTransport
+ attr :connection
+
+ def initialize(opts={})
+ @host = opts[:host] ||= 'localhost'
+ @port = opts[:port] ||= 6379
+ @environment = opts[:environment] ||= 'production'
+
+ @log = opts[:logger] ||= Merb.logger
+
+ setup_connection
+ end
+
+ def setup_connection
+ @log.info("Using redis at #{@host}:#{@port}")
+ @connection = Redis.new(:host => @host, :port => @port)
+ end
+
+ def insert_job(opts={})
+ begin
+ raise ArgumentError unless (opts[:command] && opts[:id])
+ opts[:frequency] ||= 30
+
+ job = opts.to_json
+
+ unless @connection.lpush('flapjack_jobs',job) == "OK"
+ raise FlapjackRedisInsertError
+ end
+
+ #rescue
+ # FIXME: add resonable rescue for redis connections once we
+ # figure out what the stack trace looks like
+ end
+ end
+
+ def self.clear
+ # Doing this as a transaction as I'm assuming there
+ # will be a queue for in progress jobs we'll also need to clear
+ # in order to make sure they don't get re-added
+ t = self.new
+ t.connection.del 'flapjack_jobs'
+ end
+
+ end
+end
Please sign in to comment.
Something went wrong with that request. Please try again.