-
-
Save dfockler/9a8c252eac69cc7b4f82 to your computer and use it in GitHub Desktop.
Database Shared Connection
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require 'securerandom' | |
require 'lib/job' | |
class Batch | |
attr_reader :id, :db | |
attr_accessor :jobs | |
# pass in database connection | |
def initialize(jobs, db) | |
@id = SecureRandom.uuid | |
@jobs = jobs | |
@db = db | |
end | |
def run | |
@jobs.each do |job| | |
job.run | |
end | |
end | |
def save | |
@db['batches'].insert(:id => @id) | |
end | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require 'securerandom' | |
class Job | |
attr_reader :id, :url, :db | |
# passing in database connection | |
def initialize(url, db) | |
@url = url | |
@id = SecureRandom.uuid | |
@db = db | |
end | |
def run | |
results = # Make HTTP request | |
save(results) | |
end | |
def save | |
@db['jobs'].insert(:id => @id, :results => results) | |
end | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require 'sequel' | |
require 'lib/job' | |
require 'lib/batch' | |
db = Sequel.connect('my sequel connection') | |
job1 = Job.new('test.com', db) | |
job2 = Job.new('test1.com', db) | |
jobs = [job1, job2] | |
batch = Batch.new(jobs, db) | |
batch.run |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment