- Break down the system into individual components
- Each component should work on their own, i.e. de-coupled and reusable
- Unit test on each component
- Glue the tested components into a worker flow
- Worker should be scalable, i.e. able to run single/multiple instances in single/multiple containers
Job Producer
- Put jobs to beanstalkd/tube, with payload and delayJob Consumer
- Reserve jobs from beanstalkd/tube, destroy/bury a job when necessaryForex
- Connect to a exchange rate service providerMongo Data Store
- Save data to MongoDBJob Exit Strategy
- Make decision on when to exit a jobWorker
- Glue to above components into a flow
-
All external connectivity configurations are inside
lib/job_config.js
-
Worker specific settings are inside
worker.js
const MAX_SUCCESS = 10;
const MAX_FAIL = 3;
const DELAY_SUCCESS = 60; // seconds
const DELAY_FAIL = 3; // seconds
- Cover major requirements, without external connectivity
- Usually it should mock the underlying lib, here I just skip it
- Cover the connectivity to external services
To start a consumer worker
node worker
To seed a job (by default USD:HKD)
node seed
To seed a job and specific currency pair
node seed USD HKD
Supported currency pairs. e.g.
EUR USD
USD JPY
GBP USD
NZD USD
USD HKD
Full list can be found inside the [FXCM XML] (http://rates.fxcm.com/RatesXML) with XPath /Rates/Rate/Symbol
No error occurs when seeding non-exist pair:
node seed ABC DEF
Instead, failure would be caught in the consumer worker when it tries to get the exchange rate. So it is a convenient way to test the failure path. e.g. Consumer worker logs
Waiting for a job...
Job received { job_id: '597',
payload: { from: 'ABC', to: 'DEF', failAttempts: 2 } }
Try get Exchange Rate ABC:DEF
Fail to get Exchange Rate No value for the pair ABC:DEF
Job Attempts Success/Fail = undefined/3
Job failed with failAttempts=3
grunt lint
is producing strange warnings. Anyway the codes follow the coding styles, guidelines, and JSDoc.
- FXCM - Return Exchange Rates in XML: http://rates.fxcm.com/RatesXML
- MongoLab - MongoDB provider: http://www.mongolab.com/