Skip to content

Commit

Permalink
scheduled task model changes, UI for schedule
Browse files Browse the repository at this point in the history
  • Loading branch information
Raekye committed Nov 6, 2015
1 parent 407f979 commit a954fa5
Show file tree
Hide file tree
Showing 9 changed files with 61 additions and 10 deletions.
20 changes: 13 additions & 7 deletions app/models/scheduled_task.rb
Original file line number Diff line number Diff line change
Expand Up @@ -41,25 +41,25 @@ class ScheduledTask < ActiveRecord::Base

def to_user_string
m = self.partition % 100
hours = self.partition / 100
hours = (self.partition / 100) + self.server.timezone_delta
h_24 = hours % 24
h_12 = h_24 % 12
ampm = h_24 < 12 ? 'am' : 'pm'
d = hours / 24
return "#{DAYS_OF_WEEK_INVERSE[d]} #{h_12}:#{m} #{ampm} #{action}"
end

def self.parse(str)
def self.parse(str, server)
xs = []
str.each_line do |l|
x = self.parse_line(l.clean)
x = self.parse_line(l.clean, server)
if !x.nil?
xs.push(x)
end
end
end

def self.parse_line(line)
def self.parse_line(line, server)
if line =~ /([a-z]+)\s+(\d+):(\d+)\s*([a-z]+)\s+([a-z]+)/
day = DAYS_OF_WEEK[$1]
hour = $2.to_i % 12
Expand Down Expand Up @@ -87,13 +87,19 @@ def self.parse_line(line)
return nil
end
return ScheduledTask.new({
partition: self.calculate_partition(day, hour, minute, ampm),
server: server,
partition: self.calculate_partition(day, hour, minute, ampm, server.timezone_delta),
action: action,
})
end
end

def self.calculate_partition(day, hour, minute, ampm)
return (day * 24 + hour + ampm * 12) * 100 + minute
def self.calculate_partition(day, hour, minute, ampm, delta)
x = ((day * 24) + (hour) + (ampm * 12) - (delta)) % (7 * 24)
return x * 100 + minute
end

def self.server_time
return DateTime.now.in_time_zone(Gamocosm::TIMEZONE).strftime('%-I:%M %P (%H:%M) %Z')
end
end
5 changes: 5 additions & 0 deletions app/models/server.rb
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
# remote_region_slug :string not null
# remote_size_slug :string not null
# remote_snapshot_id :integer
# timezone_delta :integer default(0), not null
#

class Server < ActiveRecord::Base
Expand Down Expand Up @@ -55,6 +56,10 @@ def before_validate_callback
self.ssh_keys = self.ssh_keys.try(:gsub, /\s/, '').clean
end

def schedule_text
return self.scheduled_tasks.map { |x| x.to_user_string }.join("\n")
end

def host_name
return "#{name}.minecraft.gamocosm"
end
Expand Down
2 changes: 1 addition & 1 deletion app/models/server_log.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,6 @@ class ServerLog < ActiveRecord::Base
belongs_to :server

