Skip to content

Commit

Permalink
database: Initial setup for storing jobs
Browse files Browse the repository at this point in the history
  • Loading branch information
jonnor committed Apr 5, 2017
1 parent 6d775de commit 9950bd3
Show file tree
Hide file tree
Showing 7 changed files with 86 additions and 2 deletions.
1 change: 1 addition & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -13,3 +13,4 @@ env:
- DATABASE_URL=postgres://postgres:@localhost/imageresize_test
before_script:
- psql -c 'create database imageresize_test;' -U postgres
- ./node_modules/.bin/knex migrate:latest
2 changes: 2 additions & 0 deletions config.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -8,3 +8,5 @@ module.exports =
resize:
timeout: parseInt envvar('IMAGERESIZE_TIMEOUT', 10) # seconds before aborting
concurrent: parseInt envvar('IMAGERESIZE_CONCURRENT', 5) # number of concurrent jobs
database:
url: envvar('DATABASE_URL', 'postgres://postgres:@localhost/imageresize_test')
28 changes: 28 additions & 0 deletions db.coffee
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
Knex = require 'knex'
config = require './config'
url = require 'url'

# Parse DB URL
dbConfig = url.parse config.database.url
connection =
charset: 'utf8'

# Normalize config
switch dbConfig.protocol
when 'postgres:'
provider = 'pg'
[user, pass] = dbConfig.auth.split ':'
connection.host = dbConfig.hostname
connection.user = user
connection.password = pass
connection.database = dbConfig.path.substr 1
connection.port = dbConfig.port

module.exports = Knex
client: provider
connection: connection
pool:
min: 1
max: 4
useNullAsDefault: true
# debug: true
25 changes: 25 additions & 0 deletions knexfile.coffee
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
config = require './config'
url = require 'url'

dbConfig = url.parse config.database.url
[user, pass] = dbConfig.auth.split ':'
switch dbConfig.protocol
when 'postgres:'
cfg =
client: 'postgresql'
connection:
host: dbConfig.hostname
port: dbConfig.port
database: dbConfig.path.substr 1
user: user
password: pass
pool:
min: 2
max: 10
migrations:
tableName: 'knex_migrations'

module.exports =
development: cfg
staging: cfg
production: cfg
19 changes: 19 additions & 0 deletions migrations/20170405132136_jobs.coffee
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
exports.up = (knex, Promise) ->
knex.schema.createTable 'jobs', (t) ->
# Job UUID
t.uuid('id').primary()

# Timestamp info
t.timestamps()
# When whole job completed
t.timestamp('completed_at').index()
# When whole job failed
t.timestamp('failed_at').index()

# Data associated with this job
# XXX: a bit quick & dirty
# Normally one would have a separate table for the images, with a foreign key referring the job
t.jsonb('data')

exports.down = (knex, Promise) ->
knex.schema.dropTableIfExists 'jobs'
2 changes: 2 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,10 @@
"cheerio": "^0.22.0",
"coffee-script": "^1.12.4",
"express": "^4.15.2",
"knex": "^0.12.9",
"msgflo": "^0.10.7",
"msgflo-nodejs": "^0.10.1",
"pg": "^6.1.5",
"request": "^2.81.0",
"request-promise": "^4.2.0",
"sharp": "^0.17.3",
Expand Down
11 changes: 9 additions & 2 deletions src/app.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,8 @@ routes.resizeImages = (req, res, next) ->
# so they can be processed independently by the worker
images = req.body.images.map (i) ->
i.id = uuid.v4()
return i

messages = images.map (i) ->
m =
job: jobId
Expand All @@ -60,10 +62,15 @@ routes.resizeImages = (req, res, next) ->

# Store images with ID on as part of job,
# so we can correlate with id from worker
imageMap = {}
images.map (i) ->
imageMap[i.id] = i

job =
id: jobId
images: images
# FIXME: persist job info to database
data:
images: imageMap
created_at: new Date()

return res.location("/job/#{jobId}").status(202).end()

Expand Down

0 comments on commit 9950bd3

Please sign in to comment.