-
Notifications
You must be signed in to change notification settings - Fork 0
/
renderstop.lua
129 lines (88 loc) · 3.45 KB
/
renderstop.lua
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
-- License: GPLv3 or any later version
--
-- Authors: Lsyncd devs and Riccardo Gagliarducci
-- From the idea in: https://lsyncd.github.io/lsyncd/manual/examples/auto-image-magic/
--
-- require "config"
package.path = '*.lua;' .. package.path
require "config"
log("Normal", "Watching " .. general.watchdir)
settings {
logfile = "/var/log/lsyncd.log",
statusFile = "/var/log/lsyncd-status.log",
nodaemon = false
}
local formats = { blend = true }
renderstop = {
delay = 0,
maxProcesses = 1,
action = function(inlet)
local event = inlet.getEvent()
if event.isdir then
-- ignores events on dirs
inlet.discardEvent(event)
return
end
-- extract extension and basefilename
local filename = event.pathname
local ext = string.match(filename, ".*%.([^.]+)$")
local base = string.match(filename, "(.*)%.[^.]+$")
if not formats[ext] then
-- an unknown extension
inlet.discardEvent(event)
return
end
-- the path, more on lsyncd.lua source
-- the baseDir as in config file
local baseDir = event.source
-- the base dir of the current file, can be subdir
local sourcePathdir = event.sourcePathdir
-- fix double slash in path
local sourcePathdir = string.gsub(sourcePathdir, "//", "/")
-- render on create and modify
if event.etype == "Create" or event.etype == "Modify" then
log("Normal", "filename is ".. base)
-- Retrieve the hostname
local machinehostname = io.popen('hostname'):read('*l')
-- Name to check to stop process
local machinehostnamestop = machinehostname .. ".stop"
-- Check if the file base contains the "hostname-stop" command
if string.match(base, machinehostnamestop) then
-- Rename the file to remove "machinehostnamestop"
local new_filename = string.gsub(filename, machinehostnamestop, "")
os.rename(baseDir .. filename, baseDir .. new_filename)
local basename = string.match(new_filename, '([^/]+)%.%w+$')
-- Sigterm the rendering process
-- subdivided execution to avoid Unterminated quoted string error
-- discover process by name and pid
local ca = "ps -ae -o 'pid' -o 'cmd'"
-- find process containing basename
local cb = "grep '" .. basename .. "'"
-- avoid himself grep -v 'grep' or...
local cc = "grep 'blender'"
-- select the first part
local cd = "awk '{print $1}'"
-- SIGTERM process
local ce = "xargs -I % kill -s 15 %"
local kommand = ca .. " | " .. cb .. " | " .. cc .. " | " .. cd .. " | " .. ce
-- Like os.execute but with return values
local handlec = io.popen( kommand )
local resultc = handlec:read("*a")
handlec:close()
local todayStart = os.date('%Y-%m-%d_%H-%M-%S')
local logContent = todayStart .. ' - Queued or processed by ' .. machinehostname .. ' - file ' .. new_filename
-- Write the processing status in lprocessing.txt, in the same source folder
os.execute('echo "' .. logContent .. '" >> "' .. sourcePathdir .. 'lprocessing.txt"')
-- Write the processing status in lprocessing.txt, in the same source folder
-- os.execute('echo $(date "+%Y-%m-%d") - Rendering stopped by user on $(hostname) ' .. new_filename .. '" >> ' .. sourcePathdir .. 'lprocessing.txt')
log("Normal", "Rendering stopped ".. baseDir .. new_filename)
end
end
if event.etype == "Delete" then
-- Todo kill process
end
-- ignores other events.
inlet.discardEvent(event)
end,
}
sync{renderstop, source=general.watchdir}