def V(): semaphore = pos.Semaphore('/FBatch_Semaphore', pos.O_CREAT) try: logInfo('[PGCYCL]: Releasing semaphore', False) semaphore.release() logInfo('[PGCYCL]: Semaphore released', False) except: semaphore.release()
def P(): semaphore = pos.Semaphore('/FBatch_Semaphore', pos.O_CREAT) try: logInfo('[PGCYCL]: Acquiring semaphore', False) semaphore.acquire() logInfo('[PGCYCL]: Semaphore acquired', False) except: semaphore.release()
def convertStringToArray(): # get the message logInfo('[GOBATCH]: Receive the message with the queue', False) queue = pos.MessageQueue('/queue', pos.O_CREAT) stringReceived = queue.receive()[0] # split the message and remove the last element because it is empty stringSplitted = stringReceived.split(';')[:-1] commandList = [] for arrayTemp in stringSplitted: # split the message and remove the last element because it is empty commandList.append(arrayTemp.split('\t')[:-1]) return commandList
def useSemaphore(func, args, update_required): res = None try: P() # semaphore.acquire() if args is None: res = func() else: res = func(args) finally: V() # semaphore.release() if update_required: try: logInfo('[GOBATCH]: Releasing semaphore', False) pos.Semaphore('/FBatch_Updated', pos.O_CREAT).release() logInfo('[GOBATCH]: Semaphore released', False) finally: pos.Semaphore('/FBatch_Updated', pos.O_CREAT).release() return res
def createSemaphore(): try: # Creating semaphore logInfo('[PGCYCL]: Creating semaphore', False) pos.Semaphore('/FBatch_Semaphore', pos.O_CREAT | pos.O_EXCL, initial_value=1) logInfo('[PGCYCL]: Created semaphore', False) except pos.ExistentialError: # Semaphore already created logInfo('[PGCYCL]: Semaphore already created', False) pos.Semaphore('/FBatch_Semaphore', pos.O_CREAT) logInfo('[PGCYCL]: Using existing semaphore', False)
def getSemaphore(): try: # Creating semaphore logInfo('[GOBATCH]: Creating semaphore', False) semaphore = pos.Semaphore('/FBatch_Updated', pos.O_CREAT | pos.O_EXCL, initial_value=1) logInfo('[GOBATCH]: Created semaphore', False) except pos.ExistentialError: # Semaphore already created logInfo('[GOBATCH]: Semaphore already created', False) semaphore = pos.Semaphore('/FBatch_Updated', pos.O_CREAT) logInfo('[GOBATCH]: Using existing semaphore', False) return semaphore
queue = pos.MessageQueue('/queue', pos.O_CREAT) stringReceived = queue.receive()[0] # split the message and remove the last element because it is empty stringSplitted = stringReceived.split(';')[:-1] commandList = [] for arrayTemp in stringSplitted: # split the message and remove the last element because it is empty commandList.append(arrayTemp.split('\t')[:-1]) return commandList fileContent = convertStringToArray() # cross the array which contains the file content for line in fileContent: minute = line[0] hour = line[1] day = line[2] month = line[3] repeat = line[4] command = line[5] # get seconds left before the command execution seconds = secondsLeft(minute, hour, day, month, repeat) if seconds == 0: logInfo('[GOBATCH]: Command "' + command + '" executed', False) os.system(command)
def addNewTask(args): ##### # Var initialization ##### index = 0 # Index of the first optional arg arg = 0 # Current argument data = { 'command': '', 'minute': 0, 'hour': 0, 'frequency': '0', 'day': '0', 'month': '0' } args_length = len(args) if args_length < 3: printUsage() else: data.update({'command': args[index]}) file = open(FBATCH, 'a') try: index += 1 minute = verifyParam('minute', int(args[index]), 0, 59) data.update({'minute': minute}) index += 1 hour = verifyParam('hour', int(args[index]), 0, 23) data.update({'hour': hour}) index += 1 if index < args_length: arg = args[index] else: arg = '-d' index += 1 if arg == '-d' or arg == '--daily': data.update({'frequency': 'daily'}) elif arg == '-w' or arg == '--weekly': day = verifyParam('day of the week', args[index], 1, 7) data.update({'frequency': 'weekly', 'day': day}) elif arg == '-m' or arg == '--monthly': day = verifyParam('day', (args[index]), 1, 31) data.update({'frequency': 'monthly', 'day': day}) elif arg == '-y' or arg == '--yearly': month = verifyParam('month', int(args[index]), 1, 12) index += 1 day = verifyParam('day', int(args[index]), 1, 31) data.update({ 'frequency': 'yearly', 'day': day, 'month': month }) else: printUsage() content = writeToFBatch(**data) if os.stat(FBATCH).st_size != 0: content = '\n' + content file.write(content) file.flush() logInfo( '[PGCYCL]: Added command "{command}" will be executed at : {hour}:{minute} on a {frequency} basis' .format(**data), True) except IndexError: logError( '[PGCYCL]: Error : No value behind parameter : {}'.format(arg), True, True) exit(2) except ValueError: logError( '[PGCYCL]: Parameter {} needs integer value, given {}'.format( arg, args[index]), True, True) exit(2) finally: file.close()
def deleteTask(): tasks = listAllTasks(True) lines = listAllTasks(False) tasks_count = len(lines) if tasks_count == 0: logError( '[PGCYCL]: No tasks in fbatch file for the moment, add some first', True, False) exit(0) print(tasks) print('Give id of the tasks you which to delete (q to exit) ') index = getinput() if index == 'q' or index == '' or index == 'exit': logInfo('[PGCYCL]: User aborted deletion', True) exit(0) try: index = int(index) except ValueError: logError('[PGCYCL]: Id invalid, given "{}"'.format(index), True, True) exit(1) if index > tasks_count: logError( '[PGCYCL]: Id not found, "{}" given, only {} tasks'.format( index, tasks_count), True, True) exit(1) elif 0 < index <= tasks_count: line = lines[index - 1] output = 'Deleting tasks {} :\n'.format(index) output += 'Minute\tHour\tDay\tMonth\tFrequency\tCommand\n' output += line output += '\nAre you sure ? (y/N) ' try: print(output) answer = getinput() if not answer or answer == 'n': logInfo("User aborted deletion", True) exit(0) elif answer == 'y': lines.pop(index - 1) last = len(lines) - 1 lines[last] = lines[last].rstrip() # Resets file content open(FBATCH, 'w').close() fbatch_file = open(FBATCH, 'a') lines = ''.join(lines) fbatch_file.write(lines) fbatch_file.close() logInfo('[PGCYCL]: Recurrent task deleted successfully', True) except SyntaxError: logError('[PGCYCL]: Error : Answer invalid', True, True) exit(1) else: logError('[PGCYCL]: Id not valid', True, True) exit(1)
async def init(loop): app = web.Application(loop=loop) app.router.add_route('GET', '/', index) srv = await loop.create_server(app.make_handler(), '127.0.0.1', 9009) logger.logInfo('server started at http://127.0.0.1:9009...') return srv
semaphore = pos.Semaphore('/FBatch_Updated', pos.O_CREAT) logInfo('[GOBATCH]: Using existing semaphore', False) return semaphore # Handler to atexit function def exit_handler(): pos.Semaphore('/FBatch_Updated', pos.O_CREAT).unlink() # create semaphore createSemaphore() # creating the queue logInfo('[GOBATCH]: Creating the queue', False) queueContentFile = pos.MessageQueue('/queue', pos.O_CREAT) stringToSend = convertFileToString() # Use when gobatch.py is kill atexit.register(exit_handler) # Create and get semaphore semaphore = getSemaphore() while True: # wait until the next minute sleep(60 - datetime.utcnow().second) # try to get the semaphore to know if the file has been updated