Updated Branches:
  refs/heads/master fc9eb2ab6 -> daa06a0ff

implementing artifact update task in cartridge agent - STRATOS-368


Project: http://git-wip-us.apache.org/repos/asf/incubator-stratos/repo
Commit: http://git-wip-us.apache.org/repos/asf/incubator-stratos/commit/f4a82d1e
Tree: http://git-wip-us.apache.org/repos/asf/incubator-stratos/tree/f4a82d1e
Diff: http://git-wip-us.apache.org/repos/asf/incubator-stratos/diff/f4a82d1e

Branch: refs/heads/master
Commit: f4a82d1e973e491608743b2475ecb6b09ea5c802
Parents: 61caa76
Author: Isuru <[email protected]>
Authored: Fri Jan 17 15:10:53 2014 +0530
Committer: Isuru <[email protected]>
Committed: Fri Jan 17 15:10:53 2014 +0530

----------------------------------------------------------------------
 .../org.apache.stratos.cartridge.agent/pom.xml  |  10 +-
 .../stratos/cartridge/agent/CartridgeAgent.java |   8 +-
 .../cartridge/agent/RepositoryFileListener.java |   2 +-
 .../git/impl/GitBasedArtifactRepository.java    | 123 +++++++++++++++----
 .../git/internal/RepositoryContext.java         |  11 +-
 5 files changed, 118 insertions(+), 36 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/incubator-stratos/blob/f4a82d1e/components/org.apache.stratos.cartridge.agent/pom.xml
----------------------------------------------------------------------
diff --git a/components/org.apache.stratos.cartridge.agent/pom.xml 
b/components/org.apache.stratos.cartridge.agent/pom.xml
index 064fc94..fad44db 100644
--- a/components/org.apache.stratos.cartridge.agent/pom.xml
+++ b/components/org.apache.stratos.cartridge.agent/pom.xml
@@ -70,14 +70,10 @@
             <version>2.0</version>
         </dependency>
         <dependency>
-            <groupId>org.eclipse.jgit</groupId>
+            <groupId>org.eclipse.jgit.wso2</groupId>
             <artifactId>org.eclipse.jgit</artifactId>
-            <version>2.1.0.wso2v1</version>
-        </dependency>
-        <dependency>
-            <groupId>org.eclipse.jgit</groupId>
-            <artifactId>org.eclipse.jgit</artifactId>
-            <version>2.1.0.wso2v1</version>
+            <!--version>2.1.0.wso2v1</version-->
+            <version>2.3.1.wso2v2</version>
         </dependency>
         <dependency>
             <groupId>com.jcraft</groupId>

http://git-wip-us.apache.org/repos/asf/incubator-stratos/blob/f4a82d1e/components/org.apache.stratos.cartridge.agent/src/main/java/org/apache/stratos/cartridge/agent/CartridgeAgent.java
----------------------------------------------------------------------
diff --git 
a/components/org.apache.stratos.cartridge.agent/src/main/java/org/apache/stratos/cartridge/agent/CartridgeAgent.java
 
b/components/org.apache.stratos.cartridge.agent/src/main/java/org/apache/stratos/cartridge/agent/CartridgeAgent.java
index 02c0d0e..0e35bd6 100644
--- 
a/components/org.apache.stratos.cartridge.agent/src/main/java/org/apache/stratos/cartridge/agent/CartridgeAgent.java
+++ 
b/components/org.apache.stratos.cartridge.agent/src/main/java/org/apache/stratos/cartridge/agent/CartridgeAgent.java
@@ -159,8 +159,8 @@ public class CartridgeAgent implements Runnable {
             repoInformation.setRepoUrl(repoURL);
             repoInformation.setRepoPath(localRepoPath);
             repoInformation.setTenantId(tenantId);
-            boolean cloneExists = 
GitBasedArtifactRepository.cloneExists(repoInformation);
-            GitBasedArtifactRepository.checkout(repoInformation);
+            boolean cloneExists = 
GitBasedArtifactRepository.getInstance().cloneExists(repoInformation);
+            GitBasedArtifactRepository.getInstance().checkout(repoInformation);
 
             ExtensionUtils.executeArtifactsUpdatedExtension();
 
@@ -168,6 +168,10 @@ public class CartridgeAgent implements Runnable {
                 // Executed git clone, publish instance activated event
                 CartridgeAgentEventPublisher.publishInstanceActivatedEvent();
             }
+
+            // Start the artifact update task
+            
GitBasedArtifactRepository.getInstance().scheduleSyncTask(repoInformation, 10);
+
         }
     }
 

