1616// under the License.
1717package org .apache .cloudstack .framework .jobs .dao ;
1818
19+ import java .sql .PreparedStatement ;
20+ import java .sql .SQLException ;
1921import java .util .Date ;
2022import java .util .List ;
23+ import java .util .TimeZone ;
2124
2225import javax .annotation .PostConstruct ;
2326
3134import com .cloud .utils .db .SearchBuilder ;
3235import com .cloud .utils .db .SearchCriteria ;
3336import com .cloud .utils .db .SearchCriteria .Op ;
37+ import com .cloud .utils .db .Transaction ;
38+ import com .cloud .utils .db .TransactionCallbackNoReturn ;
39+ import com .cloud .utils .db .TransactionLegacy ;
40+ import com .cloud .utils .db .TransactionStatus ;
3441import com .cloud .vm .VirtualMachine ;
3542
3643public class VmWorkJobDaoImpl extends GenericDaoBase <VmWorkJobVO , Long > implements VmWorkJobDao {
3744
3845 protected SearchBuilder <VmWorkJobVO > PendingWorkJobSearch ;
3946 protected SearchBuilder <VmWorkJobVO > PendingWorkJobByCommandSearch ;
40- protected SearchBuilder <VmWorkJobVO > ExpungeWorkJobSearch ;
41-
47+
4248 public VmWorkJobDaoImpl () {
4349 }
44-
50+
4551 @ PostConstruct
4652 public void init () {
4753 PendingWorkJobSearch = createSearchBuilder ();
4854 PendingWorkJobSearch .and ("jobStatus" , PendingWorkJobSearch .entity ().getStatus (), Op .EQ );
4955 PendingWorkJobSearch .and ("vmType" , PendingWorkJobSearch .entity ().getVmType (), Op .EQ );
5056 PendingWorkJobSearch .and ("vmInstanceId" , PendingWorkJobSearch .entity ().getVmInstanceId (), Op .EQ );
51- PendingWorkJobSearch .and ("step" , PendingWorkJobSearch .entity ().getStep (), Op .NEQ );
5257 PendingWorkJobSearch .done ();
5358
5459 PendingWorkJobByCommandSearch = createSearchBuilder ();
@@ -58,68 +63,132 @@ public void init() {
5863 PendingWorkJobByCommandSearch .and ("step" , PendingWorkJobByCommandSearch .entity ().getStep (), Op .NEQ );
5964 PendingWorkJobByCommandSearch .and ("cmd" , PendingWorkJobByCommandSearch .entity ().getCmd (), Op .EQ );
6065 PendingWorkJobByCommandSearch .done ();
61-
62- ExpungeWorkJobSearch = createSearchBuilder ();
63- ExpungeWorkJobSearch .and ("lastUpdated" , ExpungeWorkJobSearch .entity ().getLastUpdated (), Op .LT );
64- ExpungeWorkJobSearch .and ("jobStatus" , ExpungeWorkJobSearch .entity ().getStatus (), Op .NEQ );
65- ExpungeWorkJobSearch .done ();
6666 }
67-
67+
6868 @ Override
6969 public VmWorkJobVO findPendingWorkJob (VirtualMachine .Type type , long instanceId ) {
70-
70+
7171 SearchCriteria <VmWorkJobVO > sc = PendingWorkJobSearch .create ();
7272 sc .setParameters ("jobStatus" , JobInfo . Status .IN_PROGRESS );
7373 sc .setParameters ("vmType" , type );
7474 sc .setParameters ("vmInstanceId" , instanceId );
75-
75+
7676 Filter filter = new Filter (VmWorkJobVO .class , "created" , true , null , null );
7777 List <VmWorkJobVO > result = this .listBy (sc , filter );
7878 if (result != null && result .size () > 0 )
7979 return result .get (0 );
80-
80+
8181 return null ;
8282 }
83-
83+
8484 @ Override
8585 public List <VmWorkJobVO > listPendingWorkJobs (VirtualMachine .Type type , long instanceId ) {
86-
86+
8787 SearchCriteria <VmWorkJobVO > sc = PendingWorkJobSearch .create ();
8888 sc .setParameters ("jobStatus" , JobInfo .Status .IN_PROGRESS );
8989 sc .setParameters ("vmType" , type );
9090 sc .setParameters ("vmInstanceId" , instanceId );
91-
91+
9292 Filter filter = new Filter (VmWorkJobVO .class , "created" , true , null , null );
9393 return this .listBy (sc , filter );
9494 }
9595
9696 @ Override
9797 public List <VmWorkJobVO > listPendingWorkJobs (VirtualMachine .Type type , long instanceId , String jobCmd ) {
98-
98+
9999 SearchCriteria <VmWorkJobVO > sc = PendingWorkJobByCommandSearch .create ();
100100 sc .setParameters ("jobStatus" , JobInfo .Status .IN_PROGRESS );
101101 sc .setParameters ("vmType" , type );
102102 sc .setParameters ("vmInstanceId" , instanceId );
103103 sc .setParameters ("cmd" , jobCmd );
104-
104+
105105 Filter filter = new Filter (VmWorkJobVO .class , "created" , true , null , null );
106106 return this .listBy (sc , filter );
107107 }
108-
108+
109109 @ Override
110110 public void updateStep (long workJobId , Step step ) {
111111 VmWorkJobVO jobVo = findById (workJobId );
112112 jobVo .setStep (step );
113113 jobVo .setLastUpdated (DateUtil .currentGMTTime ());
114114 update (workJobId , jobVo );
115115 }
116-
116+
117117 @ Override
118- public void expungeCompletedWorkJobs (Date cutDate ) {
119- SearchCriteria <VmWorkJobVO > sc = ExpungeWorkJobSearch .create ();
120- sc .setParameters ("lastUpdated" ,cutDate );
121- sc .setParameters ("jobStatus" , JobInfo .Status .IN_PROGRESS );
122-
123- expunge (sc );
118+ public void expungeCompletedWorkJobs (final Date cutDate ) {
119+ // current DAO machenism does not support following usage
120+ /*
121+ SearchCriteria<VmWorkJobVO> sc = ExpungeWorkJobSearch.create();
122+ sc.setParameters("lastUpdated",cutDate);
123+ sc.setParameters("jobStatus", JobInfo.Status.IN_PROGRESS);
124+
125+ expunge(sc);
126+ */
127+ Transaction .execute (new TransactionCallbackNoReturn () {
128+ @ Override
129+ public void doInTransactionWithoutResult (TransactionStatus status ) {
130+ TransactionLegacy txn = TransactionLegacy .currentTxn ();
131+
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+ });
124154 }
155+
156+ @ Override
157+ public void expungeLeftoverWorkJobs (final long msid ) {
158+ // current DAO machenism does not support following usage
159+ /*
160+ SearchCriteria<VmWorkJobVO> sc = ExpungePlaceHolderWorkJobSearch.create();
161+ sc.setParameters("dispatcher", "VmWorkJobPlaceHolder");
162+ sc.setParameters("msid", msid);
163+
164+ expunge(sc);
165+ */
166+ Transaction .execute (new TransactionCallbackNoReturn () {
167+ @ Override
168+ public void doInTransactionWithoutResult (TransactionStatus status ) {
169+ TransactionLegacy txn = TransactionLegacy .currentTxn ();
170+
171+ PreparedStatement pstmt = null ;
172+ try {
173+ pstmt = txn .prepareAutoCloseStatement (
174+ "DELETE FROM vm_work_job WHERE id IN (SELECT id FROM async_job WHERE (job_dispatcher='VmWorkJobPlaceHolder' OR job_dispatcher='VmWorkJobDispatcher') AND job_init_msid=?)" );
175+ pstmt .setLong (1 , msid );
176+
177+ pstmt .execute ();
178+ } catch (SQLException e ) {
179+ } catch (Throwable e ) {
180+ }
181+
182+ try {
183+ pstmt = txn .prepareAutoCloseStatement (
184+ "DELETE FROM async_job WHERE (job_dispatcher='VmWorkJobPlaceHolder' OR job_dispatcher='VmWorkJobDispatcher') AND job_init_msid=?" );
185+ pstmt .setLong (1 , msid );
186+
187+ pstmt .execute ();
188+ } catch (SQLException e ) {
189+ } catch (Throwable e ) {
190+ }
191+ }
192+ });
193+ }
125194}
0 commit comments