001/**
002 *
003 * Copyright (c) 2014, the Railo Company Ltd. All rights reserved.
004 *
005 * This library is free software; you can redistribute it and/or
006 * modify it under the terms of the GNU Lesser General Public
007 * License as published by the Free Software Foundation; either 
008 * version 2.1 of the License, or (at your option) any later version.
009 * 
010 * This library is distributed in the hope that it will be useful,
011 * but WITHOUT ANY WARRANTY; without even the implied warranty of
012 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
013 * Lesser General Public License for more details.
014 * 
015 * You should have received a copy of the GNU Lesser General Public 
016 * License along with this library.  If not, see <http://www.gnu.org/licenses/>.
017 * 
018 **/
019package lucee.runtime.spooler;
020
021import lucee.commons.lang.ExceptionUtil;
022import lucee.runtime.config.Config;
023import lucee.runtime.exp.PageException;
024import lucee.runtime.op.Caster;
025import lucee.runtime.type.Array;
026import lucee.runtime.type.ArrayImpl;
027import lucee.runtime.type.Struct;
028import lucee.runtime.type.StructImpl;
029
030
031
032public abstract class SpoolerTaskSupport implements SpoolerTask {
033
034        private long creation;
035        private long lastExecution;
036        private int tries=0;
037        private long nextExecution;
038        private Array exceptions=new ArrayImpl();
039        private boolean closed;
040        private String id;
041        private ExecutionPlan[] plans;
042        
043        /**
044         * Constructor of the class
045         * @param plans
046         * @param timeOffset offset from the local time to the config time
047         */
048        public SpoolerTaskSupport(ExecutionPlan[] plans, long nextExecution) {
049                this.plans=plans;
050                creation=System.currentTimeMillis();
051
052                if (nextExecution > 0)
053                        this.nextExecution = nextExecution;
054        }
055
056        public SpoolerTaskSupport(ExecutionPlan[] plans) {
057
058                this(plans, 0);
059        }
060        
061        public final String getId() {
062                return id;
063        }
064        public final void setId(String id) {
065                this.id= id;
066        }
067
068        /**
069         * return last execution of this task
070         * @return last execution
071         */
072        public final long lastExecution() {
073                return lastExecution;
074        }
075
076        public final void setNextExecution(long nextExecution) {
077                this.nextExecution=nextExecution;
078        }
079
080        public final long nextExecution() {
081                return nextExecution;
082        }
083
084        /**
085         * returns how many tries to send are already done
086         * @return tries
087         */
088        public final int tries() {
089                return tries;
090        }
091
092        final void _execute(Config config) throws PageException {
093
094                lastExecution=System.currentTimeMillis();
095                tries++;
096                try {
097                        execute(config);
098                }
099                catch(Throwable t) {
100                        PageException pe = Caster.toPageException(t);
101                        String st = ExceptionUtil.getStacktrace(t,true);
102                        //config.getErrWriter().write(st+"\n");
103                        
104                        Struct sct=new StructImpl();
105                        sct.setEL("message", pe.getMessage());
106                        sct.setEL("detail", pe.getDetail());
107                        sct.setEL("stacktrace", st);
108                        sct.setEL("time", Caster.toLong(System.currentTimeMillis()));
109                        exceptions.appendEL(sct);
110                        
111                        throw pe;
112                }
113                finally {
114                        lastExecution=System.currentTimeMillis();
115                }
116        }
117
118        /**
119         * @return the exceptions
120         */
121        public final Array getExceptions() {
122                return exceptions;
123        }
124
125        public final void setClosed(boolean closed) {
126                this.closed=closed;
127        }
128        
129        public final boolean closed() {
130                return closed;
131        }
132
133
134        /**
135         * @return the plans
136         */
137        public ExecutionPlan[] getPlans() {
138                return plans;
139        }
140
141
142        /**
143         * @return the creation
144         */
145        public long getCreation() {
146                return creation;
147        }
148
149
150        public void setLastExecution(long lastExecution) {
151                this.lastExecution=lastExecution;
152        }
153
154}