Skip to content

Commit 1e28bca

Browse files
glebecmweibel
authored andcommitted
Promote stopExpiringSessions to public API method (#66)
* docs(readme): promote `stopExpiringSessions` to public API method * test: use `stopExpiringSessions` instead of negative interval * test: #stopExpiringSessions * docs(readme): add missing syntax highlighting to other snippets * chore: add `after`, `afterEach` to global pragma for linter
1 parent ab3a3e2 commit 1e28bca

File tree

3 files changed

+56
-8
lines changed

3 files changed

+56
-8
lines changed

README.md

Lines changed: 21 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -61,22 +61,22 @@ app.use(session({
6161
store: new SequelizeStore({
6262
db: sequelize
6363
}),
64-
resave: false, // we support the touch method so per the express-session docs this should be set to false
64+
resave: false, // we support the touch method so per the express-session docs this should be set to false
6565
proxy: true // if you do SSL outside of node.
6666
}))
6767
// continue as normal
6868
```
6969

7070
If you want SequelizeStore to create/sync the database table for you, you can call `sync()` against an instance of `SequelizeStore` - this will run a sequelize `sync()` operation on the model for an initialized SequelizeStore object:
7171

72-
```
72+
```javascript
7373
var myStore = new SequelizeStore({
7474
db: sequelize
75-
})
75+
})
7676
app.use(session({
7777
secret: 'keyboard cat',
7878
store: myStore,
79-
resave: false,
79+
resave: false,
8080
proxy: true
8181
}))
8282

@@ -87,14 +87,30 @@ myStore.sync();
8787

8888
Session records are automatically expired and removed from the database on an interval. The `cookie.expires` property is used to set session expiry time. If that property doesn't exist, a default expiry of 24 hours is used. Expired session are removed from the database every 15 minutes by default. That interval as well as the default expiry time can be set as store options:
8989

90-
```
90+
```javascript
9191
new SequelizeStore({
9292
...
9393
checkExpirationInterval: 15 * 60 * 1000, // The interval at which to cleanup expired sessions in milliseconds.
9494
expiration: 24 * 60 * 60 * 1000 // The maximum age (in milliseconds) of a valid session.
9595
});
9696
```
9797

98+
## Expiration interval cleanup: `stopExpiringSessions`
99+
100+
As expirations are checked on an interval timer, `connect-session-sequelize` can keep your process from exiting. This can be problematic e.g. in testing when it is known that the application code will no longer be used, but the test script never terminates. If you know that the process will no longer be used, you can manually clean up the interval by calling the `stopExpiringSessions` method:
101+
102+
```js
103+
// assuming you have set up a typical session store, for example:
104+
var myStore = new SequelizeStore({
105+
db: sequelize
106+
});
107+
108+
// you can stop expiring sessions (cancel the interval). Example using Mocha:
109+
after('clean up resources', () => {
110+
myStore.stopExpiringSessions();
111+
});
112+
```
113+
98114
# Add custom field(s) as a column
99115

100116
The `extendDefaultFields` can be used to add custom fields to the session table. These fields will be read-only as they will be inserted only when the session is first created as `defaults`. Make sure to return an object which contains unmodified `data` and `expires` properties, or else the module functionality will be broken:

lib/connect-session-sequelize.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -158,6 +158,8 @@ module.exports = function SequelizeSessionInit (Store) {
158158
SequelizeStore.prototype.stopExpiringSessions = function stopExpiringSessions () {
159159
if (this._expirationInterval) {
160160
clearInterval(this._expirationInterval)
161+
// added as a sanity check for testing
162+
this._expirationInterval = null
161163
}
162164
}
163165

test/test.js

Lines changed: 33 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
/* global describe,before,beforeEach,it */
1+
/* global describe,before,beforeEach,after,afterEach,it */
22

33
var assert = require('assert')
44
var session = require('express-session')
@@ -14,12 +14,16 @@ var db = new Sequelize('session_test', 'test', '12345', {
1414
})
1515
var store = new SequelizeStore({
1616
db: db,
17-
// disable expiration interval otherwise tests don't finish
18-
checkExpirationInterval: -1
17+
// the expiration check interval is removed up in an `after` block
18+
checkExpirationInterval: 100
1919
})
2020
var sessionId = '1234a'
2121
var sessionData = {foo: 'bar', 'baz': 42}
2222

23+
after('clean up resources, allowing tests to terminate', function () {
24+
store.stopExpiringSessions()
25+
})
26+
2327
describe('store', function () {
2428
before(function () {
2529
return store.sync()
@@ -217,3 +221,29 @@ describe('#clearExpiredSessions()', function () {
217221
})
218222
})
219223
})
224+
225+
describe('#stopExpiringSessions()', function () {
226+
var store
227+
beforeEach(function () {
228+
var db = new Sequelize(
229+
'session_test',
230+
'test',
231+
'12345',
232+
{ dialect: 'sqlite', logging: false }
233+
)
234+
db.import(path.join(__dirname, 'resources/model'))
235+
store = new SequelizeStore({
236+
db: db,
237+
table: 'TestSession',
238+
checkExpirationInterval: 100
239+
})
240+
})
241+
afterEach('clean up resources', function () {
242+
store.stopExpiringSessions()
243+
})
244+
it('should cancel the session check timer', function () {
245+
assert.ok(store._expirationInterval, 'missing timeout object')
246+
store.stopExpiringSessions()
247+
assert.equal(store._expirationInterval, null, 'expiration interval not nullified')
248+
})
249+
})

0 commit comments

Comments
 (0)