def get_display(my): top = my.top top.add_class("spt_sandbox_select_top") sandbox_options = [ { 'name': 'fast', 'base_dir': 'C:/Fast', }, { 'name': 'faster', 'base_dir': 'C:/Faster', }, { 'name': 'slow', 'base_dir': 'Z:/Slow', } ] process = my.kwargs.get("process") search_key = my.kwargs.get("search_key") sobject = Search.get_by_search_key(search_key) search_type = sobject.get_base_search_type() client_os = Environment.get_env_object().get_client_os() if client_os == 'nt': prefix = "win32" else: prefix = "linux" alias_dict = Config.get_dict_value("checkin", "%s_sandbox_dir" % prefix) search_key = sobject.get_search_key() key = "sandbox_dir:%s" % search_key from pyasm.web import WidgetSettings value = WidgetSettings.get_value_by_key(key) sandboxes_div = DivWdg() top.add(sandboxes_div) sandboxes_div.add_relay_behavior( { 'type': 'mouseenter', 'bvr_match_class': 'spt_sandbox_option', 'cbjs_action': ''' var last_background = bvr.src_el.getStyle("background-color"); bvr.src_el.setAttribute("spt_last_background", last_background); bvr.src_el.setStyle("background-color", "#E0E0E0"); bvr.src_el.setStyle("opacity", "1.0"); ''' } ) sandboxes_div.add_relay_behavior( { 'type': 'mouseleave', 'bvr_match_class': 'spt_sandbox_option', 'cbjs_action': ''' var last_background = bvr.src_el.getAttribute("spt_last_background"); bvr.src_el.setStyle("background-color", last_background); if (!bvr.src_el.hasClass("spt_selected")) { bvr.src_el.setStyle("opacity", "0.5"); } ''' } ) sandboxes_div.add_relay_behavior( { 'type': 'mouseup', 'key': key, 'bvr_match_class': 'spt_sandbox_option', 'cbjs_action': ''' var sandbox_dir = bvr.src_el.getAttribute("spt_sandbox_dir"); var server = TacticServerStub.get(); server.set_widget_setting(bvr.key, sandbox_dir); var applet = spt.Applet.get(); applet.makedirs(sandbox_dir); //var top = bvr.src_el.getParent(".spt_sandbox_select_top"); var top = bvr.src_el.getParent(".spt_checkin_top"); spt.panel.refresh(top); ''' } ) #search = Search("config/naming") #search.add_filter("search_type", search_type) #search.add_filter("process", process) #namings = search.get_sobjects() #naming = namings[0] from pyasm.biz import Snapshot, Naming virtual_snapshot = Snapshot.create_new() virtual_snapshot.set_value("process", process) # for purposes of the sandbox folder for the checkin widget, # the context is the process virtual_snapshot.set_value("context", process) naming = Naming.get(sobject, virtual_snapshot) if naming: naming_expr = naming.get_value("sandbox_dir_naming") alias_options = naming.get_value("sandbox_dir_alias") else: naming_expr = None alias_options = None if alias_options == "__all__": alias_options = alias_dict.keys() elif alias_options: alias_options = alias_options.split("|") else: alias_options = ['default'] for alias in alias_options: from pyasm.biz import DirNaming dir_naming = DirNaming(sobject=sobject, snapshot=virtual_snapshot) dir_naming.set_protocol("sandbox") dir_naming.set_naming(naming_expr) base_dir = dir_naming.get_dir(alias=alias) sandbox_div = DivWdg() sandboxes_div.add(sandbox_div) sandbox_div.add_class("spt_sandbox_option") sandbox_div.add_attr("spt_sandbox_dir", base_dir) if value == base_dir: sandbox_div.add_color("background", "background3") #sandbox_div.set_box_shadow() sandbox_div.add_class("spt_selected") else: sandbox_div.add_style("opacity", "0.5") sandbox_div.add_style("width: auto") sandbox_div.add_style("height: 55px") sandbox_div.add_style("padding: 5px") #sandbox_div.add_style("float: left") sandbox_div.add_style("margin: 15px") sandbox_div.add_border() if alias: alias_div = DivWdg() sandbox_div.add(alias_div) alias_div.add(alias) alias_div.add_style("font-size: 1.5em") alias_div.add_style("font-weight: bold") alias_div.add_style("margin-bottom: 15px") icon_wdg = IconWdg("Folder", IconWdg.FOLDER) sandbox_div.add(icon_wdg) sandbox_div.add(base_dir) return top
def check_new_job(self, queue_type=None): num_jobs = len(self.jobs) if num_jobs >= self.max_jobs: print("Already at max jobs [%s]" % self.max_jobs) return self.job = self.get_next_job(queue_type) if not self.job: return # set the process key process_key = self.get_process_key() self.job.set_value("host", process_key) self.job.commit() self.jobs.append(self.job) # get some info from the job command = self.job.get_value("command") job_code = self.job.get_value("code") try: kwargs = self.job.get_json_value("data") except: try: # DEPRECATED kwargs = self.job.get_json_value("serialized") except: kwargs = {} if not kwargs: kwargs = {} login = self.job.get_value("login") script_path = self.job.get_value("script_path", no_exception=True) project_code = self.job.get_value("project_code") if script_path: command = 'tactic.command.PythonCmd' folder = os.path.dirname(script_path) title = os.path.basename(script_path) search = Search("config/custom_script") search.add_filter("folder", folder) search.add_filter("title", title) custom_script = search.get_sobject() script_code = custom_script.get_value("script") kwargs['code'] = script_code # add the job to the kwargs kwargs['job'] = self.job #print("command: ", command) #print("kwargs: ", kwargs) # Because we started a new thread, the environment may not # yet be initialized try: from pyasm.common import Environment Environment.get_env_object() except: # it usually is run at the very first transaction Batch() Project.set_project(project_code) queue = self.job.get_value("queue", no_exception=True) queue_type = 'repeat' stop_on_error = False print("Running job: ", self.job.get_value("code") ) if queue_type == 'inline': cmd = Common.create_from_class_path(command, kwargs=kwargs) try: Container.put(Command.TOP_CMD_KEY, None) Container.put(Transaction.KEY, None) Command.execute_cmd(cmd) # set job to complete self.job.set_value("state", "complete") except Exception as e: self.job.set_value("state", "error") self.job.commit() self.jobs.remove(self.job) self.job = None self.jobs_completed += 1 elif queue_type == 'repeat': attempts = 0 max_attempts = 3 retry_interval = 5 Container.put(Transaction.KEY, None) while 1: try: cmd = Common.create_from_class_path(command, kwargs=kwargs) Container.put(Command.TOP_CMD_KEY, None) Command.execute_cmd(cmd) #cmd.execute() # set job to complete self.job.set_value("state", "complete") break except TacticException as e: # This is an error on this server, so just exit # and don't bother retrying print("Error: ", e) self.job.set_value("state", "error") break except Exception as e: if stop_on_error: raise print("WARNING in Queue: ", e) import time time.sleep(retry_interval) attempts += 1 if attempts >= max_attempts: print("ERROR: reached max attempts") self.job.set_value("state", "error") break print("Retrying [%s]...." % attempts) self.job.commit() self.jobs.remove(self.job) self.job = None self.jobs_completed += 1 else: class ForkedTask(SchedulerTask): def __init__(self, **kwargs): super(ForkedTask, self).__init__(**kwargs) def execute(self): # check to see the status of this job """ job = self.kwargs.get('job') job_code = job.get_code() search = Search("sthpw/queue") search.add_filter("code", job_code) self.kwargs['job'] = search.get_sobject() if not job: print("Cancelling ...") return state = job.get_value("state") if state == "cancel": print("Cancelling 2 ....") return """ subprocess_kwargs = { 'login': login, 'project_code': project_code, 'command': command, 'kwargs': kwargs } subprocess_kwargs_str = jsondumps(subprocess_kwargs) install_dir = Environment.get_install_dir() python = Config.get_value("services", "python") if not python: python = 'python' args = ['%s' % python, '%s/src/tactic/command/queue.py' % install_dir] args.append(subprocess_kwargs_str) import subprocess p = subprocess.Popen(args) DbContainer.close_thread_sql() return # can't use a forked task ... need to use a system call #Command.execute_cmd(cmd) # register this as a forked task task = ForkedTask(name=job_code, job=self.job) scheduler = Scheduler.get() scheduler.start_thread() # FIXME: the queue should not be inline if queue == 'interval': interval = self.job.get_value("interval") if not interval: interval = 60 scheduler.add_interval_task(task, interval=interval,mode='threaded') else: scheduler.add_single_task(task, mode='threaded')
def check_new_job(my): num_jobs = len(my.jobs) if num_jobs >= my.max_jobs: print "Already at max jobs [%s]" % my.max_jobs return my.job = my.get_next_job() if not my.job: return # set the process key process_key = my.get_process_key() my.job.set_value("host", process_key) my.job.commit() my.jobs.append(my.job) # get some info from the job command = my.job.get_value("command") job_code = my.job.get_value("code") try: kwargs = my.job.get_json_value("data") except: try: # DEPRECATED kwargs = my.job.get_json_value("serialized") except: kwargs = {} if not kwargs: kwargs = {} login = my.job.get_value("login") script_path = my.job.get_value("script_path", no_exception=True) project_code = my.job.get_value("project_code") if script_path: command = 'tactic.command.PythonCmd' folder = os.path.dirname(script_path) title = os.path.basename(script_path) search = Search("config/custom_script") search.add_filter("folder", folder) search.add_filter("title", title) custom_script = search.get_sobject() script_code = custom_script.get_value("script") kwargs['code'] = script_code # add the job to the kwargs kwargs['job'] = my.job #print "command: ", command #print "kwargs: ", kwargs # Because we started a new thread, the environment may not # yet be initialized try: from pyasm.common import Environment Environment.get_env_object() except: Batch() Project.set_project(project_code) queue = my.job.get_value("queue", no_exception=True) queue_type = 'repeat' stop_on_error = False print "Running job: ", my.job.get_value("code") if queue_type == 'inline': cmd = Common.create_from_class_path(command, kwargs=kwargs) try: Command.execute_cmd(cmd) # set job to complete my.job.set_value("state", "complete") except Exception, e: my.job.set_value("state", "error") my.job.commit() my.jobs.remove(my.job) my.job = None my.jobs_completed += 1
def check_new_job(my): num_jobs = len(my.jobs) if num_jobs >= my.max_jobs: print "Already at max jobs [%s]" % my.max_jobs return my.job = my.get_next_job() if not my.job: return # set the process key process_key = my.get_process_key() my.job.set_value("host", process_key) my.job.commit() my.jobs.append(my.job) # get some info from the job command = my.job.get_value("command") job_code = my.job.get_value("code") #print "Grabbing job [%s] ... " % job_code try: kwargs = my.job.get_json_value("data") except: try: kwargs = my.job.get_json_value("serialized") except: kwargs = {} project_code = my.job.get_value("project_code") login = my.job.get_value("login") script_path = my.job.get_value("script_path", no_exception=True) if script_path: Project.set_project(project_code) command = 'tactic.command.PythonCmd' folder = os.path.dirname(script_path) title = os.path.basename(script_path) search = Search("config/custom_script") search.add_filter("folder", folder) search.add_filter("title", title) custom_script = search.get_sobject() script_code = custom_script.get_value("script") kwargs['code'] = script_code # add the job to the kwargs kwargs['job'] = my.job #print "command: ", command #print "kwargs: ", kwargs # Because we started a new thread, the environment may not # yet be initialized try: from pyasm.common import Environment Environment.get_env_object() except: print "running batch" Batch() queue = my.job.get_value("queue", no_exception=True) queue_type = 'repeat' print "running job: ", my.job.get_value("code") if queue_type == 'inline': cmd = Common.create_from_class_path(command, kwargs=kwargs) try: Command.execute_cmd(cmd) # set job to complete my.job.set_value("state", "complete") except Exception, e: my.job.set_value("state", "error") my.job.commit() my.jobs.remove(my.job) my.job = None
def get_base_dir(my, protocol=None): '''get the default base directory for this sobject''' dirs = [] base_dir = '' if not protocol: protocol = my.protocol if protocol == "http": repo_handler = my.sobject.get_repo_handler(my.snapshot) if repo_handler.is_tactic_repo(): base_dir = Config.get_value("checkin", "web_base_dir") else: base_dir = Config.get_value("perforce", "web_base_dir") elif protocol == "remote": # TODO: currently needs web to do this base_dir = Environment.get_env_object().get_base_url().to_string() repo_handler = my.sobject.get_repo_handler(my.snapshot) if repo_handler.is_tactic_repo(): sub_dir = Config.get_value("checkin", "web_base_dir") else: sub_dir = Config.get_value("perforce", "web_base_dir") base_dir = "%s%s" % (base_dir, sub_dir) elif protocol == "file": #base_dir = Config.get_value("checkin", "asset_base_dir") base_dir = Environment.get_asset_dir(my._file_object) elif protocol == "env": base_dir = "$TACTIC_ASSET_DIR" # This is the central repository as seen from the client elif protocol in ["client_lib", "client_repo"]: if Environment.get_env_object().get_client_os() =='nt': base_dir = my.get_custom_setting('win32_client_repo_dir') if not base_dir: if my._file_object: base_dir_alias = my._file_object.get_value('base_dir_alias') if base_dir_alias: alias_dict = Config.get_value("checkin", "base_dir_alias", sub_key=base_dir_alias) base_dir = alias_dict.get("win32_client_repo_dir") if not base_dir: base_dir = Config.get_value("checkin", "win32_client_repo_dir", no_exception=True) else: base_dir = my.get_custom_setting('linux_client_repo_dir') if not base_dir: if my._file_object: base_dir_alias = my._file_object.get_value('base_dir_alias') if base_dir_alias: alias_dict = Config.get_value("checkin", "base_dir_alias", sub_key=base_dir_alias) base_dir = alias_dict.get("linux_client_repo_dir") if not base_dir: base_dir = Config.get_value("checkin", "linux_client_repo_dir", no_exception=True) if not base_dir: #base_dir = Config.get_value("checkin", "asset_base_dir") base_dir = Environment.get_asset_dir() # DEPRECATED: The local repo. This one has logic to add "repo" dir # at the end. Use local_repo which does not have this logic. # keeping this around for backward compatibility elif protocol == "local": remote_repo = my.get_remote_repo() if remote_repo: #base_dir = remote_repo.get_value("repo_base_dir") base_dir = Environment.get_asset_dir() else: if Environment.get_env_object().get_client_os() =='nt': base_dir = Config.get_value("checkin","win32_local_base_dir") else: base_dir = Config.get_value("checkin","linux_local_base_dir") base_dir += "/repo" # The local repo elif protocol == "local_repo": remote_repo = my.get_remote_repo() if remote_repo: base_dir = remote_repo.get_value("repo_base_dir") else: if Environment.get_env_object().get_client_os() =='nt': base_dir = Config.get_value("checkin","win32_local_repo_dir") else: base_dir = Config.get_value("checkin","linux_local_repo_dir") if not base_dir: base_dir = Environment.get_asset_dir() elif protocol == "sandbox": remote_repo = my.get_remote_repo() if remote_repo: base_dir = remote_repo.get_value("sandbox_base_dir") else: base_dir = PrefSetting.get_value_by_key("sandbox_base_dir") if not base_dir: if Environment.get_env_object().get_client_os() =='nt': base_dir = Config.get_value("checkin","win32_sandbox_dir") if base_dir == "": base_dir = Config.get_value("checkin","win32_local_base_dir") base_dir += "/sandbox" else: base_dir = Config.get_value("checkin","linux_sandbox_dir") if base_dir == "": base_dir = Config.get_value("checkin","linux_local_base_dir") base_dir += "/sandbox" elif protocol == "relative": return [] return [base_dir]
def get_base_dir(my, protocol=None, alias="default"): '''get the default base directory for this sobject''' dirs = [] base_dir = '' client_os = Environment.get_env_object().get_client_os() if client_os == 'nt': prefix = "win32" else: prefix = "linux" if not alias: alias = "default" if not protocol: protocol = my.protocol if protocol == "http": repo_handler = my.sobject.get_repo_handler(my.snapshot) if repo_handler.is_tactic_repo(): base_dir = Environment.get_web_dir(alias=alias) else: alias_dict = Config.get_dict_value("perforce", "web_base_dir") base_dir = alias_dict.get(alias) if not base_dir: asset_alias_dict = Environment.get_asset_dirs() base_dir = asset_alias_dict.get(alias) base_dir = "/%s" % os.path.basename(base_dir) if not base_dir: base_dir = alias_dict.get("default") if not base_dir: base_dir = "/assets" elif protocol == "remote": # NOTE: currently needs web to get the full http base url base_dir = Environment.get_env_object().get_base_url().to_string() sub_dir = my.get_base_dir(protocol='http', alias=alias) base_dir = "%s%s" % (base_dir, sub_dir[0]) elif protocol == "file": base_dir = Environment.get_asset_dir(alias=alias) elif protocol == "env": base_dir = "$TACTIC_ASSET_DIR" # This is the central repository as seen from the client elif protocol in ["client_lib", "client_repo"]: base_dir = my.get_custom_setting('%s_client_repo_dir' % prefix) if not base_dir: alias_dict = Config.get_dict_value("checkin", "%s_client_repo_dir" % prefix) base_dir = alias_dict.get(alias) if not base_dir: base_dir = Environment.get_asset_dir() # DEPRECATED: The local repo. This one has logic to add "repo" dir # at the end. Use local_repo which does not have this logic. # keeping this around for backward compatibility elif protocol == "local": remote_repo = my.get_remote_repo() if remote_repo: #base_dir = remote_repo.get_value("repo_base_dir") base_dir = Environment.get_asset_dir() else: if Environment.get_env_object().get_client_os() =='nt': base_dir = Config.get_value("checkin","win32_local_base_dir") else: base_dir = Config.get_value("checkin","linux_local_base_dir") base_dir += "/repo" # The local repo elif protocol == "local_repo": remote_repo = my.get_remote_repo() if remote_repo: base_dir = remote_repo.get_value("repo_base_dir") else: if Environment.get_env_object().get_client_os() =='nt': base_dir = Config.get_value("checkin","win32_local_repo_dir") else: base_dir = Config.get_value("checkin","linux_local_repo_dir") if not base_dir: base_dir = Environment.get_asset_dir() elif protocol == "sandbox": remote_repo = my.get_remote_repo() if remote_repo: base_dir = remote_repo.get_value("sandbox_base_dir") else: if not base_dir: base_dict = Config.get_dict_value("checkin","%s_sandbox_dir" % prefix) base_dir = base_dict.get(alias) elif protocol == "relative": return [] assert base_dir return [base_dir]
def get_base_dir(self, protocol=None, alias="default"): '''get the default base directory for this sobject''' dirs = [] base_dir = '' client_os = Environment.get_env_object().get_client_os() if client_os == 'nt': prefix = "win32" else: prefix = "linux" if not alias: alias = "default" if not protocol: protocol = self.protocol if protocol == "http": repo_handler = self.sobject.get_repo_handler(self.snapshot) if repo_handler.is_tactic_repo(): base_dir = Environment.get_web_dir(alias=alias) else: alias_dict = Config.get_dict_value("perforce", "web_base_dir") base_dir = alias_dict.get(alias) if not base_dir: asset_alias_dict = Environment.get_asset_dirs() base_dir = asset_alias_dict.get(alias) base_dir = "/%s" % os.path.basename(base_dir) if not base_dir: base_dir = alias_dict.get("default") if not base_dir: base_dir = "/assets" elif protocol == "remote": # NOTE: currently needs web to get the full http base url base_dir = Environment.get_env_object().get_base_url().to_string() sub_dir = self.get_base_dir(protocol='http', alias=alias) base_dir = "%s%s" % (base_dir, sub_dir[0]) elif protocol == "file": base_dir = Environment.get_asset_dir(alias=alias) elif protocol == "env": base_dir = "$TACTIC_ASSET_DIR" # This is the central repository as seen from the client elif protocol in ["client_lib", "client_repo"]: base_dir = self.get_custom_setting('%s_client_repo_dir' % prefix) if not base_dir: alias_dict = Config.get_dict_value("checkin", "%s_client_repo_dir" % prefix) base_dir = alias_dict.get(alias) if not base_dir: base_dir = Environment.get_asset_dir() # DEPRECATED: The local repo. This one has logic to add "repo" dir # at the end. Use local_repo which does not have this logic. # keeping this around for backward compatibility elif protocol == "local": remote_repo = self.get_remote_repo() if remote_repo: #base_dir = remote_repo.get_value("repo_base_dir") base_dir = Environment.get_asset_dir() else: if Environment.get_env_object().get_client_os() =='nt': base_dir = Config.get_value("checkin","win32_local_base_dir") else: base_dir = Config.get_value("checkin","linux_local_base_dir") base_dir += "/repo" # The local repo elif protocol == "local_repo": remote_repo = self.get_remote_repo() if remote_repo: base_dir = remote_repo.get_value("repo_base_dir") else: if Environment.get_env_object().get_client_os() =='nt': base_dir = Config.get_value("checkin","win32_local_repo_dir") else: base_dir = Config.get_value("checkin","linux_local_repo_dir") if not base_dir: base_dir = Environment.get_asset_dir() elif protocol == "sandbox": remote_repo = self.get_remote_repo() if remote_repo: base_dir = remote_repo.get_value("sandbox_base_dir") else: if not base_dir: base_dict = Config.get_dict_value("checkin","%s_sandbox_dir" % prefix) base_dir = base_dict.get(alias) elif protocol == "relative": return [] assert base_dir return [base_dir]
def handle_directories(self, top): # deal with asset directories top.add(DivWdg('Asset Folders', css='spt_info_title')) mailserver = Config.get_value("services", "mailserver") table = Table() table.add_color("color", "color") table.add_style("margin: 10px") top.add(table) table.add_row() td = table.add_cell("asset_base_dir: ") td.add_style("width: 150px") asset_base_dir = Config.get_value("checkin", "asset_base_dir") if asset_base_dir: table.add_cell(asset_base_dir) tr = table.add_row() tr.add_style('border-bottom: 1px #bbb solid') # check if it is writable is_writable = os.access(asset_base_dir, os.W_OK) span = SpanWdg("writable:") span.add_style('padding-left: 20px') td = table.add_cell(span) td = table.add_cell(str(is_writable)) else: table.add_cell("None configured") client_os = Environment.get_env_object().get_client_os() if os.name == 'nt': os_name = 'win32' else: os_name = 'linux' if client_os == 'nt': client_os_name = 'win32' else: client_os_name = 'linux' env = Environment.get() client_handoff_dir = env.get_client_handoff_dir(include_ticket=False, no_exception=True) client_asset_dir = env.get_client_repo_dir() table.add_row() td = table.add_cell("%s_server_handoff_dir: " % os_name) td.add_style("width: 150px") handoff_dir = Config.get_value("checkin", "%s_server_handoff_dir" % os_name) if handoff_dir: table.add_cell(handoff_dir) table.add_row() # check if it is writable is_writable = os.access(handoff_dir, os.W_OK) span = SpanWdg("writable:") span.add_style('padding-left: 20px') td = table.add_cell(span) td = table.add_cell(str(is_writable)) else: table.add_cell("None configured") table.add_row() td = table.add_cell("%s hand-off test: " % client_os_name) td.add_style("width: 150px") button = ActionButtonWdg(title='Test') button.add_behavior({ 'type': 'click_up', 'handoff_dir': client_handoff_dir, 'asset_dir': client_asset_dir, 'cbjs_action': ''' var env = spt.Environment.get(); var applet = spt.Applet.get(); var handoff_state = applet.exists(bvr.handoff_dir); var asset_state = applet.exists(bvr.asset_dir); if (asset_state == false) { env.set_transfer_mode("web"); spt.error('client repo directory is not accessible: ' + bvr.asset_dir); } else if (handoff_state == false) { env.set_transfer_mode("web"); spt.error('client handoff directory is not accessible: ' + bvr.handoff_dir); } else { env.set_transfer_mode("copy"); spt.info('<div>client handoff directory: ' + bvr.handoff_dir + '</div><br/><div>client repo directory :' + bvr.asset_dir + '</div><br/><div> can be successfully accessed.</div>', {type:'html'}); } ''' }) table.add_cell(button)
def handle_directories(self, top): # deal with asset directories top.add(DivWdg('Asset Folders', css='spt_info_title')) mailserver = Config.get_value("services", "mailserver") table = Table() table.add_color("color", "color") table.add_style("margin: 10px") top.add(table) table.add_row() td = table.add_cell("asset_base_dir: ") td.add_style("width: 150px") asset_base_dir = Config.get_value("checkin", "asset_base_dir") if asset_base_dir: table.add_cell( asset_base_dir ) tr = table.add_row() tr.add_style('border-bottom: 1px #bbb solid') # check if it is writable is_writable = os.access(asset_base_dir, os.W_OK) span = SpanWdg("writable:") span.add_style('padding-left: 20px') td = table.add_cell(span) td = table.add_cell(str(is_writable)) else: table.add_cell( "None configured") client_os = Environment.get_env_object().get_client_os() if os.name == 'nt': os_name = 'win32' else: os_name = 'linux' if client_os == 'nt': client_os_name = 'win32' else: client_os_name = 'linux' env = Environment.get() client_handoff_dir = env.get_client_handoff_dir(include_ticket=False, no_exception=True) client_asset_dir = env.get_client_repo_dir() table.add_row() td = table.add_cell("%s_server_handoff_dir: " % os_name) td.add_style("width: 150px") handoff_dir = Config.get_value("checkin", "%s_server_handoff_dir" % os_name) if handoff_dir: table.add_cell( handoff_dir ) table.add_row() # check if it is writable is_writable = os.access(handoff_dir, os.W_OK) span = SpanWdg("writable:") span.add_style('padding-left: 20px') td = table.add_cell(span) td = table.add_cell(str(is_writable)) else: table.add_cell( "None configured") table.add_row() td = table.add_cell("%s hand-off test: " % client_os_name) td.add_style("width: 150px") button = ActionButtonWdg(title='Test') button.add_behavior( { 'type': 'click_up', 'handoff_dir': client_handoff_dir, 'asset_dir': client_asset_dir, 'cbjs_action': ''' var env = spt.Environment.get(); var applet = spt.Applet.get(); var handoff_state = applet.exists(bvr.handoff_dir); var asset_state = applet.exists(bvr.asset_dir); if (asset_state == false) { env.set_transfer_mode("web"); spt.error('client repo directory is not accessible: ' + bvr.asset_dir); } else if (handoff_state == false) { env.set_transfer_mode("web"); spt.error('client handoff directory is not accessible: ' + bvr.handoff_dir); } else { env.set_transfer_mode("copy"); spt.info('<div>client handoff directory: ' + bvr.handoff_dir + '</div><br/><div>client repo directory :' + bvr.asset_dir + '</div><br/><div> can be successfully accessed.</div>', {type:'html'}); } ''' } ) table.add_cell( button )
def check_new_job(self, queue_type=None): num_jobs = len(self.jobs) if num_jobs >= self.max_jobs: print("Already at max jobs [%s]" % self.max_jobs) return self.job = self.get_next_job(queue_type) if not self.job: return # set the process key process_key = self.get_process_key() self.job.set_value("host", process_key) self.job.commit() self.jobs.append(self.job) # get some info from the job command = self.job.get_value("command") job_code = self.job.get_value("code") try: kwargs = self.job.get_json_value("data") except: try: # DEPRECATED kwargs = self.job.get_json_value("serialized") except: kwargs = {} if not kwargs: kwargs = {} login = self.job.get_value("login") script_path = self.job.get_value("script_path", no_exception=True) project_code = self.job.get_value("project_code") if script_path: command = 'tactic.command.PythonCmd' folder = os.path.dirname(script_path) title = os.path.basename(script_path) search = Search("config/custom_script") search.add_filter("folder", folder) search.add_filter("title", title) custom_script = search.get_sobject() script_code = custom_script.get_value("script") kwargs['code'] = script_code # add the job to the kwargs kwargs['job'] = self.job #print("command: ", command) #print("kwargs: ", kwargs) # Because we started a new thread, the environment may not # yet be initialized try: from pyasm.common import Environment Environment.get_env_object() except: # it usually is run at the very first transaction Batch() Project.set_project(project_code) queue = self.job.get_value("queue", no_exception=True) queue_type = 'repeat' stop_on_error = False print("Running job: ", self.job.get_value("code")) if queue_type == 'inline': cmd = Common.create_from_class_path(command, kwargs=kwargs) try: Container.put(Command.TOP_CMD_KEY, None) Container.put(Transaction.KEY, None) Command.execute_cmd(cmd) # set job to complete self.job.set_value("state", "complete") except Exception as e: self.job.set_value("state", "error") self.job.commit() self.jobs.remove(self.job) self.job = None self.jobs_completed += 1 elif queue_type == 'repeat': attempts = 0 max_attempts = 3 retry_interval = 5 Container.put(Transaction.KEY, None) while 1: try: cmd = Common.create_from_class_path(command, kwargs=kwargs) Container.put(Command.TOP_CMD_KEY, None) Command.execute_cmd(cmd) #cmd.execute() # set job to complete self.job.set_value("state", "complete") break except TacticException as e: # This is an error on this server, so just exit # and don't bother retrying print("Error: ", e) self.job.set_value("state", "error") break except Exception as e: if stop_on_error: raise print("WARNING in Queue: ", e) import time time.sleep(retry_interval) attempts += 1 if attempts >= max_attempts: print("ERROR: reached max attempts") self.job.set_value("state", "error") break print("Retrying [%s]...." % attempts) self.job.commit() self.jobs.remove(self.job) self.job = None self.jobs_completed += 1 else: class ForkedTask(SchedulerTask): def __init__(self, **kwargs): super(ForkedTask, self).__init__(**kwargs) def execute(self): # check to see the status of this job """ job = self.kwargs.get('job') job_code = job.get_code() search = Search("sthpw/queue") search.add_filter("code", job_code) self.kwargs['job'] = search.get_sobject() if not job: print("Cancelling ...") return state = job.get_value("state") if state == "cancel": print("Cancelling 2 ....") return """ subprocess_kwargs = { 'login': login, 'project_code': project_code, 'command': command, 'kwargs': kwargs } subprocess_kwargs_str = jsondumps(subprocess_kwargs) install_dir = Environment.get_install_dir() python = Config.get_value("services", "python") if not python: python = 'python' args = [ '%s' % python, '%s/src/tactic/command/queue.py' % install_dir ] args.append(subprocess_kwargs_str) import subprocess p = subprocess.Popen(args) DbContainer.close_thread_sql() return # can't use a forked task ... need to use a system call #Command.execute_cmd(cmd) # register this as a forked task task = ForkedTask(name=job_code, job=self.job) scheduler = Scheduler.get() scheduler.start_thread() # FIXME: the queue should not be inline if queue == 'interval': interval = self.job.get_value("interval") if not interval: interval = 60 scheduler.add_interval_task(task, interval=interval, mode='threaded') else: scheduler.add_single_task(task, mode='threaded')