Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Store the jobs in a mongodb database

  • Loading branch information...
commit 376947e55164ce518defddc9bf56d26480d6e649 1 parent d3d38f9
@cedricss authored
Showing with 26 additions and 6 deletions.
  1. +26 −6 main.opa
View
32 main.opa
@@ -1,5 +1,16 @@
import stdlib.themes.bootstrap.{css, icons, responsive}
+type status = { timeout } or { unreachable } or { unknown_error } or { error_simulation } or { ok }
+type log = { string url, status status, Date.date date }
+type job = { string url, int freq }
+
+// Define two collections in the "monitor" database
+database monitor {
+ stringmap(log) /logs
+ stringmap(job) /jobs
+ /logs[_]/status = { ok } // Define for example the default "status" value
+}
+
module Job {
exposed @async function check(name, url, uri) {
@@ -12,6 +23,14 @@ module Job {
}
}
+ exposed @async function log(name, label, url, status) {
+ date = Date.now();
+ name = "[{label}] {name} - {Date.in_milliseconds(date) / 1000}";
+ /monitor/logs[name] <- (~{ url, status, date }) // Add a log in the database logs list
+ }
+
+ exposed @async function add(name, url, freq){ /monitor/jobs[name] <- (~{ url, freq }) }
+ exposed @async function remove(name){ Db.remove(@/monitor/jobs[name]) }
}
client module Action {
@@ -26,8 +45,8 @@ client module Action {
function up(url) { msg(url, "label-success", "is UP") }
function invalid(url) { msg("ERROR: {url}", "label-inverse", "an invalid url") }
- function down(name, url, failure, status) { msg(url, "label-important", "is DOWN ({failure})"); }
- function test(name, url, status) { msg("", "label-inverse", "You should see a Dropbox popup on your desktop"); }
+ function down(name, url, failure, status) { msg(url, "label-important", "is DOWN ({failure})"); Job.log(name, "DOWN", url, status); }
+ function test(name, url, status) { msg("", "label-inverse", "You should see a Dropbox popup on your desktop"); Job.log(name, "TEST", url, status); }
function error_test(_) { test(Dom.get_value(#name), Dom.get_value(#url), { error_simulation }) }
function add_job(name, url, uri, freq) {
@@ -35,28 +54,29 @@ client module Action {
timer = Scheduler.make_timer(freq*1000, function() { Job.check(name, url, uri) });
Job.check(name, url, uri); timer.start();
- function remove(_) { timer.stop(); Dom.remove(#{name}); }
+ function remove(_) { timer.stop(); Dom.remove(#{name}); Job.remove(name) }
function edit(_) {
timer.stop(); Dom.remove(#{name});
Dom.set_value(#name, name); Dom.set_value(#url, url)
Dom.set_value(#freq, String.of_int(freq))
}
-
edit_btn = <a class="btn-mini" onclick={edit}><i class="icon-edit"></i></a>
remove_btn = <a class="btn-mini" onclick={remove}><i class="icon-remove"></i></a>
player_id = "{name}_player";
- // Start and pause buttons definitions depend on each other:
+ // Start and pause buttons definitions depend on each other
recursive function stop(_) { timer.stop(); #{player_id} = start_btn }
and function start(_) { timer.start(); #{player_id} = stop_btn }
and stop_btn = <a class="btn-mini" onclick={stop}><i class="icon-pause"></i></a>
and start_btn = <a class="btn-mini" onclick={start}><i class="icon-play"></i></a>
- // Add a new line on top of the job list
+ // Add a new line on top of the job list:
#jobs += <tr id=#{name}>
<td>{url} each {freq} sec</td>
<td><span id=#{player_id}>{stop_btn}</span>{edit_btn}{remove_btn}</td>
</tr>;
+
+ Job.add(name, url, freq)
}
function submit_job(_) {
Please sign in to comment.
Something went wrong with that request. Please try again.