How to launch AWS Batch from a python client app

Overview

Last time Set to start from the client application by diverting the process executed from the created Lambda.

Preparation

Preparation of client application

It's long so it's folded
from utils.logger import LoggerObj
import sys
import os
import requests
import tkinter
from datetime import datetime
import boto3
from tkinter import *
from tkinter import messagebox
from tkinter import filedialog
from tkinter import ttk
from tkinter.ttk import *
import threading
from tkinter import messagebox
from tkinter import filedialog
from tkinter import Button,ttk,StringVar
from selenium import webdriver
from functools import partial 


root= tkinter.Tk()
EXECUTE_LIST=['Process A','Process B','Process C']
class PythonGui():

    def __init__(self):
        self.lock = threading.Lock()

        self.inputText=StringVar()

        self.progressMsg=StringVar()
        self.progressBar=None
        self.progressMsgBox=None

        self.progressStatusBar=None
        self.progressValue=None

    def init(self):
        pass
    
    #Operation after initial setting
    def preparation(self,logfilename):
        self._executer=partial(self.execute,logfilename)

    def progressSequence(self,msg,sequenceValue=0):
        self.progressMsg.set(msg)
        self.progressValue=self.progressValue+sequenceValue
        self.progressStatusBar.configure(value=self.progressValue)

    def quite(self):
        if messagebox.askokcancel('Confirmation of end','Do you want to end the process?'):
            if self.lock.acquire(blocking=FALSE):
                pass
            else:
                messagebox.showinfo('Confirmation of end','Please close the browser while the browser is running.')
            self.lock.release()
            root.quit()
        else:
            pass

    def execute(self,logfilename):

        logObj=LoggerObj()
        log=logObj.createLog(logfilename)
        log.info('Start processing')

        executeType=EXECUTE_LIST.index(self.combo.get())
        nowDate=datetime.now().strftime('%Y%m%d%H%M%S')
        inputVal=self.inputText.get()

        client = boto3.client('batch')

        JOB_NAME = 'pandas-envtest'
        JOB_QUEUE = "arn:aws:batch:ap-northeast-1:XXXXXXXXXX:job-queue/first-run-job-queue"
        JOB_DEFINITION = "arn:aws:batch:ap-northeast-1:XXXXXXXXXX:job-definition/pandas-envtest:1"

        response = client.submit_job(
            jobName = JOB_NAME,
            jobQueue = JOB_QUEUE,
            jobDefinition = JOB_DEFINITION,
        containerOverrides={
            'command': [
                inputVal,nowDate,str(executeType)
            ],
            'environment': [
                {
                    'name': 'TEST',
                    'value': 'abcd'
                }
            ]
        }
        )
        self.progressMsgBox.after(10,self.progressSequence('Processing is in progress',sequenceValue=50))
        root.update_idletasks()

        self.progressBar.stop()
        self.progressMsgBox.after(10,self.progressSequence('Registration process completed',sequenceValue=50))
        root.update_idletasks()

        log.info('Processing Exit')
        self.lock.release()
    

    def doExecute(self):
        if self.lock.acquire(blocking=FALSE):
            if messagebox.askokcancel('Confirmation before execution','Do you want to perform the process?'):
                self.progressValue=0
                self.progressStatusBar.configure(value=self.progressValue)
                self.progressBar.configure(maximum=10,value=0)
                self.progressBar.start(100)
                th = threading.Thread(target=self._executer)
                th.start()
            else:
                self.lock.release()
        else:
            messagebox.showwarning('error','Processing is in progress')


    def progressMsgSet(self,msg):
        self.progressMsg.set(msg)

    def progressStart(self):
        self.progressBar.start(100)

        


    def main(self):
        root.title("Python GUI")

        content = ttk.Frame(root)
        frame = ttk.Frame(content,  relief="sunken", width=300, height=500)
        title = ttk.Label(content, text="Python GUI")

        content.grid(column=0, row=0)


        title.grid(column=0, row=0, columnspan=4)

        fileLabel=ttk.Label(content,text="Processing number")
        pulldownLabel=ttk.Label(content,text="Processing content")

        fileInput=ttk.Entry(content,textvariable=self.inputText,width=23)
        
        self.inputText.set('A01')
        #Creating a combo box(Placed as root,List values cannot be edited(readonly)Set to)
        self.combo = ttk.Combobox(content, state='readonly')
        #Set the value of the list
        self.combo["values"] = tuple(EXECUTE_LIST)
        #The default value is food expenses(index=0)Set to
        self.combo.current(0)


        labelStyle=ttk.Style()
        labelStyle.configure('PL.TLabel',font=('Helvetica',10,'bold'),background='white',foreground='red')
        self.progressMsgBox=ttk.Label(content,textvariable=self.progressMsg,width=70,style='PL.TLabel')
        self.progressMsg.set('Waiting for processing')

        self.progressBar=ttk.Progressbar(content,orient=HORIZONTAL,length=140,mode='indeterminate')
        self.progressBar.configure(maximum=10,value=0)

        self.progressStatusBar=ttk.Progressbar(content,orient=HORIZONTAL,length=140,mode='determinate')


         
        executeButton=ttk.Button(content,text='Run',command=self.doExecute)
        quiteButton=ttk.Button(content,text='End',command=self.quite)

        fileLabel.grid(column=1, row=1,sticky='w')
        fileInput.grid(column=2, row=1)
        pulldownLabel.grid(column=1, row=2,sticky='w')

        #Combo box placement
        self.combo.grid(column=2, row=2)
        executeButton.grid(column=1, row=6,columnspan=2,sticky='we')
        quiteButton.grid(column=1, row=12,columnspan=2,sticky='we')




        root.mainloop()



