Skip to content

Commit

Permalink
Add new execute button
Browse files Browse the repository at this point in the history
  • Loading branch information
chaadow committed Sep 13, 2024
1 parent 281fa8a commit fe293e2
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 6 deletions.
11 changes: 11 additions & 0 deletions app/controllers/clockwork_web/home_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -35,5 +35,16 @@ def job
ClockworkWeb.on_job_update.call(job: job, enable: enable, user: try(ClockworkWeb.user_method)) if ClockworkWeb.on_job_update
redirect_to root_path
end

def execute
job = params[:job]

event = Clockwork.manager.events.find { _1.job == params[:job] }

event.run(Time.now)
ClockworkWeb.set_last_run(event.job)

redirect_to root_path
end
end
end
11 changes: 9 additions & 2 deletions app/views/clockwork_web/home/index.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,10 @@
.width-15 {
width: 15%;
}

.button-form {
display: inline-block;
}
<% end %>
</head>
<body>
Expand Down Expand Up @@ -80,7 +84,7 @@
<th>Job</th>
<th class="width-15">Period</th>
<th class="width-15">Last Run</th>
<th class="width-15">Action</th>
<th class="width-15">Actions</th>
</tr>
</thead>
<tbody>
Expand All @@ -99,7 +103,10 @@
<% end %>
</td>
<td><%= last_run(@last_runs[event.job]) %></td>
<td><%= button_to enabled ? "Disable" : "Enable", home_job_path(job: event.job, enable: !enabled), disabled: !ClockworkWeb.redis %></td>
<td>
<%= button_to enabled ? "Disable" : "Enable", home_job_path(job: event.job, enable: !enabled), disabled: !ClockworkWeb.redis, form_class: 'button-form' %>
<%= button_to "Run now", home_execute_path(job: event.job), disabled: !ClockworkWeb.redis, form_class: 'button-form' %>
</td>
</tr>
<% end %>
</tbody>
Expand Down
2 changes: 2 additions & 0 deletions config/routes.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
ClockworkWeb::Engine.routes.draw do
post "home/job"
post "home/execute"

root to: "home#index"
end
12 changes: 8 additions & 4 deletions lib/clockwork_web.rb
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ def self.last_heartbeat
def self.heartbeat
if redis
heartbeat = Time.now.to_i
if heartbeat % 10 == 0
if heartbeat % 10 == 0 # every 10 seconds
prev_heartbeat = redis.getset(HEARTBEAT_KEY, heartbeat).to_i
if prev_heartbeat >= heartbeat
redis.setex(STATUS_KEY, 60, "multiple")
Expand All @@ -110,15 +110,19 @@ module Clockwork

on(:before_run) do |event, t|
run = true

Safely.safely do
run = ClockworkWeb.enabled?(event.job)
if run
ClockworkWeb.set_last_run(event.job)
else
unless run
manager.log "Skipping '#{event}'"
event.last = event.convert_timezone(t)
end
end

run
end

on(:after_run) do |event, _t|
ClockworkWeb.set_last_run(event.job) if ClockworkWeb.enabled?(event.job)
end
end

0 comments on commit fe293e2

Please sign in to comment.