Mentions légales du service
Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
C
c-service
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Package Registry
Container Registry
Model registry
Operate
Environments
Terraform modules
Monitor
Incidents
Service Desk
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
concordant
Software
c-service
Commits
ffd98acd
Commit
ffd98acd
authored
4 years ago
by
Ilyas Toumlilt
Committed by
Florent Coriat
4 years ago
Browse files
Options
Downloads
Patches
Plain Diff
Fix: if DBNAME doesn't exist, init DB
parent
32caff78
No related branches found
No related tags found
1 merge request
!21
Resolve "Merge new-api branch into master"
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
src/server.ts
+37
-0
37 additions, 0 deletions
src/server.ts
with
37 additions
and
0 deletions
src/server.ts
+
37
−
0
View file @
ffd98acd
...
...
@@ -186,16 +186,53 @@ server.applyMiddleware({ app });
app
.
listen
({
port
:
4000
});
/**
* DBNAME is required env arg for the application, exit if unset
*/
if
(
!
dbName
)
{
console
.
error
(
"
Please set DBNAME environment variable
"
);
process
.
exit
(
1
);
}
/**
* Creates a new Database, used when DBNAME doesn't exist
*
* In case of failure, the server will exist with error
* TODO: init DB with defaults
*
* @param appDB CouchDB DatabaseScope instance where to create the new DB
* @param dbName new DB name
*/
function
createDB
(
appDB
:
Nano
.
DatabaseScope
,
dbName
:
string
)
{
console
.
log
(
"
[SERVER] Creating DB:
"
+
dbName
);
appDB
.
create
(
dbName
)
.
then
((
body
)
=>
{
console
.
log
(
`[SERVER] database
${
dbName
}
created!`
);
})
.
catch
((
error
)
=>
{
console
.
log
(
`[SERVER][ERROR] Failed creating database
${
dbName
}
`
);
console
.
log
(
error
);
process
.
exit
(
1
);
});
}
/**
* Poll appDB waiting for connection ack
*
* If DBNAME doesn't exist, it will be created
*
* @param timeout polling sleep duration, multiplied by 2 at each retry
*/
const
connectDB
=
(
timeout
=
1000
)
=>
appDB
.
info
().
catch
((
error
)
=>
{
const
retryIn
=
Math
.
min
(
timeout
*
2
,
maxRetryTimeout
);
setTimeout
(()
=>
connectDB
(
retryIn
),
retryIn
);
console
.
warn
(
error
,
`retry in
${
retryIn
}
`
);
// If DB doesn't exist, create it before first retry
if
(
error
&&
error
.
error
===
"
not_found
"
&&
timeout
<=
1000
)
{
createDB
(
client
.
db
,
dbName
);
}
});
const
client
=
Nano
({
url
:
dbUrl
,
requestDefaults
:
{
jar
:
true
}
});
...
...
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment