Skip to content

Commit

Permalink
Added auto incremented primary key
Browse files Browse the repository at this point in the history
  • Loading branch information
mattwilshire committed Oct 30, 2024
1 parent 74199f6 commit ba2875f
Show file tree
Hide file tree
Showing 9 changed files with 49 additions and 32 deletions.
21 changes: 18 additions & 3 deletions webapp/src/main/java/com/box/l10n/mojito/entity/ScheduledJob.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
import jakarta.persistence.Column;
import jakarta.persistence.Entity;
import jakarta.persistence.ForeignKey;
import jakarta.persistence.GeneratedValue;
import jakarta.persistence.GenerationType;
import jakarta.persistence.Id;
import jakarta.persistence.JoinColumn;
import jakarta.persistence.ManyToOne;
Expand All @@ -18,7 +20,12 @@
@Entity
@Table(name = "scheduled_job")
public class ScheduledJob {
@Id private String id;
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;

@Column(name = "uuid")
private String uuid;

@ManyToOne
@JoinColumn(
Expand Down Expand Up @@ -67,14 +74,22 @@ public void deserializeProperties() {
}
}

public String getId() {
public Long getId() {
return id;
}

public void setId(String id) {
public void setId(Long id) {
this.id = id;
}

public String getUuid() {
return uuid;
}

public void setUuid(String uuid) {
this.uuid = uuid;
}

public Repository getRepository() {
return repository;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ public List<ScheduledJobDTO> getAllJobs() {
@ResponseStatus(HttpStatus.OK)
public ScheduledJobDTO getJob(@PathVariable UUID id) {
return scheduledJobRepository
.findById(id.toString())
.findByUuid(id.toString())
.map(ScheduledJobDTO::new)
.orElseThrow(
() ->
Expand All @@ -66,7 +66,7 @@ public ScheduledJobDTO getJob(@PathVariable UUID id) {
@ResponseStatus(HttpStatus.OK)
public ResponseEntity<ScheduledJobResponse> triggerJob(@PathVariable UUID id) {

Optional<ScheduledJob> optScheduledJob = scheduledJobRepository.findById(id.toString());
Optional<ScheduledJob> optScheduledJob = scheduledJobRepository.findByUuid(id.toString());

if (optScheduledJob.isEmpty()) return notFoundResponse;

Expand Down Expand Up @@ -121,7 +121,7 @@ public ResponseEntity<ScheduledJobResponse> disableJob(@PathVariable UUID id) {
}

private ResponseEntity<ScheduledJobResponse> setJobActive(UUID id, boolean active) {
Optional<ScheduledJob> optScheduledJob = scheduledJobRepository.findById(id.toString());
Optional<ScheduledJob> optScheduledJob = scheduledJobRepository.findByUuid(id.toString());

if (optScheduledJob.isEmpty()) return notFoundResponse;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ public class ScheduledJobDTO {
private Boolean enabled;

public ScheduledJobDTO(ScheduledJob scheduledJob) {
this.id = scheduledJob.getId();
this.id = scheduledJob.getUuid();
this.repository = scheduledJob.getRepository().getName();
this.jobType = scheduledJob.getJobType().getEnum();
this.cron = scheduledJob.getCron();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ public void jobToBeExecuted(JobExecutionContext context) {
/* If multi quartz scheduler is not being used, the listener will
be attached to default jobs, ignore if it's not a scheduled job */
Optional<ScheduledJob> optScheduledJob =
scheduledJobRepository.findById(context.getJobDetail().getKey().getName());
scheduledJobRepository.findByUuid(context.getJobDetail().getKey().getName());

if (optScheduledJob.isEmpty()) return;
ScheduledJob scheduledJob = optScheduledJob.get();
Expand Down Expand Up @@ -84,7 +84,7 @@ public void jobToBeExecuted(JobExecutionContext context) {
public void jobWasExecuted(JobExecutionContext context, JobExecutionException jobException) {

Optional<ScheduledJob> optScheduledJob =
scheduledJobRepository.findById(context.getJobDetail().getKey().getName());
scheduledJobRepository.findByUuid(context.getJobDetail().getKey().getName());

if (optScheduledJob.isEmpty()) return;
ScheduledJob scheduledJob = optScheduledJob.get();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
import java.util.Arrays;
import java.util.HashSet;
import java.util.List;
import java.util.Optional;
import org.quartz.CronScheduleBuilder;
import org.quartz.JobBuilder;
import org.quartz.JobDetail;
Expand Down Expand Up @@ -148,15 +149,11 @@ public void pushJobsToDB() {

uuidPool.add(syncJobConfig.getUuid());

ScheduledJob scheduledJob =
scheduledJobRepository.findByIdAndJobType(
syncJobConfig.getUuid(), ScheduledJobType.THIRD_PARTY_SYNC);
Optional<ScheduledJob> optScheduledJob =
scheduledJobRepository.findByUuid(syncJobConfig.getUuid());
ScheduledJob scheduledJob = optScheduledJob.orElseGet(ScheduledJob::new);

if (scheduledJob == null) {
scheduledJob = new ScheduledJob();
}

scheduledJob.setId(syncJobConfig.getUuid());
scheduledJob.setUuid(syncJobConfig.getUuid());
scheduledJob.setCron(syncJobConfig.getCron());
scheduledJob.setRepository(repositoryRepository.findByName(syncJobConfig.getRepository()));
scheduledJob.setJobStatus(
Expand Down Expand Up @@ -194,7 +191,7 @@ public void cleanQuartzJobs() throws SchedulerException {
}

scheduledJobRepository
.findById(jobKey.getName())
.findByUuid(jobKey.getName())
.ifPresent(
job -> {
scheduledJobRepository.delete(job);
Expand Down Expand Up @@ -238,13 +235,13 @@ public Trigger buildTrigger(JobKey jobKey, String cronExpression, TriggerKey tri

public JobKey getJobKey(ScheduledJob scheduledJob) {
// name = UUID, group = THIRD_PARTY_SYNC
return new JobKey(scheduledJob.getId(), scheduledJob.getJobType().getEnum().toString());
return new JobKey(scheduledJob.getUuid(), scheduledJob.getJobType().getEnum().toString());
}

private TriggerKey getTriggerKey(ScheduledJob scheduledJob) {
// name = trigger_UUID, group = THIRD_PARTY_SYNC
return new TriggerKey(
"trigger_" + scheduledJob.getId(), scheduledJob.getJobType().getEnum().toString());
"trigger_" + scheduledJob.getUuid(), scheduledJob.getJobType().getEnum().toString());
}

public Scheduler getScheduler() {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,14 +1,18 @@
package com.box.l10n.mojito.service.scheduledjob;

import com.box.l10n.mojito.entity.ScheduledJob;
import java.util.Optional;
import org.quartz.JobKey;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Query;

public interface ScheduledJobRepository extends JpaRepository<ScheduledJob, String> {
public interface ScheduledJobRepository extends JpaRepository<ScheduledJob, Long> {

@Query("SELECT sj FROM ScheduledJob sj WHERE sj.id = :id AND sj.jobType.jobType = :jobType")
ScheduledJob findByIdAndJobType(String id, ScheduledJobType jobType);
@Query("SELECT sj FROM ScheduledJob sj WHERE sj.uuid = :uuid AND sj.jobType.jobType = :jobType")
ScheduledJob findByIdAndJobType(String uuid, ScheduledJobType jobType);

@Query("SELECT sj FROM ScheduledJob sj WHERE sj.uuid = :uuid")
Optional<ScheduledJob> findByUuid(String uuid);

default ScheduledJob findByJobKey(JobKey jobKey) {
return findByIdAndJobType(jobKey.getName(), ScheduledJobType.valueOf(jobKey.getGroup()));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ public String getName() {
@Override
public void triggerMisfired(Trigger trigger) {
super.triggerMisfired(trigger);
Optional<ScheduledJob> job = scheduledJobRepository.findById(trigger.getJobKey().getName());
Optional<ScheduledJob> job = scheduledJobRepository.findByUuid(trigger.getJobKey().getName());
if (job.isEmpty()) return;
logger.warn(
"TRIGGER MISFIRE FOR {} | {}", job.get().getRepository().getName(), job.get().getJobType());
Expand All @@ -43,7 +43,7 @@ public void triggerMisfired(Trigger trigger) {
@Override
public boolean vetoJobExecution(Trigger trigger, JobExecutionContext context) {
// If the job is disabled, don't execute
Optional<ScheduledJob> job = scheduledJobRepository.findById(trigger.getJobKey().getName());
Optional<ScheduledJob> job = scheduledJobRepository.findByUuid(trigger.getJobKey().getName());
return job.filter(scheduledJob -> !scheduledJob.getEnabled()).isPresent();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,6 @@ public void execute(JobExecutionContext jobExecutionContext) throws JobExecution
new ThirdPartySyncJobInput(scheduledJob, scheduledJobProperties);

try {
System.out.println(timeout);
PollableFuture<Void> task =
quartzPollableTaskScheduler.scheduleJobWithCustomTimeout(
ThirdPartySyncJob.class, thirdPartySyncJobInput, "thirdPartySync", timeout);
Expand All @@ -96,7 +95,7 @@ public void onSuccess(JobExecutionContext context) {
.ifPresent(
pd -> {
try {
pd.resolveIncident(scheduledJob.getId());
pd.resolveIncident(scheduledJob.getUuid());
} catch (PagerDutyException e) {
logger.error(
"Couldn't send resolve PagerDuty notification for successful third party sync of repository: '{}' -",
Expand Down Expand Up @@ -151,7 +150,7 @@ public void onFailure(JobExecutionContext context, JobExecutionException jobExce
"Pollable Task", pollableTaskUrl, "Scheduled Job", scheduledJobUrl));

try {
pd.triggerIncident(scheduledJob.getId(), payload);
pd.triggerIncident(scheduledJob.getUuid(), payload);
} catch (PagerDutyException e) {
logger.error(
"Couldn't send PagerDuty notification for failed third party sync of repository: '{}' -",
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
CREATE TABLE scheduled_job (
id CHAR(36) PRIMARY KEY NOT NULL,
id BIGINT PRIMARY KEY AUTO_INCREMENT,
uuid CHAR(36) NOT NULL,
repository_id BIGINT NOT NULL,
job_type SMALLINT DEFAULT NULL,
cron VARCHAR(50) DEFAULT NULL,
Expand All @@ -16,8 +17,8 @@ CREATE TABLE scheduled_job_type (
);

CREATE TABLE scheduled_job_status_type (
id SMALLINT NOT NULL AUTO_INCREMENT PRIMARY KEY,
name VARCHAR(255) NOT NULL
id SMALLINT NOT NULL AUTO_INCREMENT PRIMARY KEY,
name VARCHAR(255) NOT NULL
);

ALTER TABLE scheduled_job
Expand All @@ -38,7 +39,8 @@ INSERT INTO scheduled_job_type(id, name) VALUES(1, 'THIRD_PARTY_SYNC');
INSERT INTO scheduled_job_status_type(id, name) VALUES(1, 'SCHEDULED'), (2, 'IN_PROGRESS'), (3,'FAILED'), (4,'SUCCEEDED');

CREATE TABLE scheduled_job_aud (
id CHAR(36) NOT NULL,
id BIGINT,
uuid CHAR(36),
rev INTEGER NOT NULL,
revtype TINYINT,
revend INTEGER,
Expand Down

0 comments on commit ba2875f

Please sign in to comment.