本文整理汇总了Python中freppledb.execute.models.Task.processid方法的典型用法代码示例。如果您正苦于以下问题:Python Task.processid方法的具体用法?Python Task.processid怎么用?Python Task.processid使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类freppledb.execute.models.Task
的用法示例。
在下文中一共展示了Task.processid方法的9个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: handle
# 需要导入模块: from freppledb.execute.models import Task [as 别名]
# 或者: from freppledb.execute.models.Task import processid [as 别名]
def handle(self, **options):
# Pick up the options
database = options['database']
if database not in settings.DATABASES:
raise CommandError("No database settings known for '%s'" % database )
if options['user']:
try:
user = User.objects.all().using(database).get(username=options['user'])
except:
raise CommandError("User '%s' not found" % options['user'] )
else:
user = None
now = datetime.now()
task = None
try:
# Initialize the task
if 'task' in options and options['task']:
try:
task = Task.objects.all().using(database).get(pk=options['task'])
except:
raise CommandError("Task identifier not found")
if task.started or task.finished or task.status != "Waiting" or task.name not in ('frepple_backup', 'backup'):
raise CommandError("Invalid task identifier")
task.status = '0%'
task.started = now
else:
task = Task(name='backup', submitted=now, started=now, status='0%', user=user)
# Choose the backup file name
backupfile = now.strftime("database.%s.%%Y%%m%%d.%%H%%M%%S.dump" % database)
task.message = 'Backup to file %s' % backupfile
# Run the backup command
# Commenting the next line is a little more secure, but requires you to
# create a .pgpass file.
os.environ['PGPASSWORD'] = settings.DATABASES[database]['PASSWORD']
args = [
"pg_dump",
"-Fc", "-w",
'--username=%s' % settings.DATABASES[database]['USER'],
'--file=%s' % os.path.abspath(os.path.join(settings.FREPPLE_LOGDIR, backupfile))
]
if settings.DATABASES[database]['HOST']:
args.append("--host=%s" % settings.DATABASES[database]['HOST'])
if settings.DATABASES[database]['PORT']:
args.append("--port=%s " % settings.DATABASES[database]['PORT'])
args.append(settings.DATABASES[database]['NAME'])
with subprocess.Popen(args) as p:
try:
task.processid = p.pid
task.save(using=database)
p.wait()
except:
p.kill()
p.wait()
raise Exception("Run of run pg_dump failed")
# Task update
task.processid = None
task.status = '99%'
task.save(using=database)
# Delete backups older than a month
pattern = re.compile("database.*.*.*.dump")
for f in os.listdir(settings.FREPPLE_LOGDIR):
if os.path.isfile(os.path.join(settings.FREPPLE_LOGDIR, f)):
# Note this is NOT 100% correct on UNIX. st_ctime is not alawys the creation date...
created = datetime.fromtimestamp(os.stat(os.path.join(settings.FREPPLE_LOGDIR, f)).st_ctime)
if pattern.match(f) and (now - created).days > 31:
try:
os.remove(os.path.join(settings.FREPPLE_LOGDIR, f))
except:
pass
# Task update
task.status = 'Done'
task.finished = datetime.now()
task.processid = None
except Exception as e:
if task:
task.status = 'Failed'
task.message = '%s' % e
task.finished = datetime.now()
task.processid = None
raise e
finally:
if task:
task.save(using=database)
示例2: handle
# 需要导入模块: from freppledb.execute.models import Task [as 别名]
# 或者: from freppledb.execute.models.Task import processid [as 别名]
def handle(self, **options):
# Pick up the options
database = options['database']
if database not in settings.DATABASES:
raise CommandError("No database settings known for '%s'" % database )
if options['user']:
try:
user = User.objects.all().using(database).get(username=options['user'])
except:
raise CommandError("User '%s' not found" % options['user'] )
else:
user = None
now = datetime.now()
task = None
try:
# Initialize the task
if options['task']:
try:
task = Task.objects.all().using(database).get(pk=options['task'])
except:
raise CommandError("Task identifier not found")
if task.started or task.finished or task.status != "Waiting" or task.name not in ('frepple_restore', 'restore'):
raise CommandError("Invalid task identifier")
task.status = '0%'
task.started = now
else:
task = Task(name='restore', submitted=now, started=now, status='0%', user=user)
task.arguments = options['dump']
task.processid = os.getpid()
task.save(using=database)
# Validate options
dumpfile = os.path.abspath(os.path.join(settings.FREPPLE_LOGDIR, options['dump']))
if not os.path.isfile(dumpfile):
raise CommandError("Dump file not found")
# Run the restore command
# Commenting the next line is a little more secure, but requires you to create a .pgpass file.
if settings.DATABASES[database]['PASSWORD']:
os.environ['PGPASSWORD'] = settings.DATABASES[database]['PASSWORD']
cmd = [ "pg_restore", "-n", "public", "-Fc", "-c", "--if-exists" ]
if settings.DATABASES[database]['USER']:
cmd.append("--username=%s" % settings.DATABASES[database]['USER'])
if settings.DATABASES[database]['HOST']:
cmd.append("--host=%s" % settings.DATABASES[database]['HOST'])
if settings.DATABASES[database]['PORT']:
cmd.append("--port=%s " % settings.DATABASES[database]['PORT'])
cmd.append("-d")
cmd.append(settings.DATABASES[database]['NAME'])
cmd.append('<%s' % dumpfile)
# Shell needs to be True in order to interpret the < character
with subprocess.Popen(cmd, shell=True) as p:
try:
task.processid = p.pid
task.save(using=database)
p.wait()
except:
p.kill()
p.wait()
raise Exception("Database restoration failed")
# Task update
# We need to recreate a new task record, since the previous one is lost during the restoration.
task = Task(
name='restore', submitted=task.submitted, started=task.started,
arguments=task.arguments, status='Done', finished=datetime.now(),
user=task.user
)
except Exception as e:
if task:
task.status = 'Failed'
task.message = '%s' % e
task.finished = datetime.now()
raise e
finally:
# Commit it all, even in case of exceptions
if task:
task.processid = None
task.save(using=database)
示例3: handle
# 需要导入模块: from freppledb.execute.models import Task [as 别名]
# 或者: from freppledb.execute.models.Task import processid [as 别名]
def handle(self, *args, **options):
# Pick up the options
now = datetime.now()
self.database = options['database']
if self.database not in settings.DATABASES:
raise CommandError("No database settings known for '%s'" % self.database )
if options['user']:
try:
self.user = User.objects.all().using(self.database).get(username=options['user'])
except:
raise CommandError("User '%s' not found" % options['user'] )
else:
self.user = None
timestamp = now.strftime("%Y%m%d%H%M%S")
if self.database == DEFAULT_DB_ALIAS:
logfile = 'exporttofolder-%s.log' % timestamp
else:
logfile = 'exporttofolder_%s-%s.log' % (self.database, timestamp)
try:
handler = logging.FileHandler(os.path.join(settings.FREPPLE_LOGDIR, logfile), encoding='utf-8')
# handler.setFormatter(logging.Formatter(settings.LOGGING['formatters']['simple']['format']))
logger.addHandler(handler)
logger.propagate = False
except Exception as e:
print("%s Failed to open logfile %s: %s" % (datetime.now(), logfile, e))
task = None
errors = 0
try:
# Initialize the task
if options['task']:
try:
task = Task.objects.all().using(self.database).get(pk=options['task'])
except:
raise CommandError("Task identifier not found")
if task.started or task.finished or task.status != "Waiting" or task.name not in ('frepple_exporttofolder', 'exporttofolder'):
raise CommandError("Invalid task identifier")
task.status = '0%'
task.started = now
task.logfile = logfile
else:
task = Task(name='exporttofolder', submitted=now, started=now, status='0%', user=self.user, logfile=logfile)
task.arguments = ' '.join(['"%s"' % i for i in args])
task.processid = os.getpid()
task.save(using=self.database)
# Execute
if os.path.isdir(settings.DATABASES[self.database]['FILEUPLOADFOLDER']):
if not os.path.isdir(os.path.join(settings.DATABASES[self.database]['FILEUPLOADFOLDER'], 'export')):
try:
os.makedirs(os.path.join(settings.DATABASES[self.database]['FILEUPLOADFOLDER'], 'export'))
except OSError as exception:
if exception.errno != errno.EEXIST:
raise
logger.info("%s Started export to folder\n" % datetime.now())
cursor = connections[self.database].cursor()
task.status = '0%'
task.save(using=self.database)
i = 0
cnt = len(self.statements)
# Calling all the pre-sql statements
for stmt in self.pre_sql_statements:
try:
logger.info("Executing pre-statement '%s'" % stmt)
cursor.execute(stmt)
logger.info("%s record(s) modified" % cursor.rowcount)
except:
errors += 1
logger.error("An error occurred when executing statement '%s'" % stmt)
for cfg in self.statements:
# Validate filename
filename = cfg.get('filename', None)
if not filename:
raise Exception("Missing filename in export configuration")
folder = cfg.get('folder', None)
if not folder:
raise Exception("Missing folder in export configuration for %s" % filename)
logger.info("%s Started export of %s" % (datetime.now(), filename))
# Make sure export folder exists
exportFolder = os.path.join(settings.DATABASES[self.database]['FILEUPLOADFOLDER'], folder)
if not os.path.isdir(exportFolder):
os.makedirs(exportFolder)
try:
reportclass = cfg.get('report', None)
sql = cfg.get('sql', None)
if reportclass:
# Export from report class
# Create a dummy request
#.........这里部分代码省略.........
示例4: handle
# 需要导入模块: from freppledb.execute.models import Task [as 别名]
# 或者: from freppledb.execute.models.Task import processid [as 别名]
def handle(self, **options):
# Pick up the options
now = datetime.now()
self.database = options['database']
if self.database not in settings.DATABASES:
raise CommandError("No database settings known for '%s'" % self.database )
if options['user']:
try:
self.user = User.objects.all().using(self.database).get(username=options['user'])
except:
raise CommandError("User '%s' not found" % options['user'] )
else:
self.user = None
timestamp = now.strftime("%Y%m%d%H%M%S")
if self.database == DEFAULT_DB_ALIAS:
logfile = 'importfromfolder-%s.log' % timestamp
else:
logfile = 'importfromfolder_%s-%s.log' % (self.database, timestamp)
try:
handler = logging.FileHandler(os.path.join(settings.FREPPLE_LOGDIR, logfile), encoding='utf-8')
# handler.setFormatter(logging.Formatter(settings.LOGGING['formatters']['simple']['format']))
logger.addHandler(handler)
logger.propagate = False
except Exception as e:
print("%s Failed to open logfile %s: %s" % (datetime.now(), logfile, e))
task = None
errors = [0, 0]
returnederrors = [0, 0]
try:
setattr(_thread_locals, 'database', self.database)
# Initialize the task
if options['task']:
try:
task = Task.objects.all().using(self.database).get(pk=options['task'])
except:
raise CommandError("Task identifier not found")
if task.started or task.finished or task.status != "Waiting" or task.name not in ('frepple_importfromfolder', 'importfromfolder'):
raise CommandError("Invalid task identifier")
task.status = '0%'
task.started = now
task.logfile = logfile
else:
task = Task(name='importfromfolder', submitted=now, started=now, status='0%', user=self.user, logfile=logfile)
task.processid = os.getpid()
task.save(using=self.database)
# Choose the right self.delimiter and language
self.delimiter = get_format('DECIMAL_SEPARATOR', settings.LANGUAGE_CODE, True) == ',' and ';' or ','
translation.activate(settings.LANGUAGE_CODE)
# Execute
if 'FILEUPLOADFOLDER' in settings.DATABASES[self.database] \
and os.path.isdir(settings.DATABASES[self.database]['FILEUPLOADFOLDER']):
# Open the logfile
logger.info("%s Started importfromfolder\n" % datetime.now().replace(microsecond=0))
all_models = [ (ct.model_class(), ct.pk) for ct in ContentType.objects.all() if ct.model_class() ]
models = []
for ifile in os.listdir(settings.DATABASES[self.database]['FILEUPLOADFOLDER']):
if not ifile.lower().endswith(('.csv', '.csv.gz', '.xlsx')):
continue
filename0 = ifile.split('.')[0]
model = None
contenttype_id = None
for m, ct in all_models:
if matchesModelName(filename0, m):
model = m
contenttype_id = ct
logger.info("%s Matched a model to file: %s" % (datetime.now().replace(microsecond=0), ifile))
break
if not model or model in EXCLUDE_FROM_BULK_OPERATIONS:
logger.info("%s Ignoring data in file: %s" % (datetime.now().replace(microsecond=0), ifile))
elif self.user and not self.user.has_perm('%s.%s' % (model._meta.app_label, get_permission_codename('add', model._meta))):
# Check permissions
logger.info("%s You don't have permissions to add: %s" % (datetime.now().replace(microsecond=0), ifile))
else:
deps = set([model])
GridReport.dependent_models(model, deps)
models.append( (ifile, model, contenttype_id, deps) )
# Sort the list of models, based on dependencies between models
models = GridReport.sort_models(models)
i = 0
cnt = len(models)
for ifile, model, contenttype_id, dependencies in models:
task.status = str(int(10 + i / cnt * 80)) + '%'
task.message = 'Processing data file %s' % ifile
task.save(using=self.database)
i += 1
filetoparse = os.path.join(os.path.abspath(settings.DATABASES[self.database]['FILEUPLOADFOLDER']), ifile)
if ifile.lower().endswith('.xlsx'):
logger.info("%s Started processing data in Excel file: %s" % (datetime.now().replace(microsecond=0), ifile))
returnederrors = self.loadExcelfile(model, filetoparse)
#.........这里部分代码省略.........
示例5: handle
# 需要导入模块: from freppledb.execute.models import Task [as 别名]
# 或者: from freppledb.execute.models.Task import processid [as 别名]
#.........这里部分代码省略.........
os.environ[i.label[0]] = '1'
# Set environment variables
if options['env']:
task.arguments = "--constraint=%d --plantype=%d --env=%s" % (constraint, plantype, options['env'])
for i in options['env'].split(','):
j = i.split('=')
if len(j) == 1:
os.environ[j[0]] = '1'
else:
os.environ[j[0]] = j[1]
else:
task.arguments = "--constraint=%d --plantype=%d" % (constraint, plantype)
if options['background']:
task.arguments += " --background"
# Log task
# Different from the other tasks the frepple engine will write the processid
task.save(using=database)
# Locate commands.py
import freppledb.common.commands
cmd = freppledb.common.commands.__file__
def setlimits():
import resource
if settings.MAXMEMORYSIZE:
resource.setrlimit(
resource.RLIMIT_AS,
(settings.MAXMEMORYSIZE * 1024 * 1024, (settings.MAXMEMORYSIZE + 10) * 1024 * 1024)
)
if settings.MAXCPUTIME:
resource.setrlimit(
resource.RLIMIT_CPU,
(settings.MAXCPUTIME, settings.MAXCPUTIME + 5)
)
# Limiting the file size is a bit tricky as this limit not only applies to the log
# file, but also to temp files during the export
# if settings.MAXTOTALLOGFILESIZE:
# resource.setrlimit(
# resource.RLIMIT_FSIZE,
# (settings.MAXTOTALLOGFILESIZE * 1024 * 1024, (settings.MAXTOTALLOGFILESIZE + 1) * 1024 * 1024)
# )
# Prepare environment
os.environ['FREPPLE_PLANTYPE'] = str(plantype)
os.environ['FREPPLE_CONSTRAINT'] = str(constraint)
os.environ['FREPPLE_TASKID'] = str(task.id)
os.environ['FREPPLE_DATABASE'] = database
os.environ['FREPPLE_LOGFILE'] = logfile
os.environ['FREPPLE_PROCESSNAME'] = settings.DATABASES[database]['NAME'].replace('demo', '')
os.environ['PATH'] = settings.FREPPLE_HOME + os.pathsep + os.environ['PATH'] + os.pathsep + settings.FREPPLE_APP
if os.path.isfile(os.path.join(settings.FREPPLE_HOME, 'libfrepple.so')):
os.environ['LD_LIBRARY_PATH'] = settings.FREPPLE_HOME
if 'DJANGO_SETTINGS_MODULE' not in os.environ:
os.environ['DJANGO_SETTINGS_MODULE'] = 'freppledb.settings'
os.environ['PYTHONPATH'] = os.path.normpath(settings.FREPPLE_APP)
libdir = os.path.join(os.path.normpath(settings.FREPPLE_HOME), 'lib')
if os.path.isdir(libdir):
# Folders used by the Windows version
os.environ['PYTHONPATH'] += os.pathsep + libdir
if os.path.isfile(os.path.join(libdir, 'library.zip')):
os.environ['PYTHONPATH'] += os.pathsep + os.path.join(libdir, 'library.zip')
if options['background']:
# Execute as background process on Windows
if os.name == 'nt':
subprocess.Popen(['frepple', cmd], creationflags=0x08000000)
else:
# Execute as background process on Linux
subprocess.Popen(['frepple', cmd], preexec_fn=setlimits)
else:
if os.name == 'nt':
# Execute in foreground on Windows
ret = subprocess.call(['frepple', cmd])
else:
# Execute in foreground on Linux
ret = subprocess.call(['frepple', cmd], preexec_fn=setlimits)
if ret != 0 and ret != 2:
# Return code 0 is a successful run
# Return code is 2 is a run cancelled by a user. That's shown in the status field.
raise Exception('Failed with exit code %d' % ret)
# Reread the task from the database and update it
if not options['background']:
task = Task.objects.all().using(database).get(pk=task.id)
task.processid = None
task.status = 'Done'
task.finished = datetime.now()
task.save(using=database)
except Exception as e:
if task:
task = Task.objects.all().using(database).get(pk=task.id)
task.status = 'Failed'
task.message = '%s' % e
task.finished = datetime.now()
task.processid = None
task.save(using=database)
raise e
示例6: handle
# 需要导入模块: from freppledb.execute.models import Task [as 别名]
# 或者: from freppledb.execute.models.Task import processid [as 别名]
def handle(self, **options):
# Make sure the debug flag is not set!
# When it is set, the django database wrapper collects a list of all sql
# statements executed and their timings. This consumes plenty of memory
# and cpu time.
tmp_debug = settings.DEBUG
settings.DEBUG = False
# Pick up options
force = options['force']
test = 'FREPPLE_TEST' in os.environ
if options['user']:
try:
user = User.objects.all().get(username=options['user'])
except:
raise CommandError("User '%s' not found" % options['user'] )
else:
user = None
# Synchronize the scenario table with the settings
Scenario.syncWithSettings()
# Initialize the task
source = options['source']
try:
sourcescenario = Scenario.objects.using(DEFAULT_DB_ALIAS).get(pk=source)
except:
raise CommandError("No source database defined with name '%s'" % source)
now = datetime.now()
task = None
if 'task' in options and options['task']:
try:
task = Task.objects.all().using(source).get(pk=options['task'])
except:
raise CommandError("Task identifier not found")
if task.started or task.finished or task.status != "Waiting" or task.name not in ('frepple_copy', 'scenario_copy'):
raise CommandError("Invalid task identifier")
task.status = '0%'
task.started = now
else:
task = Task(name='scenario_copy', submitted=now, started=now, status='0%', user=user)
task.processid = os.getpid()
task.save(using=source)
# Validate the arguments
destination = options['destination']
destinationscenario = None
try:
task.arguments = "%s %s" % (source, destination)
if options['description']:
task.arguments += '--description="%s"' % options['description'].replace('"', '\\"')
if force:
task.arguments += " --force"
task.save(using=source)
try:
destinationscenario = Scenario.objects.using(DEFAULT_DB_ALIAS).get(pk=destination)
except:
raise CommandError("No destination database defined with name '%s'" % destination)
if source == destination:
raise CommandError("Can't copy a schema on itself")
if settings.DATABASES[source]['ENGINE'] != settings.DATABASES[destination]['ENGINE']:
raise CommandError("Source and destination scenarios have a different engine")
if sourcescenario.status != 'In use':
raise CommandError("Source scenario is not in use")
if destinationscenario.status != 'Free' and not force:
raise CommandError("Destination scenario is not free")
# Logging message - always logging in the default database
destinationscenario.status = 'Busy'
destinationscenario.save(using=DEFAULT_DB_ALIAS)
# Copying the data
# Commenting the next line is a little more secure, but requires you to create a .pgpass file.
if settings.DATABASES[source]['PASSWORD']:
os.environ['PGPASSWORD'] = settings.DATABASES[source]['PASSWORD']
if os.name == 'nt':
# On windows restoring with pg_restore over a pipe is broken :-(
cmd = "pg_dump -c -Fp %s%s%s%s | psql %s%s%s%s"
else:
cmd = "pg_dump -Fc %s%s%s%s | pg_restore -n public -Fc -c --if-exists %s%s%s -d %s"
commandline = cmd % (
settings.DATABASES[source]['USER'] and ("-U %s " % settings.DATABASES[source]['USER']) or '',
settings.DATABASES[source]['HOST'] and ("-h %s " % settings.DATABASES[source]['HOST']) or '',
settings.DATABASES[source]['PORT'] and ("-p %s " % settings.DATABASES[source]['PORT']) or '',
test and settings.DATABASES[source]['TEST']['NAME'] or settings.DATABASES[source]['NAME'],
settings.DATABASES[destination]['USER'] and ("-U %s " % settings.DATABASES[destination]['USER']) or '',
settings.DATABASES[destination]['HOST'] and ("-h %s " % settings.DATABASES[destination]['HOST']) or '',
settings.DATABASES[destination]['PORT'] and ("-p %s " % settings.DATABASES[destination]['PORT']) or '',
test and settings.DATABASES[destination]['TEST']['NAME'] or settings.DATABASES[destination]['NAME'],
)
with subprocess.Popen(commandline, shell=True, stdout=subprocess.DEVNULL, stderr=subprocess.STDOUT) as p:
try:
task.processid = p.pid
task.save(using=source)
p.wait()
except:
p.kill()
p.wait()
# Consider the destination database free again
destinationscenario.status = 'Free'
#.........这里部分代码省略.........
示例7: handle
# 需要导入模块: from freppledb.execute.models import Task [as 别名]
# 或者: from freppledb.execute.models.Task import processid [as 别名]
def handle(self, *fixture_labels, **options):
# get the database object
database = options['database']
if database not in settings.DATABASES:
raise CommandError("No database settings known for '%s'" % database )
now = datetime.now()
task = None
try:
setattr(_thread_locals, 'database', database)
# Initialize the task
if options['task']:
try:
task = Task.objects.all().using(database).get(pk=options['task'])
except:
raise CommandError("Task identifier not found")
if task.started or task.finished or task.status != "Waiting" or task.name != 'loaddata':
raise CommandError("Invalid task identifier")
task.status = '0%'
task.started = now
task.processid = os.getpid()
task.save(using=database, update_fields=['started', 'status', 'processid'])
else:
if options['user']:
try:
user = User.objects.all().using(database).get(username=options['user'])
except:
raise CommandError("User '%s' not found" % options['user'] )
else:
user = None
task = Task(
name='loaddata', submitted=now, started=now, status='0%',
user=user, arguments=' '.join(fixture_labels)
)
task.processid = os.getpid()
task.save(using=database)
# Excecute the standard django command
super(Command, self).handle(*fixture_labels, **options)
# if the fixture doesn't contain the 'demo' word, let's not apply loaddata post-treatments
for f in fixture_labels:
if '_demo' not in f.lower():
return
with transaction.atomic(using=database, savepoint=False):
print('updating fixture to current date')
cursor = connections[database].cursor()
cursor.execute('''
select to_timestamp(value,'YYYY-MM-DD hh24:mi:ss') from common_parameter where name = 'currentdate'
''')
currentDate = cursor.fetchone()[0]
now = datetime.now()
offset = (now - currentDate).days
#update currentdate to now
cursor.execute('''
update common_parameter set value = 'now' where name = 'currentdate'
''')
#update demand due dates
cursor.execute('''
update demand set due = due + %s * interval '1 day'
''', (offset,))
#update PO/DO/MO due dates
cursor.execute('''
update operationplan set startdate = startdate + %s * interval '1 day', enddate = enddate + %s * interval '1 day'
''', 2 * (offset,))
# Task update
task.status = 'Done'
task.finished = datetime.now()
task.processid = None
task.save(using=database, update_fields=['status', 'finished'])
except Exception as e:
if task:
task.status = 'Failed'
task.message = '%s' % e
task.finished = datetime.now()
task.processid = None
task.save(using=database, update_fields=['status', 'finished', 'message'])
raise CommandError('%s' % e)
finally:
setattr(_thread_locals, 'database', None)
示例8: handle
# 需要导入模块: from freppledb.execute.models import Task [as 别名]
# 或者: from freppledb.execute.models.Task import processid [as 别名]
def handle(self, **options):
# Pick up the options
database = options['database']
if database not in settings.DATABASES:
raise CommandError("No database settings known for '%s'" % database )
if options['user']:
try:
user = User.objects.all().using(database).get(username=options['user'])
except:
raise CommandError("User '%s' not found" % options['user'] )
else:
user = None
now = datetime.now()
task = None
try:
# Initialize the task
if options['task']:
try:
task = Task.objects.all().using(database).get(pk=options['task'])
except:
raise CommandError("Task identifier not found")
if task.started or task.finished or task.status != "Waiting" or task.name not in ('frepple_loadxml', 'loadxml'):
raise CommandError("Invalid task identifier")
task.status = '0%'
task.started = now
else:
task = Task(name='loadxml', submitted=now, started=now, status='0%', user=user)
task.arguments = ' '.join(options['file'])
task.processid = os.getpid()
task.save(using=database)
# Execute
# TODO: if frePPLe is available as a module, we don't really need to spawn another process.
os.environ['FREPPLE_HOME'] = settings.FREPPLE_HOME.replace('\\', '\\\\')
os.environ['FREPPLE_APP'] = settings.FREPPLE_APP
os.environ['FREPPLE_DATABASE'] = database
os.environ['PATH'] = settings.FREPPLE_HOME + os.pathsep + os.environ['PATH'] + os.pathsep + settings.FREPPLE_APP
os.environ['LD_LIBRARY_PATH'] = settings.FREPPLE_HOME
if 'DJANGO_SETTINGS_MODULE' not in os.environ:
os.environ['DJANGO_SETTINGS_MODULE'] = 'freppledb.settings'
if os.path.exists(os.path.join(os.environ['FREPPLE_HOME'], 'python36.zip')):
# For the py2exe executable
os.environ['PYTHONPATH'] = os.path.join(
os.environ['FREPPLE_HOME'],
'python%d%d.zip' % (sys.version_info[0], sys.version_info[1])
) + os.pathsep + os.path.normpath(os.environ['FREPPLE_APP'])
else:
# Other executables
os.environ['PYTHONPATH'] = os.path.normpath(os.environ['FREPPLE_APP'])
cmdline = [ '"%s"' % i for i in options['file'] ]
cmdline.insert(0, 'frepple')
cmdline.append( '"%s"' % os.path.join(settings.FREPPLE_APP, 'freppledb', 'execute', 'loadxml.py') )
proc = subprocess.run(' '.join(cmdline))
if proc.returncode:
raise Exception('Exit code of the batch run is %d' % proc.returncode)
# Task update
task.status = 'Done'
task.finished = datetime.now()
except Exception as e:
if task:
task.status = 'Failed'
task.message = '%s' % e
task.finished = datetime.now()
raise e
finally:
if task:
task.processid = None
task.save(using=database)
示例9: handle
# 需要导入模块: from freppledb.execute.models import Task [as 别名]
# 或者: from freppledb.execute.models.Task import processid [as 别名]
def handle(self, **options):
# Pick up options
database = options['database']
if database not in settings.DATABASES:
raise CommandError("No database settings known for '%s'" % database )
if options['user']:
try:
user = User.objects.all().using(database).get(username=options['user'])
except:
raise CommandError("User '%s' not found" % options['user'] )
else:
user = None
if options['models']:
models = options['models'].split(',')
else:
models = None
now = datetime.now()
task = None
try:
# Initialize the task
if options['task']:
try:
task = Task.objects.all().using(database).get(pk=options['task'])
except:
raise CommandError("Task identifier not found")
if task.started or task.finished or task.status != "Waiting" or task.name not in ('frepple_flush', 'empty'):
raise CommandError("Invalid task identifier")
task.status = '0%'
task.started = now
else:
task = Task(name='empty', submitted=now, started=now, status='0%', user=user)
task.processid = os.getpid()
task.save(using=database)
# Create a database connection
cursor = connections[database].cursor()
# Get a list of all django tables in the database
tables = set(connections[database].introspection.django_table_names(only_existing=True))
ContentTypekeys = set()
# Validate the user list of tables
if models:
models2tables = set()
admin_log_positive = True
for m in models:
try:
x = m.split('.', 1)
x = apps.get_model(x[0], x[1])
if x in EXCLUDE_FROM_BULK_OPERATIONS:
continue
ContentTypekeys.add(ContentType.objects.get_for_model(x).pk)
x = x._meta.db_table
if x not in tables:
raise
models2tables.add(x)
except Exception as e:
raise CommandError("Invalid model to erase: %s" % m)
tables = models2tables
else:
admin_log_positive = False
tables.discard('django_admin_log')
for i in EXCLUDE_FROM_BULK_OPERATIONS:
tables.discard(i._meta.db_table)
ContentTypekeys.add(ContentType.objects.get_for_model(i).pk)
# Some tables need to be handled a bit special
if 'operationplan' in tables:
tables.add('operationplanmaterial')
tables.add('operationplanresource')
tables.add('out_problem')
if 'resource' in tables and 'out_resourceplan' not in tables:
tables.add('out_resourceplan')
if 'demand' in tables and 'out_constraint' not in tables:
tables.add('out_constraint')
tables.discard('auth_group_permissions')
tables.discard('auth_permission')
tables.discard('auth_group')
tables.discard('django_session')
tables.discard('common_user')
tables.discard('common_user_groups')
tables.discard('common_user_user_permissions')
tables.discard('common_preference')
tables.discard('django_content_type')
tables.discard('execute_log')
tables.discard('common_scenario')
# Delete all records from the tables.
with transaction.atomic(using=database, savepoint=False):
if ContentTypekeys:
if admin_log_positive:
cursor.execute(
"delete from django_admin_log where content_type_id = any(%s)",
( list(ContentTypekeys), )
)
else:
cursor.execute(
"delete from django_admin_log where content_type_id != any(%s)",
( list(ContentTypekeys), )
#.........这里部分代码省略.........