if  __name__ == "__main__":
    pythonGui=PythonGui()
    pythonGui.preparation('log')
    pythonGui.main()

Recommended Posts

How to launch AWS Batch from a python client app
How to open a web browser from python
How to generate a Python object from JSON
Steps from installing Python 3 to creating a Django app
[Python] How to call a c function from python (ctypes)
How to create a kubernetes pod from python code
How to write a Python class
How to slice a block multiple array from a multiple array in Python
How to run a Python program from within a shell script
How to launch Explorer from WSL
How to access wikipedia from python
How to batch start a python program created with Jupyter notebook
How to pass arguments to a Python script in SPSS Modeler Batch
How to get a string from a command line argument in python
[Python] How to get & change rows / columns / values from a table.
Slack --APIGateway --Lambda (Python) --How to make a RedShift interactive app
How to remove duplicates from a Python list while preserving order.
[ROS2] How to play a bag file with python format launch
How to create a clone from Github
[Python] How to make a class iterable
[Python] How to convert a 2D list to a 1D list
Send a message from Python to Slack
[Python] How to invert a character string
How to get a stacktrace in python
How to access RDS from Lambda (python)
Launch a Flask app in Python Anywhere
How to create a repository from media
Manipulate BigQuery tables from a Python client
How to run a Maya Python script
How to get a value from a parameter store in lambda (using python)
Send a message from Slack to a Python server
Edit Excel from Python to create a PivotTable
How to read a CSV file with Python 2/3
How to create a Python virtual environment (venv)
How to clear tuples in a list (Python)
[2020 version] How to install Python3 on AWS EC2
How to embed a variable in a python string
How to create a function object from a string
Study from Python Hour7: How to use classes
How to develop a cart app with Django
How to create a JSON file in Python
[Python] How to read data from CIFAR-10 and CIFAR-100
How to add a Python module search path
How to handle Linux commands well from Python
How to extract coefficients from a fractional formula
How to notify a Discord channel in Python
[AWS / Lambda] How to load Python external library
How to create a multi-platform app with kivy
[Python] How to draw a histogram in Matplotlib
I made a library to operate AWS CloudFormation stack from CUI (Python Fabric)
[Python] How to easily drop a child process started by multiprocess from another process
Create a setting in terraform to send a message from AWS Lambda Python3.8 to Slack
How to install Python
Changes from Python 2 to Python 3.0
How to install python
[Python] How to draw a line graph with Matplotlib
How to set up a Python environment using pyenv
Terraform configured to launch AWS Lambda from Amazon SQS
App development to tweet in Python from Visual Studio 2017
[Python] How to expand variables in a character string
How to write a list / dictionary type of Python3