Search in sources :

Example 11 with RepoResult

use of com.searchcode.app.model.RepoResult in project searchcode-server by boyter.

the class IndexFileRepoJob method execute.

/**
     * The main method used for finding jobs to index and actually doing the work
     */
@Override
public void execute(JobExecutionContext context) throws JobExecutionException {
    if (!isEnabled()) {
        return;
    }
    if (!this.sharedService.getBackgroundJobsEnabled()) {
        return;
    }
    Thread.currentThread().setPriority(Thread.MIN_PRIORITY);
    while (Singleton.getCodeIndexer().shouldPauseAdding()) {
        Singleton.getLogger().info("Pausing parser.");
        return;
    }
    // Pull the next repo to index from the queue
    UniqueRepoQueue repoQueue = this.getNextQueuedRepo();
    RepoResult repoResult = repoQueue.poll();
    if (repoResult != null && !Singleton.getRunningIndexRepoJobs().containsKey(repoResult.getName())) {
        Singleton.getLogger().info("File Indexer Indexing " + repoResult.getName());
        repoResult.getData().indexStatus = "indexing";
        repoResult.getData().jobRunTime = Instant.now();
        Singleton.getRepo().saveRepo(repoResult);
        try {
            Singleton.getRunningIndexRepoJobs().put(repoResult.getName(), new RunningIndexJob("Indexing", Singleton.getHelpers().getCurrentTimeSeconds()));
            JobDataMap data = context.getJobDetail().getJobDataMap();
            String repoName = repoResult.getName();
            this.repoName = repoName;
            String repoRemoteLocation = repoResult.getUrl();
            String repoLocations = data.get("REPOLOCATIONS").toString();
            this.LOWMEMORY = Boolean.parseBoolean(data.get("LOWMEMORY").toString());
            Path docDir = Paths.get(repoRemoteLocation);
            this.indexDocsByPath(docDir, repoName, repoLocations, repoRemoteLocation, true);
            int runningTime = Singleton.getHelpers().getCurrentTimeSeconds() - Singleton.getRunningIndexRepoJobs().get(repoResult.getName()).startTime;
            repoResult.getData().averageIndexTimeSeconds = (repoResult.getData().averageIndexTimeSeconds + runningTime) / 2;
            repoResult.getData().indexStatus = "success";
            repoResult.getData().jobRunTime = Instant.now();
            Singleton.getRepo().saveRepo(repoResult);
        } finally {
            // Clean up the job
            Singleton.getRunningIndexRepoJobs().remove(repoResult.getName());
        }
    }
}
Also used : RunningIndexJob(com.searchcode.app.dto.RunningIndexJob) Path(java.nio.file.Path) UniqueRepoQueue(com.searchcode.app.util.UniqueRepoQueue) RepoResult(com.searchcode.app.model.RepoResult)

Example 12 with RepoResult

use of com.searchcode.app.model.RepoResult in project searchcode-server by boyter.

the class Repo method getRepoByUrl.

@Override
public synchronized RepoResult getRepoByUrl(String repositoryUrl) {
    if (repositoryUrl == null) {
        return null;
    }
    Connection connection = null;
    PreparedStatement preparedStatement = null;
    ResultSet resultSet = null;
    RepoResult result = null;
    try {
        connection = this.dbConfig.getConnection();
        preparedStatement = connection.prepareStatement("select rowid,name,scm,url,username,password,source,branch,data from repo where url=?;");
        preparedStatement.setString(1, repositoryUrl);
        resultSet = preparedStatement.executeQuery();
        while (resultSet.next()) {
            int rowId = resultSet.getInt("rowid");
            String repoName = resultSet.getString("name");
            String repoScm = resultSet.getString("scm");
            String repoUrl = resultSet.getString("url");
            String repoUsername = resultSet.getString("username");
            String repoPassword = resultSet.getString("password");
            String repoSource = resultSet.getString("source");
            String repoBranch = resultSet.getString("branch");
            String repoData = resultSet.getString("data");
            result = new RepoResult(rowId, repoName, repoScm, repoUrl, repoUsername, repoPassword, repoSource, repoBranch, repoData);
        }
    } catch (SQLException ex) {
        Singleton.getLogger().severe(" caught a " + ex.getClass() + "\n with message: " + ex.getMessage());
    } finally {
        Singleton.getHelpers().closeQuietly(resultSet);
        Singleton.getHelpers().closeQuietly(preparedStatement);
    }
    return result;
}
Also used : SQLException(java.sql.SQLException) Connection(java.sql.Connection) ResultSet(java.sql.ResultSet) PreparedStatement(java.sql.PreparedStatement) RepoResult(com.searchcode.app.model.RepoResult)

Example 13 with RepoResult

use of com.searchcode.app.model.RepoResult in project searchcode-server by boyter.

the class DeleteRepositoryJob method execute.

