10gen / core-modules-admin

10gen Site Admin Interface

This URL has Read+Write access

core-modules-admin / cron.jxp
100644 188 lines (159 sloc) 5.881 kb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
<% /**
* Copyright (C) 2008 10gen Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/ %>
 
<%
head.addCSS("NOCDN/admin/assets/admin.css");
head.addCSS("/@@/yui/current/container/assets/skins/sam/container.css");
head.addCSS("button/assets/skins/sam/button.css");
head.addScript("NOCDN/admin/assets/interpreter.js");
head.push('<script type="text/javascript" src="/@@/yui/current/yuiloader/yuiloader-min.js" ></script>');
head.push('<script type="text/javascript" src="/@@/yui/current/yahoo-dom-event/yahoo-dom-event.js" ></script>');
head.push('<script type="text/javascript" src="/@@/yui/current/element/element-beta-min.js" ></script>');
head.push('<script type="text/javascript" src="/@@/yui/current/button/button-min.js" ></script>');
head.push('<script type="text/javascript" src="/@@/yui/current/container/container-min.js" ></script>');
%>
 
 
<h1 style="margin:0;">
  <nobr>Cron Jobs</nobr>
</h1>
 
<p class="abstract">Add and edit jobs that will be repeated at intervals.</p>
 
<p class="abstract"><a onclick="newCron()">Add Cron</a></p>
<div id="myMarkedUpContainer" class="yui-skin-sam">
</div>
 
<div id="editCron">
<div id="editCronPanelHd" class="hd">Add New Cron</div>
<div class="bd">
<form>
  <input type="hidden" id="c__id" >
      <div><label>Name</label><input id="c_name" ></div>
      <div><label>url</label><input id="c_url"></div>
      <div><label>Every X minutes</label><input id="c_xminutes"></div>
</form>
</div>
<div class="ft"><input type="button" onclick="sendCron()" value="Save"><input type="button" onclick="editCronPanel.hide()" value="Cancel"></div>
</div>
 
<script type="text/javascript">
 
var currentPage = 1;
var lastSearch = "";
 
function deleteJob(id) {
    ajax("action=delete&ns=admin.cron&id="+id, "editDb", refreshTable);
}
 
function refreshTable(passData){
    if(passData)
        passData = passData+"&";
    else
        passData = "";
    ajax("pagename=cron&ns=admin.cron&action=search&currentPage="+currentPage+"&"+passData+lastSearch, "table", function(response) {
        document.getElementById("myCollection").innerHTML = response;
    });
}
 
function getSearchArgs() {
    return document.getElementById("searchopt").value + "=" + document.getElementById("searchtext").value;
}
 
function doSearch() {
    var passData = getSearchArgs();
    lastSearch = passData;
    refreshTable();
}
function clearSearch() {
    document.getElementById("searchtext").value = "";
    document.getElementById("searchopt").options[0].selected = true;
    currentSearch = {};
    lastSearch = "";
    refreshTable();
}
 
var handleNo = function() {
    this.hide();
}
 
function loadPage(pagenum) {
    currentPage = pagenum;
    refreshTable();
}
function sendCron() {
    var passData = "name="+document.getElementById("c_name").value+
    "&url="+document.getElementById("c_url").value+
    "&xminutes="+document.getElementById("c_xminutes").value;
    var id = document.getElementById("c__id").value;
    if(id!="")
        passData+="&_id="+id;
 
    ajax(passData, "editCron", refreshTable);
    editCronPanel.hide();
    document.getElementById("editCronPanelHd").innerHTML = "Add New Cron";
}
function newCron() {
    document.getElementById("c__id").value = "";
    document.getElementById("c_name").value = "";
    document.getElementById("c_url").value = "";
    document.getElementById("c_xminutes").value = "";
    document.getElementById("editCronPanelHd").innerHTML = "Add New Cron";
    editCronPanel.show();
}
 
function editCron(id) {
    document.getElementById("c__id").value = id;
    document.getElementById("editCronPanelHd").innerHTML = "Edit Existing Cron";
    editCronPanel.show();
}
 
editCronPanel = new YAHOO.widget.Panel("editCron", { visible: false, constraintoviewport: true, fixedcenter: true });
editCronPanel.render("myMarkedUpContainer");
document.getElementById("editCron").style.display = "block";
refreshTable();
</script>
 
 
<div id="myCollection">
<%
 
/*request.pageName = "cron";
request.ns = "admin.cron";
request.action = "search";
adminRoot.table();
*/
 
if ( request._run == "true" ){
    var debug = request._debug;
 
    var all = db.cron.find().toArray();
    for ( var i=0; i<all.length; i++ ){
        var cron = all[i];
        // doing this so if someone else updated i'll know about it
        cron = db.cron.findOne( cron._id );
 
        if ( ! cron.xminutes ){
            cron.lastResult = "Error - no xminutes set";
            db.cron.save( cron );
            continue;
        }
 
        if ( cron.lastRun ){
            var nextRun = cron.lastRun.getTime() + ( cron.xminutes * 60 * 1000.0 );
            if ( nextRun > new Date().getTime() )
                continue;
        }
 
        print( "running : " + cron.name + "<BR>" );
        cron.lastRun = new Date();
        db.cron.save( cron );
 
        var func = scope.eval( cron.url );
        if ( ! func ){
            cron.lastResult = "can't find : " + cron.url;
        }
        else {
            var printSave = print;
            var buf = "";
            print = function(z){
                buf += z;
            };
            func();
            print = printSave;
            cron.lastResult = buf;
        }
 
        if ( debug ) print( "<div style='border:1px solid black;'>result: " + cron.lastResult + "</div>" );
        db.cron.save( cron );
        print( "<hr>" );
    }
}
 
%>
</div>