Skip to content

Instantly share code, notes, and snippets.

@Grandvizir
Created August 21, 2015 14:59
Show Gist options
  • Save Grandvizir/d0244e5909cbec00ce37 to your computer and use it in GitHub Desktop.
Save Grandvizir/d0244e5909cbec00ce37 to your computer and use it in GitHub Desktop.
module Administration
class Administration::BaseController < ApplicationController
layout 'administration'
private
def current_session_process_id(_city_id, _process_id = nil )
session["user.process_id.#{ _city_id }"] = _process_id if _process_id
session["user.process_id.#{ _city_id }"]
end
def get_current_status_for( _city_id )
status = Sidekiq::Status::pct_complete( current_session_process_id( _city_id ) )
if status.is_a?( Integer ) && s == 100 then reset_session end
status
end
end
end
module Administration
class Administration::TrackersController < Administration::BaseController
before_action :set_tracker, only: [:start, :shutdown, :restart]
def restart
@tracker.restart
current_session_process_id(@tracker.city_entity.id, @tracker.process_id)
redirect_to :back, notice: 'Tracker have been restarted.'
end
def start
@tracker.deploy!
current_session_process_id(@tracker.city_entity.id, @tracker.process_id)
redirect_to :back, notice: 'Tracker have been started.'
end
def shutdown
@tracker.shutdown!
current_session_process_id(@tracker.city_entity.id, @tracker.process_id)
redirect_to :back, notice: 'Tracker have been turned off.'
end
private
def set_tracker
@tracker = Tracker.find( params[:id] )
end
end
end
require 'test_helper'
class TrackersControllerTest < ActionController::TestCase
setup do
@url = '/administration/city_entities/980190963'
@tracker = trackers(:one)
@tracker.save
puts @tracker.state
@controller = Administration::TrackersController.new
request.env['HTTP_REFERER'] = @url # tels where the request is coming from
end
test 'Should start a Tracker' do
get :start, id: @tracker.id
assert_redirected_to @url
puts session.inspect
tmp_tracker = Tracker.find( @tracker.id )
puts tmp_tracker.state
assert (tmp_tracker.state.to_sym == :running), 'expected state: loading'
end
test 'Should restart a Tracker' do
get :restart, id: @tracker.id
assert_redirected_to @url
tmp_tracker = Tracker.find( @tracker.id )
puts tmp_tracker.state
assert ( (tmp_tracker.state.to_sym == :running) || (tmp_tracker.state.to_sym == :loading) ), 'expected state: loading'
end
test 'Should shutdown a Tracker and delete unsed files (pid & log)' do
get :shutdown, id: @tracker.id
assert_redirected_to @url
tmp_tracker = Tracker.find( @tracker.id )
puts tmp_tracker.state
assert (tmp_tracker.state.to_sym == :stopped), 'expected state: stopped'
assert ( !File.exists?( tmp_tracker.pid_file_path ) ), 'expected pid file to be deleted'
assert ( !File.exists?( tmp_tracker.log_file_path ) ), 'expected log file to be deleted'
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment