Search in sources :

Example 16 with WriteTransaction

use of bio.terra.common.db.WriteTransaction in project terra-workspace-manager by DataBiosphere.

the class ApplicationDao method updateApplication.

/**
 * Update an application based on the configuration
 *
 * @param app the updated application configuration
 */
@WriteTransaction
public void updateApplication(WsmApplication app) {
    final String sql = "UPDATE application SET" + " display_name = :display_name," + " description = :description," + " service_account = :service_account," + " state = :state" + " WHERE application_id = :application_id";
    var params = new MapSqlParameterSource().addValue("application_id", app.getApplicationId()).addValue("display_name", app.getDisplayName()).addValue("description", app.getDescription()).addValue(SERVICE_ACCOUNT, app.getServiceAccount()).addValue("state", app.getState().toDb());
    jdbcTemplate.update(sql, params);
}
Also used : MapSqlParameterSource(org.springframework.jdbc.core.namedparam.MapSqlParameterSource) WriteTransaction(bio.terra.common.db.WriteTransaction)

Example 17 with WriteTransaction

use of bio.terra.common.db.WriteTransaction in project terra-workspace-manager by DataBiosphere.

the class ApplicationDao method disableWorkspaceApplication.

/**
 * Disable an application in a workspace. It is not an error to disable an already disabled
 * application.
 *
 * @param workspaceUuid workspace of interest
 * @param applicationId application of interest
 * @return workspace-application object
 */
@WriteTransaction
public WsmWorkspaceApplication disableWorkspaceApplication(UUID workspaceUuid, String applicationId) {
    // Validate that the application exists; workspace is validated in layers above this
    getApplicationOrThrow(applicationId);
    // It is an error to have application resources in the workspace if we are disabling it.
    final String countAppUsesSql = "SELECT COUNT(*) FROM resource" + " WHERE associated_app = :application_id AND workspace_id = :workspace_id";
    MapSqlParameterSource params = new MapSqlParameterSource().addValue("workspace_id", workspaceUuid.toString()).addValue("application_id", applicationId.toString());
    Integer count = jdbcTemplate.queryForObject(countAppUsesSql, params, Integer.class);
    if (count != null && count > 0) {
        throw new ApplicationInUseException(String.format("Application %s in use in workspace %s", applicationId, workspaceUuid));
    }
    // No uses, so we disable
    final String sql = "DELETE FROM enabled_application" + " WHERE workspace_id = :workspace_id AND application_id = :application_id";
    int rowCount = jdbcTemplate.update(sql, params);
    if (rowCount > 0) {
        logger.info("Deleted record enabling application {} for workspace {}", applicationId, workspaceUuid);
    } else {
        logger.info("Ignoring duplicate disabling of application {} for workspace {}", applicationId, workspaceUuid);
    }
    return getWorkspaceApplicationWorker(workspaceUuid, applicationId);
}
Also used : ApplicationInUseException(bio.terra.workspace.db.exception.ApplicationInUseException) MapSqlParameterSource(org.springframework.jdbc.core.namedparam.MapSqlParameterSource) WriteTransaction(bio.terra.common.db.WriteTransaction)

Example 18 with WriteTransaction

use of bio.terra.common.db.WriteTransaction in project terra-workspace-manager by DataBiosphere.

the class CronjobDao method claimJob.

/**
 * Attempt to claim the latest run of a cron job in the database.
 *
 * <p>This method reads the timestamp of the most recent run of the specified job. If {@code
 * timeSinceLastRun} has elapsed since the run, this method will write the current time as the
 * latest timestamp in the table and return true. If less than {@timeSinceLastRun} has elapsed
 * this will return false without modifying the database.
 *
 * <p>Jobs which have never been run before are treated as if the last run was at Instant.EPOCH
 * (1970-01-01T00:00:00Z).
 *
 * <p>This method uses the {@WriteTransaction} annotation to ensure the read and write queries are
 * executed as a single transaction.
 *
 * @param jobName Name of the job being claimed
 * @param timeSinceLastRun If this much time has elapsed since the previous job execution, claim
 *     the latest job run. Otherwise, do nothing.
 * @return true if {@timeSinceLastRun} has elapsed since the job's last execution, false otherwise
 */
@WriteTransaction
public boolean claimJob(String jobName, Duration timeSinceLastRun) {
    String readQuery = "SELECT date_last_run FROM cronjob_state WHERE cronjob_name = :cronjob_name";
    MapSqlParameterSource readParams = new MapSqlParameterSource().addValue("cronjob_name", jobName);
    Timestamp lastJobRun;
    try {
        lastJobRun = jdbcTemplate.queryForObject(readQuery, readParams, Timestamp.class);
    } catch (EmptyResultDataAccessException e) {
        lastJobRun = Timestamp.from(Instant.EPOCH);
    }
    if (lastJobRun.toInstant().plus(timeSinceLastRun).isAfter(Instant.now())) {
        // Job has been run more recently, do nothing.
        return false;
    }
    String upsertQuery = "INSERT INTO cronjob_state (cronjob_name, date_last_run) VALUES (:cronjob_name, :timestamp) " + "ON CONFLICT (cronjob_name) " + "DO UPDATE SET date_last_run = :timestamp";
    MapSqlParameterSource upsertParams = new MapSqlParameterSource().addValue("cronjob_name", jobName).addValue("timestamp", Timestamp.from(Instant.now()));
    jdbcTemplate.update(upsertQuery, upsertParams);
    return true;
}
Also used : MapSqlParameterSource(org.springframework.jdbc.core.namedparam.MapSqlParameterSource) EmptyResultDataAccessException(org.springframework.dao.EmptyResultDataAccessException) Timestamp(java.sql.Timestamp) WriteTransaction(bio.terra.common.db.WriteTransaction)

Aggregations

WriteTransaction (bio.terra.common.db.WriteTransaction)18 MapSqlParameterSource (org.springframework.jdbc.core.namedparam.MapSqlParameterSource)18 DuplicateKeyException (org.springframework.dao.DuplicateKeyException)5 ApplicationInUseException (bio.terra.workspace.db.exception.ApplicationInUseException)2 DuplicateCloudContextException (bio.terra.workspace.service.workspace.exceptions.DuplicateCloudContextException)2 DuplicateUserFacingIdException (bio.terra.workspace.service.workspace.exceptions.DuplicateUserFacingIdException)2 MissingRequiredFieldException (bio.terra.common.exception.MissingRequiredFieldException)1 DbResource (bio.terra.workspace.db.model.DbResource)1 DuplicateResourceException (bio.terra.workspace.service.resource.exception.DuplicateResourceException)1 SpendProfileId (bio.terra.workspace.service.spendprofile.SpendProfileId)1 DuplicateWorkspaceException (bio.terra.workspace.service.workspace.exceptions.DuplicateWorkspaceException)1 Timestamp (java.sql.Timestamp)1 EmptyResultDataAccessException (org.springframework.dao.EmptyResultDataAccessException)1