You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
calculate-utils-3-core/core/server/sid_pid_file.py

205 lines
5.7 KiB

#-*- coding: utf-8 -*-
# Copyright 2012 Calculate Ltd. http://www.calculate-linux.org
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
import os, time
13 years ago
import random, pickle
from threading import Lock
import soaplib
from soaplib.serializers.primitive import String, Integer
from soaplib.serializers.clazz import Array
13 years ago
class CoreWsdl () :
13 years ago
def del_sid_pid(self, sid):
# delete conformity pid and sid of sid_pid file
13 years ago
if not os.path.exists(self.sids_pids):
temp = open(self.sids_pids, 'w')
13 years ago
temp.close()
#try:
rst = []
13 years ago
SID_PID_T = self.sids_pids + '_temp'
13 years ago
lock = Lock()
lock.acquire()
13 years ago
fd = open(self.sids_pids, 'r')
13 years ago
ft = open(SID_PID_T, 'w')
while 1:
try:
#read out on 1 record
list_sid = pickle.load(fd)
except:
break
if sid != list_sid[0]:
pickle.dump(list_sid, ft)
else:
# end process pid = list_sid[1]
self.serv_pid_kill (list_sid[1], sid, self.certbase)
# delete this of process file
while self.glob_process_dict[list_sid[1]]['status'] == 1:
time.sleep(0.1)
13 years ago
self.del_pid(list_sid[1])
# delete process file
os.unlink(self.pids + '/%d.pid' %list_sid[1])
13 years ago
fd.close()
ft.close()
13 years ago
13 years ago
ft = open(SID_PID_T, 'rb')
13 years ago
fd = open(self.sids_pids, 'wb')
13 years ago
ft.seek(0)
fd.write(ft.read())
ft.close()
fd.close()
# delete sid file
if os.path.exists(self.sids + "/%d.sid"%sid):
os.unlink(self.sids + "/%d.sid"%sid)
13 years ago
os.unlink(SID_PID_T)
self.clear_cache(int(sid))
13 years ago
lock.release()
return 0
#except:
#return 1
def del_pid_from_sid_pid(self, pid):
# delete conformity pid and sid of sid_pid file
if not os.path.exists(self.sids_pids):
temp = open(self.sids_pids, 'w')
temp.close()
#try:
rst = []
SID_PID_T = self.sids_pids + '_temp'
lock = Lock()
lock.acquire()
fd = open(self.sids_pids, 'r')
ft = open(SID_PID_T, 'w')
while 1:
try:
#read out on 1 record
list_sid = pickle.load(fd)
except:
break
if pid != list_sid[1]:
pickle.dump(list_sid, ft)
fd.close()
ft.close()
ft = open(SID_PID_T, 'rb')
fd = open(self.sids_pids, 'wb')
ft.seek(0)
fd.write(ft.read())
ft.close()
fd.close()
# delete temp file
os.unlink(SID_PID_T)
lock.release()
13 years ago
return 0
#except:
#return 1
#
def find_sid_pid_file(self, sid):
results = []
lock = Lock()
lock.acquire()
13 years ago
if not os.path.exists(self.sids_pids):
temp = open(self.sids_pids, 'w')
13 years ago
temp.close()
13 years ago
fd = open(self.sids_pids, 'r')
13 years ago
while 1:
try:
#read out on 1 record
list_sid = pickle.load(fd)
except:
break
if sid == list_sid[0]:
results.append(list_sid[1])
if results == []:
results.append(0)
fd.close()
lock.release()
return results
def serv_pid_info(self, sid, pid):
f = 0
results = []
# Check pid presence and conformity sid
13 years ago
lock = Lock()
lock.acquire()
13 years ago
fd = open(self.sids_pids, 'r')
13 years ago
while 1:
try:
#read out on 1 record
list_sid = pickle.load(fd)
except:
break
if sid == list_sid[0]:
if pid == list_sid[1]:
f = 1
fd.close()
lock.release()
# Get information about pid
if f == 1:
lock = Lock()
lock.acquire()
self.glob_process_dict[pid]
# process id
13 years ago
results.append(str(pid))
# current state
13 years ago
results.append(str(self.glob_process_dict[pid]['status']))
# start time
13 years ago
results.append(str(self.glob_process_dict[pid]['time']))
# process (function) name
13 years ago
results.append(str(self.glob_process_dict[pid]['name']))
12 years ago
# process soap method name
results.append(str(self.glob_process_dict[pid]['method_name']))
13 years ago
lock.release()
return results
def add_sid_pid (self, sid, pid):
''' add conformity pid and sin in sid_pid file '''
13 years ago
lock = Lock()
lock.acquire()
13 years ago
if not os.path.exists(self.sids_pids):
temp = open(self.sids_pids, 'w')
13 years ago
temp.close()
lock.release()
try:
lock.acquire()
13 years ago
fd = open(self.sids_pids, 'a')
13 years ago
list = [sid, pid]
pickle.dump(list,fd)
fd.close()
lock.release()
return 0
except:
return 1