2020import java .sql .SQLException ;
2121import java .util .Date ;
2222import java .util .List ;
23- import java .util .TimeZone ;
24-
2523import javax .annotation .PostConstruct ;
24+ import javax .inject .Inject ;
25+
26+ import org .apache .log4j .Logger ;
2627
2728import org .apache .cloudstack .framework .jobs .impl .VmWorkJobVO ;
2829import org .apache .cloudstack .framework .jobs .impl .VmWorkJobVO .Step ;
4142import com .cloud .vm .VirtualMachine ;
4243
4344public class VmWorkJobDaoImpl extends GenericDaoBase <VmWorkJobVO , Long > implements VmWorkJobDao {
45+ private static final Logger s_logger = Logger .getLogger (VmWorkJobDaoImpl .class );
4446
4547 protected SearchBuilder <VmWorkJobVO > PendingWorkJobSearch ;
4648 protected SearchBuilder <VmWorkJobVO > PendingWorkJobByCommandSearch ;
49+ protected SearchBuilder <VmWorkJobVO > ExpungingWorkJobSearch ;
50+
51+ @ Inject
52+ protected AsyncJobDao _baseJobDao ;
4753
4854 public VmWorkJobDaoImpl () {
4955 }
@@ -63,6 +69,12 @@ public void init() {
6369 PendingWorkJobByCommandSearch .and ("step" , PendingWorkJobByCommandSearch .entity ().getStep (), Op .NEQ );
6470 PendingWorkJobByCommandSearch .and ("cmd" , PendingWorkJobByCommandSearch .entity ().getCmd (), Op .EQ );
6571 PendingWorkJobByCommandSearch .done ();
72+
73+ ExpungingWorkJobSearch = createSearchBuilder ();
74+ ExpungingWorkJobSearch .and ("jobStatus" , ExpungingWorkJobSearch .entity ().getStatus (), Op .NEQ );
75+ ExpungingWorkJobSearch .and ("cutDate" , ExpungingWorkJobSearch .entity ().getLastUpdated (), Op .LT );
76+ ExpungingWorkJobSearch .and ("dispatcher" , ExpungingWorkJobSearch .entity ().getDispatcher (), Op .EQ );
77+ ExpungingWorkJobSearch .done ();
6678 }
6779
6880 @ Override
@@ -124,33 +136,19 @@ public void expungeCompletedWorkJobs(final Date cutDate) {
124136
125137 expunge(sc);
126138 */
127- Transaction .execute (new TransactionCallbackNoReturn () {
128- @ Override
129- public void doInTransactionWithoutResult (TransactionStatus status ) {
130- TransactionLegacy txn = TransactionLegacy .currentTxn ();
131139
132- PreparedStatement pstmt = null ;
133- try {
134- pstmt = txn .prepareAutoCloseStatement (
135- "DELETE FROM vm_work_job WHERE id IN (SELECT id FROM async_job WHERE job_dispatcher='VmWorkJobDispatcher' AND job_status != 0 AND last_updated < ?)" );
136- pstmt .setString (1 , DateUtil .getDateDisplayString (TimeZone .getTimeZone ("GMT" ), cutDate ));
137-
138- pstmt .execute ();
139- } catch (SQLException e ) {
140- } catch (Throwable e ) {
141- }
142-
143- try {
144- pstmt = txn .prepareAutoCloseStatement (
145- "DELETE FROM async_job WHERE job_dispatcher='VmWorkJobDispatcher' AND job_status != 0 AND last_updated < ?" );
146- pstmt .setString (1 , DateUtil .getDateDisplayString (TimeZone .getTimeZone ("GMT" ), cutDate ));
147-
148- pstmt .execute ();
149- } catch (SQLException e ) {
150- } catch (Throwable e ) {
151- }
152- }
153- });
140+ // loop at application level to avoid mysql deadlock issues
141+ SearchCriteria <VmWorkJobVO > sc = ExpungingWorkJobSearch .create ();
142+ sc .setParameters ("jobStatus" , JobInfo .Status .IN_PROGRESS );
143+ sc .setParameters ("lastUpdated" , cutDate );
144+ sc .setParameters ("dispatcher" , "VmWorkJobDispatcher" );
145+ List <VmWorkJobVO > expungeList = listBy (sc );
146+ for (VmWorkJobVO job : expungeList ) {
147+ if (s_logger .isDebugEnabled ())
148+ s_logger .debug ("Expunge completed work job-" + job .getId ());
149+ expunge (job .getId ());
150+ _baseJobDao .expunge (job .getId ());
151+ }
154152 }
155153
156154 @ Override
0 commit comments