Skip to content

Commit

Permalink
fix(db): 🐛 fix tasks not being properly removed from DB
Browse files Browse the repository at this point in the history
  • Loading branch information
SCadilhac committed Nov 17, 2024
1 parent 0a50791 commit eb877ba
Show file tree
Hide file tree
Showing 12 changed files with 373 additions and 39 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -93,10 +93,15 @@ public void prepare(Session session) {
*/
@Override
public void run() {
log.debug("Task {}. Starting check compliance task for device {}.", this.getId(), device.getId());
this.trace(String.format("Check compliance task for device %s (%s).",
log.debug("Task {}. Starting check compliance task for device {}.", this.getId(),
device == null ? "null" : device.getId());
if (device == null) {
this.info("The device doesn't exist, the task will be cancelled.");
this.status = Status.CANCELLED;
return;
}
this.info(String.format("Check compliance task for device %s (%s).",
device.getName(), device.getMgmtAddress().getIp()));

Session session = Database.getSession();
try {
session.beginTransaction();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,13 @@ public Object clone() throws CloneNotSupportedException {
*/
@Override
public void run() {
log.debug("Task {}. Starting check compliance task for group {}.", this.getId(), deviceGroup.getId());
log.debug("Task {}. Starting check compliance task for group {}.",
this.getId(), deviceGroup == null ? "null" : deviceGroup.getId());
if (deviceGroup == null) {
this.info("The device group doesn't exist, the task will be cancelled.");
this.status = Status.CANCELLED;
return;
}
this.trace(String.format("Check compliance task for group %s.",
deviceGroup.getName()));

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,12 @@ public Object clone() throws CloneNotSupportedException {
@Override
public void run() {
log.debug("Task {}. Starting check software compliance and hardware support status task for group {}.",
this.getId(), deviceGroup.getId());
this.getId(), deviceGroup == null ? "null" : deviceGroup.getId());
if (deviceGroup == null) {
this.info("The device group doesn't exist, the task will be cancelled.");
this.status = Status.CANCELLED;
return;
}
this.trace(String.format("Check software compliance task for group %s.",
deviceGroup.getName()));

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -484,10 +484,10 @@ public JobKey getIdentity() {
@XmlElement @JsonView(DefaultView.class)
@Transient
protected long getDeviceId() {
if (device == null) {
if (this.device == null) {
return 0;
}
return device.getId();
return this.device.getId();
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,9 @@ public String getTaskDescription() {
@Override
public void prepare(Session session) {
Hibernate.initialize(this.getDeviceGroup());
Hibernate.initialize(this.getDeviceGroup().getCachedDevices());
if (this.getDeviceGroup() != null) {
Hibernate.initialize(this.getDeviceGroup().getCachedDevices());
}
}

/* (non-Javadoc)
Expand All @@ -120,7 +122,12 @@ public void prepare(Session session) {
@Override
public void run() {
log.debug("Task {}. Starting run script task for group {}.",
this.getId(), this.getDeviceGroup().getId());
this.getId(), deviceGroup == null ? "null" : deviceGroup.getId());
if (deviceGroup == null) {
this.info("The device group doesn't exist, the task will be cancelled.");
this.status = Status.CANCELLED;
return;
}
Set<Device> devices = this.getDeviceGroup().getCachedDevices();
log.debug("Task {}. {} devices in the group.", this.getId(), devices.size());
String comment = String.format("Started due to group %s script task", this.getDeviceGroup().getName());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,13 @@ public void prepare(Session session) {

@Override
public void run() {
log.debug("Task {}. Starting script task for device {}.", this.getId(), device.getId());
log.debug("Task {}. Starting script task for device {}.", this.getId(),
device == null ? "null" : device.getId());
if (device == null) {
this.info("The device doesn't exist, the task will be cancelled.");
this.status = Status.CANCELLED;
return;
}
this.info(String.format("Run script task for device %s (%s).",
device.getName(), device.getMgmtAddress().getIp()));

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,10 @@ public String getTaskDescription() {
@XmlElement @JsonView(DefaultView.class)
@Transient
public long getDeviceId() {
return device.getId();
if (this.device == null) {
return 0;
}
return this.device.getId();
}

/* (non-Javadoc)
Expand All @@ -135,8 +138,13 @@ public Object clone() throws CloneNotSupportedException {

@Override
public void run() {
log.debug("Task {}. Starting diagnostic task for device {}.",
this.getId(), device.getId());
log.debug("Task {}. Starting diagnostic task for device {}.", this.getId(),
device == null ? "null" : device.getId());
if (device == null) {
this.info("The device doesn't exist, the task will be cancelled.");
this.status = Status.CANCELLED;
return;
}
this.trace(String.format("Run diagnostic task for device %s (%s).",
device.getName(), device.getMgmtAddress().getIp()));
boolean locked = false;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,9 @@ public String getTaskDescription() {
@Override
public void prepare(Session session) {
Hibernate.initialize(this.getDeviceGroup());
Hibernate.initialize(this.getDeviceGroup().getCachedDevices());
if (this.getDeviceGroup() != null) {
Hibernate.initialize(this.getDeviceGroup().getCachedDevices());
}
}

/* (non-Javadoc)
Expand All @@ -113,7 +115,12 @@ public void prepare(Session session) {
@Override
public void run() {
log.debug("Task {}. Starting diagnostics task for group {}.",
this.getId(), this.getDeviceGroup().getId());
this.getId(), deviceGroup == null ? "null" : deviceGroup.getId());
if (deviceGroup == null) {
this.info("The device group doesn't exist, the task will be cancelled.");
this.status = Status.CANCELLED;
return;
}
Set<Device> devices = this.getDeviceGroup().getCachedDevices();
log.debug("Task {}. {} devices in the group.", this.getId(), devices.size());
String comment = String.format("Started due to group %s diagnotics", this.getDeviceGroup().getName());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,9 @@ public String getTaskDescription() {
@Override
public void prepare(Session session) {
Hibernate.initialize(this.getDeviceGroup());
Hibernate.initialize(this.getDeviceGroup().getCachedDevices());
if (this.getDeviceGroup() != null) {
Hibernate.initialize(this.getDeviceGroup().getCachedDevices());
}
}

/* (non-Javadoc)
Expand All @@ -133,7 +135,12 @@ public void prepare(Session session) {
@Override
public void run() {
log.debug("Task {}. Starting snapshot task for group {}.",
this.getId(), this.getDeviceGroup().getId());
this.getId(), deviceGroup == null ? "null" : deviceGroup.getId());
if (deviceGroup == null) {
this.info("The device group doesn't exist, the task will be cancelled.");
this.status = Status.CANCELLED;
return;
}
Set<Device> devices = this.getDeviceGroup().getCachedDevices();
log.debug("Task {}. {} devices in the group.", this.getId(), devices.size());
String comment = String.format("Started due to group %s snapshot", this.getDeviceGroup().getName());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -194,7 +194,13 @@ public void prepare(Session session) {
*/
@Override
public void run() {
log.debug("Task {}. Starting snapshot task for device {}.", this.getId(), device.getId());
log.debug("Task {}. Starting snapshot task for device {}.", this.getId(),
device == null ? "null" : device.getId());
if (device == null) {
this.info("The device doesn't exist, the task will be cancelled.");
this.status = Status.CANCELLED;
return;
}
this.info(String.format("Snapshot task for device %s (%s).",
device.getName(), device.getMgmtAddress().getIp()));
boolean locked = false;
Expand Down Expand Up @@ -297,6 +303,9 @@ public String getTaskDescription() {
@XmlElement @JsonView(DefaultView.class)
@Transient
protected long getDeviceId() {
if (this.device == null) {
return 0;
}
return device.getId();
}

Expand Down
45 changes: 45 additions & 0 deletions src/main/resources/migration/netshot0.xml
Original file line number Diff line number Diff line change
Expand Up @@ -1324,4 +1324,49 @@
<dropForeignKeyConstraint baseTableName="java_script_rule" constraintName="FK_qxf28k9n9g16igqsqb9v5o4s"/>
<addForeignKeyConstraint baseColumnNames="id" baseTableName="java_script_rule" constraintName="FK_qxf28k9n9g16igqsqb9v5o4s" deferrable="false" initiallyDeferred="false" onDelete="CASCADE" onUpdate="NO ACTION" referencedColumnNames="id" referencedTableName="rule" validate="true"/>
</changeSet>
<changeSet author="netshot (generated)" id="0.20.2_2">
<dropForeignKeyConstraint baseTableName="take_snapshot_task" constraintName="FK_8nwgcvkktkgb0ep9jngl0fhkw"/>
<addForeignKeyConstraint baseColumnNames="device" baseTableName="take_snapshot_task" constraintName="FK_8nwgcvkktkgb0ep9jngl0fhkw" deferrable="false" initiallyDeferred="false" onDelete="SET NULL" onUpdate="NO ACTION" referencedColumnNames="id" referencedTableName="device" validate="true"/>
<dropForeignKeyConstraint baseTableName="run_device_script_task" constraintName="FK_a9huwp5oxt701ovqo18qcfqu8"/>
<addForeignKeyConstraint baseColumnNames="device" baseTableName="run_device_script_task" constraintName="FK_a9huwp5oxt701ovqo18qcfqu8" deferrable="false" initiallyDeferred="false" onDelete="SET NULL" onUpdate="NO ACTION" referencedColumnNames="id" referencedTableName="device" validate="true"/>
<dropForeignKeyConstraint baseTableName="check_compliance_task" constraintName="FK_mmce331srfb7u1u997bs68d1r"/>
<addForeignKeyConstraint baseColumnNames="device" baseTableName="check_compliance_task" constraintName="FK_mmce331srfb7u1u997bs68d1r" deferrable="false" initiallyDeferred="false" onDelete="SET NULL" onUpdate="NO ACTION" referencedColumnNames="id" referencedTableName="device" validate="true"/>
<dropForeignKeyConstraint baseTableName="run_diagnostics_task" constraintName="FK_ok7b5sevyhg72d7gtlaey7gy"/>
<addForeignKeyConstraint baseColumnNames="device" baseTableName="run_diagnostics_task" constraintName="FK_ok7b5sevyhg72d7gtlaey7gy" deferrable="false" initiallyDeferred="false" onDelete="SET NULL" onUpdate="NO ACTION" referencedColumnNames="id" referencedTableName="device" validate="true"/>
<dropForeignKeyConstraint baseTableName="check_group_compliance_task" constraintName="FK_1ritpkb4tkbgfj1ohxvnm72k4"/>
<addForeignKeyConstraint baseColumnNames="device_group" baseTableName="check_group_compliance_task" constraintName="FK_1ritpkb4tkbgfj1ohxvnm72k4" deferrable="false" initiallyDeferred="false" onDelete="SET NULL" onUpdate="NO ACTION" referencedColumnNames="id" referencedTableName="device_group" validate="true"/>
<dropForeignKeyConstraint baseTableName="run_group_diagnostics_task" constraintName="FK_5cyfno9wgsuwo6b8fukyicduq"/>
<addForeignKeyConstraint baseColumnNames="device_group" baseTableName="run_group_diagnostics_task" constraintName="FK_5cyfno9wgsuwo6b8fukyicduq" deferrable="false" initiallyDeferred="false" onDelete="SET NULL" onUpdate="NO ACTION" referencedColumnNames="id" referencedTableName="device_group" validate="true"/>
<dropForeignKeyConstraint baseTableName="take_group_snapshot_task" constraintName="FK_chqgqnpukbtthsxxs7l03c50a"/>
<addForeignKeyConstraint baseColumnNames="device_group" baseTableName="take_group_snapshot_task" constraintName="FK_chqgqnpukbtthsxxs7l03c50a" deferrable="false" initiallyDeferred="false" onDelete="SET NULL" onUpdate="NO ACTION" referencedColumnNames="id" referencedTableName="device_group" validate="true"/>
<dropForeignKeyConstraint baseTableName="run_device_group_script_task" constraintName="FK_h8avh8p102nh9utmk7o9t1ob4"/>
<addForeignKeyConstraint baseColumnNames="device_group" baseTableName="run_device_group_script_task" constraintName="FK_h8avh8p102nh9utmk7o9t1ob4" deferrable="false" initiallyDeferred="false" onDelete="SET NULL" onUpdate="NO ACTION" referencedColumnNames="id" referencedTableName="device_group" validate="true"/>
<dropForeignKeyConstraint baseTableName="check_group_software_task" constraintName="FK_qslc3h936kdd8tyrdtjvp11yu"/>
<addForeignKeyConstraint baseColumnNames="device_group" baseTableName="check_group_software_task" constraintName="FK_qslc3h936kdd8tyrdtjvp11yu" deferrable="false" initiallyDeferred="false" onDelete="SET NULL" onUpdate="NO ACTION" referencedColumnNames="id" referencedTableName="device_group" validate="true"/>
</changeSet>
<changeSet author="netshot (generated)" id="0.20.2_3">
<sql>
DELETE FROM task WHERE id IN (
SELECT t1_0.id from task t1_0
LEFT JOIN check_compliance_task t1_1 on t1_0.id = t1_1.id
LEFT JOIN check_group_compliance_task t1_2 on t1_0.id = t1_2.id
LEFT JOIN check_group_software_task t1_3 on t1_0.id = t1_3.id
LEFT JOIN discover_device_type_task t1_4 on t1_0.id = t1_4.id
LEFT JOIN purge_database_task t1_5 on t1_0.id = t1_5.id
LEFT JOIN run_device_group_script_task t1_6 on t1_0.id = t1_6.id
LEFT JOIN run_device_script_task t1_7 on t1_0.id = t1_7.id
LEFT JOIN run_diagnostics_task t1_8 on t1_0.id = t1_8.id
LEFT JOIN run_group_diagnostics_task t1_9 on t1_0.id = t1_9.id
LEFT JOIN scan_subnets_task t1_10 on t1_0.id = t1_10.id
LEFT JOIN take_group_snapshot_task t1_11 on t1_0.id = t1_11.id
LEFT JOIN take_snapshot_task t1_12 on t1_0.id = t1_12.id
WHERE
t1_1.id is null and t1_2.id is null and t1_3.id is null and
t1_4.id is null and t1_5.id is null and t1_6.id is null and
t1_7.id is null and t1_8.id is null and t1_9.id is null and
t1_10.id is null and t1_11.id is null and t1_12.id is null
)
</sql>
</changeSet>

</databaseChangeLog>
Loading

0 comments on commit eb877ba

Please sign in to comment.