http://git-wip-us.apache.org/repos/asf/incubator-stratos/blob/f4a82d1e/components/org.apache.stratos.cartridge.agent/src/main/java/org/apache/stratos/cartridge/agent/RepositoryFileListener.java
----------------------------------------------------------------------
diff --git 
a/components/org.apache.stratos.cartridge.agent/src/main/java/org/apache/stratos/cartridge/agent/RepositoryFileListener.java
 
b/components/org.apache.stratos.cartridge.agent/src/main/java/org/apache/stratos/cartridge/agent/RepositoryFileListener.java
index c6e53b5..77c844c 100644
--- 
a/components/org.apache.stratos.cartridge.agent/src/main/java/org/apache/stratos/cartridge/agent/RepositoryFileListener.java
+++ 
b/components/org.apache.stratos.cartridge.agent/src/main/java/org/apache/stratos/cartridge/agent/RepositoryFileListener.java
@@ -32,7 +32,7 @@ public class RepositoryFileListener implements Runnable {
         if(log.isInfoEnabled()) {
                    log.info("Executing repository file listener");
         }
-               boolean commitStatus = GitBasedArtifactRepository.commit();
+               boolean commitStatus = 
GitBasedArtifactRepository.getInstance().commit();
         if(log.isInfoEnabled()) {
                    log.info("Commit status: " + commitStatus);
         }

http://git-wip-us.apache.org/repos/asf/incubator-stratos/blob/f4a82d1e/components/org.apache.stratos.cartridge.agent/src/main/java/org/apache/stratos/cartridge/agent/artifact/deployment/synchronizer/git/impl/GitBasedArtifactRepository.java
----------------------------------------------------------------------
diff --git 
a/components/org.apache.stratos.cartridge.agent/src/main/java/org/apache/stratos/cartridge/agent/artifact/deployment/synchronizer/git/impl/GitBasedArtifactRepository.java
 
b/components/org.apache.stratos.cartridge.agent/src/main/java/org/apache/stratos/cartridge/agent/artifact/deployment/synchronizer/git/impl/GitBasedArtifactRepository.java
index fcd6a83..f5d658d 100644
--- 
a/components/org.apache.stratos.cartridge.agent/src/main/java/org/apache/stratos/cartridge/agent/artifact/deployment/synchronizer/git/impl/GitBasedArtifactRepository.java
+++ 
b/components/org.apache.stratos.cartridge.agent/src/main/java/org/apache/stratos/cartridge/agent/artifact/deployment/synchronizer/git/impl/GitBasedArtifactRepository.java
@@ -39,7 +39,7 @@ import java.io.IOException;
 import java.util.Iterator;
 import java.util.Map.Entry;
 import java.util.Set;
-import java.util.concurrent.ConcurrentHashMap;
+import java.util.concurrent.*;
 
 /**
  * Git based artifact repository.
@@ -53,18 +53,31 @@ public class GitBasedArtifactRepository {
     //Map to keep track of git context per tenant (remote urls, jgit git 
objects, etc.)
     private static ConcurrentHashMap<Integer, RepositoryContext>
                                        tenantToRepoContextMap = new 
ConcurrentHashMap<Integer, RepositoryContext>();
+    private static volatile GitBasedArtifactRepository 
gitBasedArtifactRepository;
 
     private GitBasedArtifactRepository () {
 
     }
 
+    public static GitBasedArtifactRepository getInstance () {
+
+        if (gitBasedArtifactRepository == null) {
+            synchronized (GitBasedArtifactRepository.class) {
+                if (gitBasedArtifactRepository == null) {
+                    gitBasedArtifactRepository = new 
GitBasedArtifactRepository();
+                }
+            }
+        }
+        return gitBasedArtifactRepository;
+    }
+
     /**
      * initializes and populates the git context with relevant data
      *
      * @param repositoryInformation id of the tenant
      *
      */
-    private static void initGitContext (RepositoryInformation 
repositoryInformation)  {
+    private void initGitContext (RepositoryInformation repositoryInformation)  
{
 
      /*   if (tenantId == GitDeploymentSynchronizerConstants.SUPER_TENANT_ID)
             return;*/
@@ -121,7 +134,7 @@ public class GitBasedArtifactRepository {
      *
      * @return true if SSH authentication is required, else false
      */
-    private static boolean isKeyBasedAuthentication(String url, int tenantId) {
+    private boolean isKeyBasedAuthentication(String url, int tenantId) {
 
         if 
(url.startsWith(GitDeploymentSynchronizerConstants.GIT_HTTP_REPO_URL_PREFIX) ||
                 
url.startsWith(GitDeploymentSynchronizerConstants.GIT_HTTPS_REPO_URL_PREFIX)) 
{//http or https url
@@ -149,7 +162,7 @@ public class GitBasedArtifactRepository {
     /**
      * Initializes SSH authentication
      */
-    private static void initSSHAuthentication () {
+    private void initSSHAuthentication () {
 
         SshSessionFactory.setInstance(new CustomJschConfigSessionFactory());
     }
@@ -160,7 +173,7 @@ public class GitBasedArtifactRepository {
      * @param tenantId tenant repository path
      * @param gitRepoCtx RepositoryContext instance for tenant
      */
-    private static void cacheGitRepoContext(int tenantId, RepositoryContext 
gitRepoCtx) {
+    private void cacheGitRepoContext(int tenantId, RepositoryContext 
gitRepoCtx) {
 
        log.info("caching repo context");
         tenantToRepoContextMap.put(tenantId, gitRepoCtx);
@@ -174,7 +187,7 @@ public class GitBasedArtifactRepository {
      * @return corresponding RepositoryContext instance for the
      * tenant's local repo if available, else null
      */
-    private static RepositoryContext retrieveCachedGitContext (int tenantId) {
+    private RepositoryContext retrieveCachedGitContext (int tenantId) {
 
         return tenantToRepoContextMap.get(tenantId);
     }
@@ -185,7 +198,7 @@ public class GitBasedArtifactRepository {
      * @return
      *      
      */
-    public static boolean commit() {
+    public boolean commit() {
        // TODO implement later, this is applicable for management node.
 
                for (Entry<Integer, RepositoryContext> tenantMap : 
tenantToRepoContextMap
@@ -242,7 +255,7 @@ public class GitBasedArtifactRepository {
      *
      * @return artifact names set
      */
-    private static Set<String> getNewArtifacts (Status status) {
+    private Set<String> getNewArtifacts (Status status) {
 
         return status.getUntracked();
     }
@@ -254,7 +267,7 @@ public class GitBasedArtifactRepository {
      *
      * @return artifact names set
      */
-    private static Set<String> getRemovedArtifacts (Status status) {
+    private Set<String> getRemovedArtifacts (Status status) {
 
         return status.getMissing();
     }
@@ -266,7 +279,7 @@ public class GitBasedArtifactRepository {
      *
      * @return artifact names set
      */
-    private static Set<String> getModifiedArtifacts (Status status) {
+    private Set<String> getModifiedArtifacts (Status status) {
 
         return status.getModified();
     }
@@ -277,7 +290,7 @@ public class GitBasedArtifactRepository {
      * @param gitRepoCtx RepositoryContext instance
      * @param artifacts set of artifacts
      */
-    private static void addArtifacts (RepositoryContext gitRepoCtx, 
Set<String> artifacts) {
+    private void addArtifacts (RepositoryContext gitRepoCtx, Set<String> 
artifacts) {
 
         if(artifacts.isEmpty())
             return;
@@ -302,7 +315,7 @@ public class GitBasedArtifactRepository {
      * @param gitRepoCtx RepositoryContext instance
      * @param artifacts Set of artifact names to remove
      */
-    private static void removeArtifacts (RepositoryContext gitRepoCtx, 
Set<String> artifacts) {
+    private void removeArtifacts (RepositoryContext gitRepoCtx, Set<String> 
artifacts) {
 
         if(artifacts.isEmpty())
             return;
@@ -327,7 +340,7 @@ public class GitBasedArtifactRepository {
      *
      * @param gitRepoCtx RepositoryContext instance for the tenant
      */
-    private static void commitToLocalRepo (RepositoryContext gitRepoCtx) {
+    private void commitToLocalRepo (RepositoryContext gitRepoCtx) {
 
         CommitCommand commitCmd = gitRepoCtx.getGit().commit();
         commitCmd.setMessage("tenant " + gitRepoCtx.getTenantId() + "'s 
artifacts committed to local repo at " +
@@ -347,7 +360,7 @@ public class GitBasedArtifactRepository {
      *
      * @param gitRepoCtx RepositoryContext instance for the tenant
      */
-    private static void pushToRemoteRepo(RepositoryContext gitRepoCtx) {
+    private void pushToRemoteRepo(RepositoryContext gitRepoCtx) {
 
         PushCommand pushCmd = gitRepoCtx.getGit().push();
         if(!gitRepoCtx.getKeyBasedAuthentication()) {
@@ -365,10 +378,16 @@ public class GitBasedArtifactRepository {
         }
     }
 
-    public static boolean checkout(RepositoryInformation 
repositoryInformation) {
-        if(log.isInfoEnabled()) {
+    public boolean checkout(RepositoryInformation repositoryInformation) {
+        /*if(log.isInfoEnabled()) {
            log.info("Executing checkout");
+        }*/
+
+        if (log.isDebugEnabled()) {
+            log.debug("Artifact checkout done by thread " + 
Thread.currentThread().getName() + " - " +
+                Thread.currentThread().getId());
         }
+
        int tenantId = Integer.parseInt(repositoryInformation.getTenantId());
        
        // if context for tenant is not initialized
@@ -384,17 +403,46 @@ public class GitBasedArtifactRepository {
             return true;
         }
 
-        /*if(gitRepoCtx.getTenantId() == 
GitDeploymentSynchronizerConstants.SUPER_TENANT_ID)
-            return true;  */
-        if(!gitRepoCtx.cloneExists())
-            cloneRepository(gitRepoCtx);
+        synchronized (gitRepoCtx) {
+            if(!gitRepoCtx.cloneExists())
+                cloneRepository(gitRepoCtx);
 
-        return pullArtifacts(gitRepoCtx);
+            return pullArtifacts(gitRepoCtx);
+        }
     }
     
+    public void scheduleSyncTask (RepositoryInformation repoInformation, long 
delay) {
+
+        int tenantId = Integer.parseInt(repoInformation.getTenantId());
+
+        RepositoryContext repoCtxt = tenantToRepoContextMap.get(tenantId);
+        if (repoCtxt == null) {
+            log.error("Unable to schedule artifact sync task, 
repositoryContext null for tenant " + tenantId);
+            return;
+        }
+
+        if (repoCtxt.getArtifactSyncSchedular() == null) {
+           synchronized (repoCtxt) {
+               if (repoCtxt.getArtifactSyncSchedular() == null) {
+                   // create a new ScheduledExecutorService instance
+                   final ScheduledExecutorService artifactSyncScheduler = 
Executors.newScheduledThreadPool(1,
+                           new ArtifactSyncTaskThreadFactory(repoInformation));
+
+                   // schedule at the given interval
+                   artifactSyncScheduler.scheduleAtFixedRate(new 
ArtifactSyncTask(repoInformation), delay, delay, TimeUnit.SECONDS);
+                   // cache
+                   repoCtxt.setArtifactSyncSchedular(artifactSyncScheduler);
+
+                   log.info("Scheduled Artifact Synchronization Task for path 
" + repoCtxt.getGitLocalRepoPath());
+
+               } else {
+                   log.info("Artifact Synchronization Task for path " + 
repoCtxt.getGitLocalRepoPath() + " already scheduled");
+               }
+           }
+        }
+    }
     
-    
-    public static boolean cloneExists(RepositoryInformation 
repositoryInformation) {
+    public boolean cloneExists(RepositoryInformation repositoryInformation) {
        
        int tenantId = Integer.parseInt(repositoryInformation.getTenantId());
        
@@ -421,7 +469,7 @@ public class GitBasedArtifactRepository {
      *
      * @return true if success, else false
      */
-    private static boolean pullArtifacts (RepositoryContext gitRepoCtx) {
+    private boolean pullArtifacts (RepositoryContext gitRepoCtx) {
         if(log.isInfoEnabled())  {
            log.info("Pulling artifacts");
         }
@@ -674,6 +722,31 @@ public class GitBasedArtifactRepository {
      /*   return false;
     }*/
 
-       
+       private class ArtifactSyncTask implements Runnable {
+
+        private RepositoryInformation repositoryInformation;
+
+        public ArtifactSyncTask (RepositoryInformation repositoryInformation) {
+            this.repositoryInformation = repositoryInformation;
+        }
+
+        @Override
+        public void run() {
+            checkout(repositoryInformation);
+        }
+    }
+
+    class ArtifactSyncTaskThreadFactory implements ThreadFactory {
+
+        private RepositoryInformation repositoryInformation;
+
+        public ArtifactSyncTaskThreadFactory (RepositoryInformation 
repositoryInformation) {
+            this.repositoryInformation = repositoryInformation;
+        }
+
+        public Thread newThread(Runnable r) {
+            return new Thread(r, "Artifact Update Thread - " + 
repositoryInformation.getRepoPath());
+        }
+    }
 
 }

http://git-wip-us.apache.org/repos/asf/incubator-stratos/blob/f4a82d1e/components/org.apache.stratos.cartridge.agent/src/main/java/org/apache/stratos/cartridge/agent/artifact/deployment/synchronizer/git/internal/RepositoryContext.java
----------------------------------------------------------------------
diff --git 
a/components/org.apache.stratos.cartridge.agent/src/main/java/org/apache/stratos/cartridge/agent/artifact/deployment/synchronizer/git/internal/RepositoryContext.java
 
b/components/org.apache.stratos.cartridge.agent/src/main/java/org/apache/stratos/cartridge/agent/artifact/deployment/synchronizer/git/internal/RepositoryContext.java
index c769a4b..f91a3f3 100644
--- 
a/components/org.apache.stratos.cartridge.agent/src/main/java/org/apache/stratos/cartridge/agent/artifact/deployment/synchronizer/git/internal/RepositoryContext.java
+++ 
b/components/org.apache.stratos.cartridge.agent/src/main/java/org/apache/stratos/cartridge/agent/artifact/deployment/synchronizer/git/internal/RepositoryContext.java
@@ -25,6 +25,7 @@ import org.eclipse.jgit.api.Git;
 import org.eclipse.jgit.lib.Repository;
 
 import java.io.File;
+import java.util.concurrent.ScheduledExecutorService;
 
 /**
  * Git Repository Context class. Keeps track of git configurations per tenant.
@@ -43,6 +44,7 @@ public class RepositoryContext {
     private boolean keyBasedAuthentication;
     private String repoUsername;
     private String repoPassword;
+    private ScheduledExecutorService artifactSyncSchedular;
 
     public RepositoryContext () {
 
@@ -127,5 +129,12 @@ public class RepositoryContext {
        public void setRepoPassword(String repoPassword) {
                this.repoPassword = repoPassword;
        }
-    
+
+    public ScheduledExecutorService getArtifactSyncSchedular() {
+        return artifactSyncSchedular;
+    }
+
+    public void setArtifactSyncSchedular(ScheduledExecutorService 
artifactSyncSchedular) {
+        this.artifactSyncSchedular = artifactSyncSchedular;
+    }
 }

Reply via email to