Class: Schdlr::Queue::Sqlite

Inherits:
Object
  • Object
show all
Defined in:
lib/schdlr/queue/sqlite.rb

Constant Summary collapse

INITIAL_STATUS =
'queued'
FAILED_STATUS =
'failed'

Instance Method Summary collapse

Constructor Details

#initialize(name = 'test.db') ⇒ Sqlite

Returns a new instance of Sqlite.



10
11
12
13
14
15
16
17
18
19
# File 'lib/schdlr/queue/sqlite.rb', line 10

def initialize(name='test.db')
  @db = SQLite3::Database.new(name)
  @db.execute %(
    create table if not exists tasks (
      id integer PRIMARY KEY,
      scheduled_at integer,
      status varchar(30),
      name varchar(30)
    );)
end

Instance Method Details

#del(*keys) ⇒ Object



38
39
40
# File 'lib/schdlr/queue/sqlite.rb', line 38

def del(*keys)
  @db.execute("DELETE FROM tasks WHERE id IN (?)", keys)
end

#drop_all!Object



46
47
48
# File 'lib/schdlr/queue/sqlite.rb', line 46

def drop_all!
  @db.execute("DELETE FROM tasks")
end

#fail(*keys) ⇒ Object



42
43
44
# File 'lib/schdlr/queue/sqlite.rb', line 42

def fail(*keys)
  @db.execute("UPDATE tasks SET status = '#{FAILED_STATUS}' WHERE id IN (?)", keys)
end

#get(key) ⇒ Object



26
27
28
# File 'lib/schdlr/queue/sqlite.rb', line 26

def get(key)
  @db.execute("SELECT * FROM tasks WHERE id IN (?)", key)
end

#set(timestamp, task_spec) ⇒ Object



21
22
23
24
# File 'lib/schdlr/queue/sqlite.rb', line 21

def set(timestamp, task_spec)
  #TODO: should raise if timestamp is not a proper date
  @db.execute("INSERT INTO tasks (scheduled_at, name, status) VALUES (?, ?, ?)", [timestamp.to_i, task_spec.to_yaml, INITIAL_STATUS])
end

#tasks(at_time = nil) ⇒ Object



30
31
32
33
34
35
36
# File 'lib/schdlr/queue/sqlite.rb', line 30

def tasks(at_time=nil)
  if at_time
    @db.execute("SELECT * FROM tasks WHERE scheduled_at <= ? and status = ?", [at_time.to_i, INITIAL_STATUS])
  else
    @db.execute("SELECT * FROM tasks")
  end
end