-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
waitfordb function and package config
- Loading branch information
1 parent
3d1513c
commit c0e447b
Showing
4 changed files
with
84 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
/node_modules/ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
#! /usr/bin/env node | ||
'use strict' | ||
|
||
const path = require('path'); | ||
|
||
const KNEXFILE_DEFAULT = 'knexfile'; | ||
const RETRY_DEFAULT = 10; | ||
const DELAY_DEFAULT = 1; | ||
const TESTSQL = 'select 1+1'; | ||
|
||
const sleep = (time) => new Promise((r) => setTimeout(r, time)); | ||
|
||
const retry = async (fn, attempts, delaySeconds) => { | ||
for (let i = 1; i <= attempts; i++) { | ||
try { | ||
await fn(); | ||
return true; | ||
} catch (error) { | ||
console.warn(`Attempt #${i}/${attempts} failed: ${error.message}`); | ||
} | ||
if (i < attempts) { | ||
await sleep(1000 * delaySeconds); | ||
} | ||
} | ||
return false; | ||
}; | ||
|
||
const checkConnection = async (config) => { | ||
const knex = require('knex')(config); | ||
try { | ||
await knex.raw(TESTSQL); | ||
} finally { | ||
await knex.destroy(); | ||
} | ||
} | ||
|
||
const checkKnexInstalled = () => { | ||
try { | ||
require('knex'); | ||
} catch (e) { | ||
return false; | ||
} | ||
return true; | ||
} | ||
|
||
const waitfordb = async () => { | ||
const config = require(path.join(process.cwd(), KNEXFILE_DEFAULT)) | ||
if (!checkKnexInstalled()) { | ||
console.error("ERROR: Knex is not installed. Please run 'npm install knex'"); | ||
return process.exit(1); | ||
} | ||
|
||
const rc = await retry(() => checkConnection(config), RETRY_DEFAULT, DELAY_DEFAULT); | ||
if (rc) { | ||
console.info('Database connection successful'); | ||
return process.exit(0); | ||
} else { | ||
console.error('ERROR: There are no more attempts left, exiting...'); | ||
return process.exit(1); | ||
} | ||
} | ||
|
||
|
||
waitfordb() | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
{ | ||
"name": "knex-waitfordb", | ||
"version": "1.0.0", | ||
"description": "Waiting knex database connection", | ||
"license": "Apache-2.0", | ||
"repository": { | ||
"type": "git", | ||
"url": "https://github.com/plesk/knex-waitfordb.git" | ||
}, | ||
"bin": { | ||
"knex-waitfordb": "index.js" | ||
}, | ||
"dependencies": {} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
# THIS IS AN AUTOGENERATED FILE. DO NOT EDIT THIS FILE DIRECTLY. | ||
# yarn lockfile v1 | ||
|
||
|