source: trunk/grails-app/services/nl/tno/massSequencing/worker/WorkerService.groovy @ 70

Last change on this file since 70 was 70, checked in by robert@…, 8 years ago
  • Installed templates (in order to extend session lifetime to 2 hours)
  • Implemented background worker to do work outside the HTTP request
File size: 2.1 KB
Line 
1package nl.tno.massSequencing.worker
2
3class WorkerService {
4
5    static transactional = false
6       
7        // Retrieve a reference to the application tag library in order to create links in this service
8        // See also http://jira.grails.org/browse/GRAILS-2605
9        def g = new org.codehaus.groovy.grails.plugins.web.taglib.ApplicationTagLib();
10       
11    /**
12     * Initiates a new process that can perform actions in the background.
13     *
14     * @param httpSession       Reference to the HTTP session, in which progress data is stored
15     * @return
16     */
17        public String initProcess( def httpSession, String description = "Please wait while processing your request", Number numSteps = 1, Number stepTotal = 0 ) {
18                String processId = UUID.randomUUID().toString();
19               
20                // Save process entry in session
21                if( !httpSession.process )
22                        httpSession.process = [:]
23                       
24                if( !httpSession.process[ processId ] )
25                        httpSession.process[ processId ] = [:]
26                         
27                // Save progress information in session
28                if( !httpSession.progress )
29                        httpSession.progress = [:]
30               
31                httpSession.progress[ processId ] = [
32                        stepNum: 1,
33                        numSteps: numSteps,
34                        stepDescription: description,   // Second step is Store classification
35                       
36                        stepProgress: 0,
37                        stepTotal: stepTotal,
38                        finished: false,
39                        error: false,
40                        errorMessage: ""
41                ]
42               
43                return processId;
44    }
45       
46        /**
47         * Start the process by showing a 'waiting' screen to the user and calling the processURL
48         * @param processId     
49         * @return              Url to redirect the user to
50         */
51        public String startProcess( def httpSession, String processId, String finishUrl, String errorUrl = null, String returnUrl = null ) {
52                // Store data in HTTP session
53                httpSession.process[ processId ][ "worker" ] = [
54                        "returnUrl": returnUrl ?: "",
55                        "progressUrl": g.createLink( controller: "worker", action: "getProgress", params: [ "processId": processId ] ),
56                        "finishUrl": finishUrl,
57                        "errorUrl": ( errorUrl ?: finishUrl )
58                ];
59
60                return g.createLink( controller: "worker", action: "process", params: [ "processId": processId ] );
61        }
62       
63        public void clearProcess( def httpSession, String processId ) {
64                httpSession.process?.remove( processId );
65                httpSession.progress?.remove( processId );
66        }
67}
Note: See TracBrowser for help on using the repository browser.