def when
return created_at.in_time_zone(ActiveSupport::TimeZone[-8]).strftime('%Y %b %e (%H:%M:%S %Z)')
return created_at.in_time_zone(Gamocosm::TIMEZONE).strftime('%Y %b %-d (%H:%M:%S %Z)')
end
end
30 changes: 30 additions & 0 deletions app/views/servers/_schedule.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
<br />
<%= panel_with_heading 'Schedule' do %>
<%= simple_form_for @server, (@demo.nil? ? { url: server_path(@server), method: :put } : { html: { onsubmit: 'return false;' } }) do |f| %>
<p>
You can setup a schedule to start/stop a server at different days of the week.
Autoshutdown also works with the schedule, but note that autoshutdown goes off after ~8 minutes of inactivity.
</p>
<br />
<%= f.input :timezone_delta %>
<p>
The current server time is <b><%= ScheduledTask.server_time %></b> (Pacific Time).
Suppose your time is 3 hours ahead of the server: your "timezone delta" is 3.
If your time is behind the server, you can enter a negative number.
This way, your schedule will be based on your times.
</p>
<br />
<%= f.input :schedule_text, as: :text, label: 'Schedule', input_html: { rows: 4 } %>
<p>
Enter a rule on each line, in the format <code>[day of week] [hour]:[minute] [am or pm] [start or stop]</code>:
<ul>
<li>A day of the week is [<%= ScheduledTask::DAYS_OF_WEEK_INVERSE.map { |k, v| k }.join(', ') %>], or just the first 3 letters</li>
<li>The hour is in 12-hour format (1-12)</li>
<li>The minute is either "00" or "30" (only half-hour intervals supported)</li>
<li>Everything is case-insensitive ("am" is the same as "AM")</li>
</ul>
</p>
<br />
<%= f.button :submit, 'Save', class: 'btn btn-success' %>
<%end %>
<% end %>
9 changes: 7 additions & 2 deletions app/views/servers/show.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -19,20 +19,22 @@
<div class="col-sm-9">
<ul class="nav nav-tabs">
<% if @demo.nil? %>
<li class="<%= @server_advanced_tab.nil? && @server_ftp_ssh_tab.nil? ? 'active' : '' %>"><%= link_to 'Profile', server_path(@server, anchor: 'profile'), 'data-toggle' => 'tab' %></li>
<li class="<%= @server_advanced_tab.nil? && @server_ftp_ssh_tab.nil? && @server_schedule_tab.nil? ? 'active' : '' %>"><%= link_to 'Profile', server_path(@server, anchor: 'profile'), 'data-toggle' => 'tab' %></li>
<li><%= link_to 'Minecraft Settings', server_path(@server, anchor: 'settings'), 'data-toggle' => 'tab' %></li>
<li class="<%= @server_ftp_ssh_tab ? 'active' : '' %>"><%= link_to 'FTP and SSH', server_path(@server, anchor: 'ftp_ssh'), 'data-toggle' => 'tab' %></li>
<li class="<%= @server_schedule_tab ? 'active' : '' %>"><%= link_to 'Schedule', server_path(@server, anchor: 'schedule'), 'data-toggle' => 'tab' %></li>
<li class="<%= @server_advanced_tab ? 'active' : '' %>"><%= link_to 'Advanced', server_path(@server, anchor: 'advanced'), 'data-toggle' => 'tab' %></li>
<% else %>
<li class="active"><a href="#profile" data-toggle="tab">Profile</a></li>
<li><a href="#settings" data-toggle="tab">Minecraft Settings</a></li>
<li><a href="#ftp_ssh" data-toggle="tab">FTP and SSH</a></li>
<li><a href="#schedule" data-toggle="tab">Schedule</a></li>
<li><a href="#advanced" data-toggle="tab">Advanced</a></li>
<% end %>
</ul>

<div class="tab-content">
<div class="tab-pane <%= @server_advanced_tab.nil? && @server_ftp_ssh_tab.nil? ? 'active' : '' %>" id="profile">
<div class="tab-pane <%= @server_advanced_tab.nil? && @server_ftp_ssh_tab.nil? && @server_schedule_tab.nil? ? 'active' : '' %>" id="profile">
<br />
<%= render partial: 'servers/profile' %>
</div>
Expand All @@ -42,6 +44,9 @@
<div class="tab-pane <%= @server_ftp_ssh_tab ? 'active' : '' %>" id="ftp_ssh">
<%= render partial: 'servers/ftp_ssh' %>
</div>
<div class="tab-pane <%= @server_schedule_tab ? 'active' : '' %>" id="schedule">
<%= render partial: 'servers/schedule' %>
</div>
<div class="tab-pane <%= @server_advanced_tab ? 'active' : '' %>" id="advanced">
<%= render partial: 'servers/advanced' %>
</div>
Expand Down
2 changes: 2 additions & 0 deletions config/application.rb
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,8 @@ module Gamocosm
DIGITAL_OCEAN_SSH_PUBLIC_KEY_FINGERPRINT = Digest::MD5.hexdigest(Base64.decode64(DIGITAL_OCEAN_SSH_PUBLIC_KEY.split(/\s+/m)[1])).scan(/../).join(':')
GIT_HEAD = `git rev-parse HEAD`.strip
GIT_HEAD_DATE = Time.at(`git show -s --format=%ct HEAD`.to_i).strftime('%Y %b %-d %H:%M %Z')
# see ActiveSupport::TimeZone
TIMEZONE = 'Pacific Time (US & Canada)'

@digital_ocean = nil
def self.digital_ocean
Expand Down
1 change: 1 addition & 0 deletions db/migrate/20151106024157_create_scheduled_tasks.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,5 +7,6 @@ def change
end
add_foreign_key :scheduled_tasks, :servers, { on_delete: :cascade }
add_index :scheduled_tasks, :partition
add_column :servers, :timezone_delta, :integer, { null: false, default: 0 }
end
end
1 change: 1 addition & 0 deletions db/schema.rb
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@
t.string "remote_region_slug", null: false
t.string "remote_size_slug", null: false
t.integer "remote_snapshot_id"
t.integer "timezone_delta", default: 0, null: false
end

add_index "servers", ["domain"], name: "index_servers_on_domain", unique: true, using: :btree
Expand Down
1 change: 1 addition & 0 deletions test/models/server_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
# remote_region_slug :string not null
# remote_size_slug :string not null
# remote_snapshot_id :integer
# timezone_delta :integer default(0), not null
#

require 'test_helper'
Expand Down

0 comments on commit a954fa5

Please sign in to comment.