public void execute(JobExecutionContext context) throws JobExecutionException {
    if (!Singleton.getSharedService().getBackgroundJobsEnabled()) {
        return;
    }
    List<String> persistentDelete = Singleton.getDataService().getPersistentDelete();
    if (persistentDelete.isEmpty()) {
        return;
    }
    RepoResult rr = Singleton.getRepo().getRepoByName(persistentDelete.get(0));
    if (rr == null) {
        Singleton.getDataService().removeFromPersistentDelete(persistentDelete.get(0));
        return;
    }
    try {
        Thread.currentThread().setPriority(Thread.MIN_PRIORITY);
        Singleton.getUniqueGitRepoQueue().delete(rr);
        if (Singleton.getRunningIndexRepoJobs().containsKey(rr.getName())) {
            return;
        }
        Singleton.getLogger().info("Deleting repository. " + rr.getName());
        Singleton.getCodeIndexer().deleteByReponame(rr.getName());
        // remove the directory
        String repoLocations = Properties.getProperties().getProperty(Values.REPOSITORYLOCATION, Values.DEFAULTREPOSITORYLOCATION);
        FileUtils.deleteDirectory(new File(repoLocations + rr.getName() + "/"));
        // Remove from the database
        Singleton.getRepo().deleteRepoByName(rr.getName());
        // Remove from the persistent queue
        Singleton.getDataService().removeFromPersistentDelete(rr.getName());
    } catch (Exception ignored) {
    }
}
Also used : File(java.io.File) RepoResult(com.searchcode.app.model.RepoResult)

Example 14 with RepoResult

use of com.searchcode.app.model.RepoResult in project searchcode-server by boyter.

the class ApiRouteServiceTest method testRepoDeleteAuthReponameNoPub.

public void testRepoDeleteAuthReponameNoPub() {
    Request mockRequest = Mockito.mock(Request.class);
    Repo mockRepo = Mockito.mock(Repo.class);
    UniqueRepoQueue uniqueRepoQueue = new UniqueRepoQueue(new ConcurrentLinkedQueue<>());
    when(mockRepo.getRepoByName("unit-test")).thenReturn(new RepoResult());
    ApiRouteService apiRouteService = new ApiRouteService(null, null, mockRepo, null, null);
    apiRouteService.apiEnabled = true;
    apiRouteService.apiAuth = true;
    when(mockRequest.queryParams("reponame")).thenReturn("unit-test");
    ApiResponse apiResponse = apiRouteService.repoDelete(mockRequest, null);
    assertThat(apiResponse.getMessage()).isEqualTo("pub is a required parameter");
    assertThat(apiResponse.isSucessful()).isFalse();
    assertThat(uniqueRepoQueue.size()).isEqualTo(0);
}
Also used : Repo(com.searchcode.app.dao.Repo) ApiRouteService(com.searchcode.app.service.route.ApiRouteService) Request(spark.Request) UniqueRepoQueue(com.searchcode.app.util.UniqueRepoQueue) ApiResponse(com.searchcode.app.dto.api.ApiResponse) RepoResultApiResponse(com.searchcode.app.dto.api.RepoResultApiResponse) RepoResult(com.searchcode.app.model.RepoResult)

Example 15 with RepoResult

use of com.searchcode.app.model.RepoResult in project searchcode-server by boyter.

the class ApiRouteServiceTest method testRepoDeleteNoAuthReponame.

public void testRepoDeleteNoAuthReponame() {
    Request mockRequest = Mockito.mock(Request.class);
    Repo mockRepo = Mockito.mock(Repo.class);
    DataService dataServiceMock = Mockito.mock(DataService.class);
    when(mockRepo.getRepoByName("unit-test")).thenReturn(new RepoResult());
    ApiRouteService apiRouteService = new ApiRouteService(null, null, mockRepo, dataServiceMock, null);
    apiRouteService.apiEnabled = true;
    apiRouteService.apiAuth = false;
    when(mockRequest.queryParams("reponame")).thenReturn("unit-test");
    ApiResponse apiResponse = apiRouteService.repoDelete(mockRequest, null);
    assertThat(apiResponse.getMessage()).isEqualTo("repository queued for deletion");
    assertThat(apiResponse.isSucessful()).isTrue();
    verify(dataServiceMock, times(1)).addToPersistentDelete("");
}
Also used : Repo(com.searchcode.app.dao.Repo) ApiRouteService(com.searchcode.app.service.route.ApiRouteService) Request(spark.Request) ApiResponse(com.searchcode.app.dto.api.ApiResponse) RepoResultApiResponse(com.searchcode.app.dto.api.RepoResultApiResponse) RepoResult(com.searchcode.app.model.RepoResult)

Aggregations

RepoResult (com.searchcode.app.model.RepoResult)59 Repo (com.searchcode.app.dao.Repo)13 ApiResponse (com.searchcode.app.dto.api.ApiResponse)9 RepoResultApiResponse (com.searchcode.app.dto.api.RepoResultApiResponse)9 Request (spark.Request)9 ValidatorResult (com.searchcode.app.model.ValidatorResult)8 ApiRouteService (com.searchcode.app.service.route.ApiRouteService)8 UniqueRepoQueue (com.searchcode.app.util.UniqueRepoQueue)8 Connection (java.sql.Connection)5 PreparedStatement (java.sql.PreparedStatement)5 SQLException (java.sql.SQLException)5 ResultSet (java.sql.ResultSet)4 ArrayList (java.util.ArrayList)3 RepositoryChanged (com.searchcode.app.dto.RepositoryChanged)2 RunningIndexJob (com.searchcode.app.dto.RunningIndexJob)2 File (java.io.File)2 Gson (com.google.gson.Gson)1 Data (com.searchcode.app.dao.Data)1 IndexBaseRepoJob (com.searchcode.app.jobs.repository.IndexBaseRepoJob)1 IndexFileRepoJob (com.searchcode.app.jobs.repository.IndexFileRepoJob)1