This repository was archived by the owner on Mar 5, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathpool.coffee
More file actions
161 lines (121 loc) · 4.36 KB
/
Copy pathpool.coffee
File metadata and controls
161 lines (121 loc) · 4.36 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
PoolFactory = require('generic-pool').Pool
async = require 'async'
bunyan = require 'bunyan'
Container = require './container'
class Pool
constructor: (options={}) ->
@_log = options.log || bunyan.createLogger(name: 'docker-pool', module: 'pool')
delete options.log
@_maxDestroyAttempts = @_option options.maxDestroyAttempts, 10
delete options.maxDestroyAttempts
@_readyCheck = @_option options.readyCheck, (container, retryCount, callback) ->
callback(null, true, 0 ) # default callback indicates immediate readiness
@_createConcurrency = @_option options.createConcurrency, 1
delete options.createConcurrency
@_disposeConcurrency = @_option options.disposeConcurrency, 1
delete options.disposeConcurrency
@_containerOptions = options.container
@_containerOptions.log = @_log
delete options.container
for key, value of options
@[key] = value
@_createQueue = async.queue(@_createWorker, @_createConcurrency)
@_destroyQueue = async.queue(@_destroyWorker, @_disposeConcurrency)
@_containers = {}
@_pool = new PoolFactory(@)
acquire: (callback, priority) =>
retryOnError = (err, container) =>
if err
if container?
# ._log? makes it so we don't need a real container for tests
container._log?.warn('retry acquire')
@dispose container
else
@_log.warn('retry acquire')
@acquire callback, priority
else
callback(null, container)
@_pool.acquire retryOnError, priority
release: (container) ->
container._log.info('releasing')
@_pool.release(container)
#
# Public: Wait for all currently acquired containers to finish their current job, then dispose of all containers.
# An error will be thrown if acquire is called after drain.
#
drain: (callback) ->
@_log.info('draining')
@_pool.min = 0
@_pool.drain =>
@_log.info('drained')
containers = []
for _, container of @_containers
containers.push container
if containers.length > 0 || @_destroyQueue.length() > 0
drain = @_destroyQueue.drain
@_destroyQueue.drain = =>
@_destroyQueue.drain = drain
callback()
for container in containers
@dispose container
else
callback()
#
# Public: Dispose of a container by removing it from the pool and destroying it.
#
dispose: (container, callback) ->
container.on 'destroy', callback if callback
@_pool.destroy(container)
#
# Internal: Used by the pool to create a new container
#
create: (callback) =>
@_createQueue.push {}, callback
#
# Internal: Used by the pool to destroy a container
#
destroy: (container) ->
afterDestroy = (err) =>
if err
if container.destroyCount >= @_maxDestroyAttempts
container._log.info 'cannot destroy: giving up'
else
container._log.info 'retry destroy'
retry = =>
@_destroyWorker container, afterDestroy
setTimeout retry, 1000
else
delete @_containers[container.id]
@_destroyQueue.push container, afterDestroy
#
# Private: Creates a new container. This function is used as the worker for the @_createQueue.
#
_createWorker: (_, callback) =>
container = new Container(@_containerOptions)
container.start (err) =>
return callback(err) if err
@_containers[container.id] = container
@_log.info(container: container.id, 'started')
retryCount = 0
readyCheckCallback = (err=null, ready=true, retryMillis=100) =>
if err
@_log.error(err, 'ready check error')
callback(err, container)
else if ready
@_log.info(container: container.id, 'ready')
callback(null, container)
else
retryCount += 1
@_log.warn(container: container.id, wait: retryMillis, retries: retryCount, 'ready check retry')
retryReadyCheck = =>
@_readyCheck container, retryCount, readyCheckCallback
setTimeout retryReadyCheck, retryMillis
@_readyCheck container, retryCount, readyCheckCallback
#
# Private: Destroys a container. This function is used as the worker for the @_destroyQueue.
#
_destroyWorker: (container, callback) =>
container.destroy callback
_option: (value, deflt) ->
if value? then value else deflt
module.exports = Pool