UNPKG

1.55 kBtext/coffeescriptView Raw
1#!/usr/bin/env coffee
2require('coffee-script').register()
3
4async = require 'async'
5path = require 'path'
6
7bobbin = require '../src/bobbin.coffee'
8
9async.series [
10
11
12
13 # Example 1: Hello World
14 (example_cb) ->
15 bobbin.create (err, pool) ->
16 return example_cb(err) if err?
17
18 # Work function says hello world and how many times run
19 work_function = (n, callback) ->
20 console.log "Hello World #{n}!"
21 callback()
22
23 # use caolan/async to manage async calls
24 async.times 5, (n, callback) ->
25
26 # call the Hello World work function with n injected
27 pool.run n, work_function, callback
28
29 , (err) ->
30 return example_cb(err) if err?
31 console.log 'Example 1 Done!\n'
32 example_cb()
33
34
35
36 # Example 2: Module Pathname Injection
37 , (example_cb) ->
38 # Define some options, including the working directory for this pool
39 opts = {
40 work_dir: path.resolve(__dirname, './path/name/injection/example/')
41 num_workers: 5
42 }
43
44 # Pass opts into bobbin.create this time
45 bobbin.create opts, (err, pool) ->
46 return example_cb(err) if err?
47
48 # Print the string exported from the example module.
49 # require() will look for the module in opts.work_dir
50 work_function = (callback) ->
51 console.log require('./module.coffee')
52 callback()
53
54
55 pool.run work_function, (err) ->
56 return example_cb(err) if err?
57 console.log 'Example 2 Done!\n'
58 example_cb()
59
60
61
62], (err) ->
63 if err?
64 console.error 'There was an error!'
65 console.error err
66 console.trace err
67 process.exit -1
68
69 console.log 'Examples finished!'
70 process.exit 0
\No newline at end of file