- 1:方法一:
- 1.1:UI界面部分
- 1.2:机器分配事件插件(当机器空闲时触发)
- 2:方法二:
- 2.1:UI界面部分
- 2.2:机器分配事件插件(当提交任务时触发)
方法一:
UI界面部分
[State]
Type=Enum
Items=Global Enabled;Opt-In;Disabled
Category=Options
CategoryOrder=0
CategoryIndex=0
Label=State
Default=Disabled
Description=How this event plug-in should respond to events. If Global, all jobs and slaves will trigger the events for this plugin. If Opt-In, jobs and slaves can choose to trigger the events for this plugin. If Disabled, no events are triggered for this plugin.c
机器分配事件插件(当机器空闲时触发)
###############################################################
# Imports
###############################################################
import math
from System import *
from Deadline.Events import *
from Deadline.Scripting import *
##################################################################################################
# This is the function called by Deadline to get an instance of the Draft event listener.
##################################################################################################
def GetDeadlineEventListener():
return OverrideMachineLimitListener()
def CleanupDeadlineEventListener(eventListener):
eventListener.Cleanup()
###############################################################
# The event listener class.
###############################################################
class OverrideMachineLimitListener(DeadlineEventListener):
def __init__(self):
self.OnSlaveIdleCallback += self.OnSlaveIdle
def Cleanup(self):
del self.OnSlaveIdleCallback
def OnSlaveIdle(self, string):
#^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^以上部分为 套路写法
self.LogInfo("AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA" * 15)
#--------------------------------------------------------------------------任务状态 为 激活或者等待 的任务列表
jobs = RepositoryUtils.GetJobsInState(["Active", "Pending"])
#-----------------------------------------------------------------------------从机信息 为 空闲或者渲染 的设备列表 ( 方法一 GetSlaveInfos )
slaveInfos = RepositoryUtils.GetSlaveInfos(True)
slaves = [x for x in slaveInfos if x.SlaveState in ["Idle", "Rendering"]]
for i in slaves:
self.LogInfo("Idle Or Rendering: %s" % i.SlaveName)#------------------------------------------------------------------------------------日志
self.LogInfo("Idle Or Rendering Num : %d" % len(slaves))#------------------------------------------------------------------------------------日志
# enableSlaves = [x for x in slaveInfos if x.SlaveIsActive in ["Enable"]]
# self.LogInfo("%d" % len(enableSlaves))
# for slaveInfo in slaveInfos:
# self.LogInfo(slaveInfo.SlaveStatus)
# self.LogInfo(",".join(dir(slaveInfos[0])))
#---------------------------------------------------------------------------------------------------从机状态 为 开启 的设备列表 ( 方法二 GetSlaveSettingsList )
# slaveSettings = RepositoryUtils.GetSlaveSettingsList(True)
# self.LogInfo(",".join(dir(slaveSettings[0])))
# slaves = [x for x in slaveSettings if x.SlaveEnabled]
# slavesiii = [x.SlaveName for x in slaveSettings if x.SlaveIsActive]#---------从机状态为 激活 的设备列表-------是否可行未知
#-------------------------------------------------------------------输出 加入过池 的设备数量
# ppp = [x for x in slaveSettings if x.SlavePools]
# self.LogInfo("ChiShu: %d" % len(ppp))
#--------------------------------------------------------------------------------加入到 两个池 的设备名
# self.LogInfo(len(x.SlavePools))
# if len(x.SlavePools) == 2:
# self.LogInfo(x.SlaveName)
#------------------------------------------------------------------------输出 所有设备名
# for x in slaveSettings:
# self.LogInfo(x.SlaveName)
#--------------------------------------------------------------------------------其他
# self.LogInfo(",".join(dir(jobs[0])))
# for job in jobs:
# self.LogInfo(job.Name)
# self.LogInfo("%d" % job.JobTaskCount)
if jobs and slaves:
userNum = len(set(job.UserName for job in jobs if job.JobTaskCount != 2))
self.LogInfo("User Num: %d" % userNum)#------------------------------------------------------------------------------------日志
self.LogInfo("Machine Num: %d" % len(slaves))#------------------------------------------------------------------------------------日志
if not userNum:
return
num = int(math.ceil(float(len(slaves)) / float(userNum)))
self.LogInfo("PingJun Num: %d" % num)#------------------------------------------------------------------------------------日志
self.LogInfo("######################################################" * 10)
if userNum == 1:
for job in jobs:
if job.MachineLimit != 0:
self.LogInfo("need change slaves to 0: %s" % job.Name)#------------------------------------------------------------------------------------日志
RepositoryUtils.SetMachineLimitMaximum(job.ID, 0)
# self.LogInfo(RepositoryUtils.GetMachineLimit(job.ID))#------------------------------------------------------------------------------------日志
else:
for job in jobs:
if job.MachineLimit != num:
self.LogInfo("need change limit slaves: %s" % job.Name)#------------------------------------------------------------------------------------日志
RepositoryUtils.SetMachineLimitMaximum(job.ID, num)
# self.LogInfo("MachineLimit Num: %d" % len(RepositoryUtils.GetMachineLimit(job.ID)))#------------------------------------------------------------------------------------日志
self.LogInfo("==========================================================" * 10)
方法二:
UI界面部分
[State]
Type=Enum
Items=Global Enabled;Opt-In;Disabled
Label=State
Default=Global Enabled
Description=How this event plug-in should respond to events. If Global, all jobs and Workers will trigger the events for this plugin. If Opt-In, jobs and Workers can choose to trigger the events for this plugin. If Disabled, no events are triggered for this plugin.
[EventCallbacks]
Type=Label
Description=The Event Callbacks that this Event Plugin subscribes to.
Default=OnJobSubmitted
机器分配事件插件(当提交任务时触发)
import math
from Deadline.Events import *
from Deadline.Scripting import *
def GetDeadlineEventListener():
return DeadlineBalancer()
def CleanupDeadlineEventListener(deadlinePlugin):
deadlinePlugin.Cleanup()
class DeadlineBalancer(DeadlineEventListener):
def __init__(self):
self.OnJobSubmittedCallback += self.OnJobSubmitted
self.OnHouseCleaningCallback += self.OnHouseCleaning
def Cleanup(self):
del self.OnJobSubmittedCallback
del self.OnHouseCleaningCallback
def OnJobSubmitted(self, job):
self.Balance()
def OnHouseCleaning(self):
self.Balance()
def Balance(self):
print("Starting balancing of current active jobs")
# function variables
totalPriority = 0.0
skipJobs = []
reusableWorkers = 0
# get all active jobs
jobs = RepositoryUtils.GetJobsInState("Active")
jobsN = len(jobs)
print("Total amount of jobs balancing: %s" % (jobsN))
# get all active workers
workersUnfiltered = RepositoryUtils.GetSlaveInfoSettings(invalidateCache=True)
workers = []
# filter workers for active only
for worker in workersUnfiltered:
if worker.Info.SlaveIsActive:
workers.append(worker)
workersN = len(workers)
print("Total amount of active workers for balancing: %s" % (workersN))
# set the total priority for all jobs found
for job in jobs:
# get priority
priority = job.JobPriority
# update total priority
totalPriority += priority
print("The total priority is: %s" % totalPriority)
# calculate the machine limit for every job
for job in jobs:
# calculate the amount of workers applicable for this job
percent = 0.0
percent = float(job.JobPriority) / float(totalPriority)
# if no slaves are available set the amount to 1 to avoid errors
if math.ceil(percent * workersN) is 0:
workers = 1
else:
workers = math.ceil(percent * workersN)
print("Handing %s over to %s" % (workers, job))
RepositoryUtils.SetMachineLimitMaximum(job.JobId, workers)
job.MachineLimit = workers
# check if job has rendering frames that are less than applicable amount of workers
frames = job.JobRenderingTasks + job.JobQueuedTasks
if frames < workers:
skipJobs.append(job)
reusableWorkers += workers - frames
workers = frames
print(
"Detected fewer frames than handed workers. Handing %s over to %s"
% (workers, job)
)
job.MachineLimit = workers
RepositoryUtils.SetMachineLimitMaximum(job.JobId, workers)
# redistribute reusable workers
for job in jobs:
if job in skipJobs:
continue
if reusableWorkers > 0:
# calculate the new amount
percent = float(job.JobPriority) / float(totalPriority)
workers = math.ceil(percent * reusableWorkers)
print("Handing %s extra workers to %s" % (workers, job))
RepositoryUtils.SetMachineLimitMaximum(
job.JobId, job.MachineLimit + workers
)