Skip to content

Commit f438e25

Browse files
committed
convert project add API to asynchronous
fixes #3878
1 parent ad8a45c commit f438e25

File tree

4 files changed

+36
-23
lines changed

4 files changed

+36
-23
lines changed

opengrok-web/src/main/java/org/opengrok/web/api/v1/controller/ProjectsController.java

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
*/
1919

2020
/*
21-
* Copyright (c) 2018, 2021, Oracle and/or its affiliates. All rights reserved.
21+
* Copyright (c) 2018, 2022, Oracle and/or its affiliates. All rights reserved.
2222
* Portions Copyright (c) 2020, Chris Fraire <[email protected]>.
2323
*/
2424
package org.opengrok.web.api.v1.controller;
@@ -85,10 +85,22 @@ public class ProjectsController {
8585

8686
@POST
8787
@Consumes(MediaType.TEXT_PLAIN)
88-
public Response addProject(String projectName) {
88+
public Response addProject(@Context HttpServletRequest request, String projectNameParam) {
8989
// Avoid classification as a taint bug.
90-
projectName = Laundromat.launderInput(projectName);
90+
final String projectName = Laundromat.launderInput(projectNameParam);
91+
92+
LOGGER.log(Level.INFO, "adding project {0}", projectName);
9193

94+
return ApiTaskManager.getInstance().submitApiTask(PROJECTS_PATH,
95+
new ApiTask(request.getRequestURI(),
96+
() -> {
97+
addProjectWorkHorse(projectName);
98+
return null;
99+
},
100+
Response.Status.CREATED));
101+
}
102+
103+
private void addProjectWorkHorse(String projectName) {
92104
File srcRoot = env.getSourceRootFile();
93105
File projDir = new File(srcRoot, projectName);
94106

@@ -138,8 +150,6 @@ public Response addProject(String projectName) {
138150
map.put(project, repos);
139151
}
140152
}
141-
142-
return Response.status(Response.Status.CREATED).build();
143153
}
144154

145155
private List<RepositoryInfo> getRepositoriesInDir(final File projDir) {

opengrok-web/src/test/java/org/opengrok/web/api/v1/controller/ProjectsControllerTest.java

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
*/
1919

2020
/*
21-
* Copyright (c) 2018, 2021, Oracle and/or its affiliates. All rights reserved.
21+
* Copyright (c) 2018, 2022, Oracle and/or its affiliates. All rights reserved.
2222
* Portions Copyright (c) 2019, 2020, Chris Fraire <[email protected]>.
2323
*/
2424
package org.opengrok.web.api.v1.controller;
@@ -155,10 +155,11 @@ void testAddInherit() {
155155
assertTrue(proj.isHandleRenamedFiles());
156156
}
157157

158-
private void addProject(final String project) {
159-
target("projects")
158+
private Response addProject(final String project) {
159+
Response response = target("projects")
160160
.request()
161161
.post(Entity.text(project));
162+
return waitForTask(response);
162163
}
163164

164165
/**
@@ -319,8 +320,7 @@ void testDelete() throws Exception {
319320
Indexer.getInstance().doIndexerExecution(true, null, null);
320321

321322
for (String proj : projectsToDelete) {
322-
Response response = delete(proj);
323-
waitForTask(response);
323+
deleteProject(proj);
324324
}
325325

326326
assertEquals(1, env.getProjects().size());
@@ -349,11 +349,12 @@ void testDelete() throws Exception {
349349
assertEquals(0, group.getProjects().size());
350350
}
351351

352-
private Response delete(final String project) {
353-
return target("projects")
352+
private Response deleteProject(final String project) {
353+
Response response = target("projects")
354354
.path(project)
355355
.request()
356356
.delete();
357+
return waitForTask(response);
357358
}
358359

359360
@Test

tools/src/main/python/opengrok_tools/projadm.py

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
# CDDL HEADER END
1919

2020
#
21-
# Copyright (c) 2018, 2021, Oracle and/or its affiliates. All rights reserved.
21+
# Copyright (c) 2018, 2022, Oracle and/or its affiliates. All rights reserved.
2222
#
2323

2424
"""
@@ -164,18 +164,16 @@ def config_refresh(doit, logger, basedir, uri, configmerge, jar_file,
164164
install_config(doit, logger, fmerged.name, main_config)
165165

166166

167-
def project_add(doit, logger, project, uri, headers=None, timeout=None):
167+
def project_add(doit, logger, project, uri, headers=None, timeout=None, api_timeout=None):
168168
"""
169-
Adds a project to configuration. Works in multiple steps:
170-
171-
1. add the project to configuration
172-
2. refresh on disk configuration
169+
Adds a project to configuration.
173170
"""
174171

175172
logger.info("Adding project {}".format(project))
176173

177174
if doit:
178-
if add_project(logger, project, uri, headers=headers, timeout=timeout):
175+
if add_project(logger, project, uri, headers=headers, timeout=timeout,
176+
api_timeout=api_timeout):
179177
repos = get_repos(logger, project, uri,
180178
headers=headers, timeout=timeout)
181179
if repos:
@@ -356,7 +354,8 @@ def main():
356354
project_add(doit=doit, logger=logger,
357355
project=proj,
358356
uri=uri, headers=headers,
359-
timeout=args.api_timeout)
357+
timeout=args.api_timeout,
358+
api_timeout=args.async_api_timeout)
360359

361360
config_refresh(doit=doit, logger=logger,
362361
basedir=args.base,

tools/src/main/python/opengrok_tools/utils/opengrok.py

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -174,10 +174,13 @@ def list_indexed_projects(logger, uri, headers=None, timeout=None):
174174
return res.json()
175175

176176

177-
def add_project(logger, project, uri, headers=None, timeout=None):
177+
def add_project(logger, project, uri, headers=None, timeout=None, api_timeout=None):
178178
try:
179-
do_api_call('POST', get_uri(uri, 'api', 'v1', 'projects'),
180-
data=project, headers=headers, timeout=timeout)
179+
r = do_api_call('POST', get_uri(uri, 'api', 'v1', 'projects'),
180+
data=project, headers=headers, timeout=timeout, api_timeout=api_timeout)
181+
if r is None or r.status_code != 201:
182+
logger.error(f"could not add project '{project}' in web application")
183+
return False
181184
except Exception as exception:
182185
logger.error("could not add project '{}' to web application: {}".
183186
format(project, exception))

0 commit comments

Comments
 (0)