Tasks.js
[gitlive] / Tasks.js
1
2 XObject = imports.XObject.XObject;
3 XMLHttpRequest = imports.XMLHttpRequest.XMLHttpRequest;
4 Netrc = imports.Netrc.Netrc;
5 Date = imports.Date.Date;
6 /**
7  *
8  * Tasks
9  *
10  *  Flow
11  *
12  *    Commit
13  *      => Tasks.notify(commit)
14  *
15  *
16  *
17  *
18  */
19
20 Tasks = {
21     
22     curTask : false,
23     commitRepo : false, // the DB version of repo info..
24     lastCommit : false,
25     
26     notify : function(commit)
27     {
28         if (this.inQuery && this.inQuery > (new Date())) {
29             // ignore the notification.. we are currently checking what the current
30             // status is.
31             
32             // we need to handle a WTF situation where something below failed... so
33             
34             return; 
35         }
36         this.inQuery = (new Date()).add(Date.MINUTE, 5);
37         this.lastCommit = commit;
38         this.commitRepo = false;
39         this.curTask = false;
40         this.fetchTask();
41         
42         
43     },
44     fetchTask: function()
45     {
46         // have we got the status in the last 15 mins..
47         // we should not need to get it again... - it's probably not changed.
48         if (this.curTask && !this.curTask.hasExpired()) {
49             this.fetchRepo();
50         }
51         _this = this;
52         // do the request to get the task..
53         var r = new XMLHttpRequest({
54             onreadystatechange : function() {
55                 print("Got result.");
56                 if (this.status != 4) {
57                     return;
58                 }
59                 
60                   
61                 var res = JSON.parse(this.responseText);
62                 
63                 //print(JSON.stringify(res,null,4))
64                 //print([ res.success , res.data.length ]);
65                 _this.curTask = (res.success && res.data.length) ? (new Task(res.data[0])) : false;
66                 print(JSON.stringify(_this.curTask,null,4));
67                 _this.fetchRepo();
68             }
69             
70         });
71         var netrc  = Netrc.forHost('git.roojs.com');
72         
73         r.open('GET',
74                "http://roojs.com/admin.php/Roo/cash_invoice_entry?_current_task=1"
75                ,true, netrc.login, netrc.password  );
76         //print("SEding request");        
77         r.send();
78         
79     },
80     
81     verifyCommit : function()
82     {
83         // using curTask + lastCommit decide what to do.
84         
85         
86         
87         
88         
89         this.inQuery = 0;
90         
91     },
92     
93     fetchRepo: function()
94     {
95         
96         var repo = this.lastCommit.repo;
97         
98          _this = this;
99         var r = new XMLHttpRequest({
100             onreadystatechange : function() {
101                 print("Got result.");
102                 if (this.status != 4) {
103                     return;
104                 }
105                 
106                   
107                 var res = JSON.parse(this.responseText);
108                 
109                 //print(JSON.stringify(res,null,4))
110                 //print([ res.success , res.data.length ]);
111                 _this.commitRepo = (res.success && res.data.length) ? currRepores.data[0] : false;
112                 print(JSON.stringify(_this.commit))
113                 _this.verifyCommit();
114             }
115             
116         });
117         var netrc  = Netrc.forHost('git.roojs.com');
118         
119         r.open('GET',
120                "http://roojs.com/admin.php/Roo/mtrack_repo?name=" + repo.name
121                ,true, netrc.login, netrc.password  );
122         //print("SEding request");        
123         r.send();
124         
125         
126     }
127     
128     
129     
130     
131     
132     
133 };
134
135
136
137
138
139 Task = XObject.define(
140     function(cfg) {
141         // cal parent?
142         if (typeof(cfg) != 'object') {
143             print("CFG not oboject?");
144             return;
145         } 
146         XObject.extend(this,cfg);
147  
148         // fix up the values.
149         this.action_datetime = Date.parseDate(this.action_dt,'Y-m-d H:i:s');
150       // print("ACT DT: " + this.action_dt);
151         
152     },
153     Object,
154     {
155         /**
156          * This is similar to the cash_invoice_entry data..
157          * 
158          */
159         action_dt: '', //"2012-11-23 11:00:00"
160         description: '', //"QA on new site"
161         qtyvalue: 0, //"2.25"
162         
163         hasExpired : function()
164         {
165             
166             var exp = this.action_datetime.add(Date.HOUR, this.qtyvalue);
167             return (new Date()) > exp;  
168             
169         }
170     }
171 );
172
173
174
175
176
177
178
179
180
181
182 //-------------- testing
183 Gtk = imports.gi.Gtk;
184 Gtk.init(Seed.argv);
185 Tasks.notify( { repo : imports.Scm.Repo.Repo.get('web.xtuple') } );
186 Gtk.main();