Skip to content
GitLab
Explore
Sign in
Register
Primary navigation
Search or go to…
Project
workspaces
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Package Registry
Model registry
Operate
Environments
Terraform modules
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Terms and privacy
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
ssa
workspaces
Merge requests
!1457
Delete stackstorm prototype
Code
Review changes
Check out branch
Download
Patches
Plain diff
Merged
Delete stackstorm prototype
delete_stackstorm_prototype
into
main
Overview
0
Commits
1
Pipelines
1
Changes
4
Merged
Daniel Nemergut
requested to merge
delete_stackstorm_prototype
into
main
1 year ago
Overview
0
Commits
1
Pipelines
1
Changes
4
Expand
Removed the stackstorm prototype as it's now in the ws-stackstorm repo.
0
0
Merge request reports
Compare
main
main (base)
and
latest version
latest version
63c14135
1 commit,
1 year ago
4 files
+
0
−
164
Inline
Compare changes
Side-by-side
Inline
Show whitespace changes
Show one file at a time
Files
4
Search (e.g. *.vue) (Ctrl+P)
support/stackstorm/ws/sensors/directory_watcher.py deleted
100644 → 0
+
0
−
115
Options
#
# Copyright (C) 2021 Associated Universities, Inc. Washington DC, USA.
#
# This file is part of NRAO Workspaces.
#
# Workspaces is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# Workspaces is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with Workspaces. If not, see <https://www.gnu.org/licenses/>.
from
pathlib
import
Path
from
st2reactor.sensor.base
import
PollingSensor
class
DirectoryWatcher
(
PollingSensor
):
"""
A Sensor that watches a directory for changes.
Works by keeping track of what files it has alerted for already, in memory (so will realert on restart).
"""
def
setup
(
self
):
self
.
_logger
=
self
.
_sensor_service
.
get_logger
(
__name__
)
# We keep a dictionary of all the watchers in here so that we can update/remove them easily
self
.
triggers
=
{}
def
poll
(
self
):
# If we have no triggers, there's nothing for us to do but return
if
not
self
.
triggers
:
self
.
_logger
.
info
(
"
DirectoryWatcher: No triggers are configured; nothing to do.
"
)
# If we have triggers, examine the filesystem for each one
for
monitor
in
self
.
triggers
.
values
():
monitor
.
check
(
self
.
_sensor_service
)
def
cleanup
(
self
):
# This is called when the st2 system goes down. We don't currently need
# to release any resources here
pass
def
add_trigger
(
self
,
trigger
):
# This method is called when someone creates a new trigger based on the ws.new_files trigger.
self
.
_logger
.
info
(
"
DirectoryWatcher: Adding trigger
"
)
directory
=
trigger
[
"
parameters
"
].
get
(
"
directory
"
)
self
.
triggers
[
directory
]
=
DirectoryMonitor
(
self
.
_logger
,
trigger
)
def
update_trigger
(
self
,
trigger
):
# This method is called when trigger is updated. This usually doesn't matter to us.
directory
=
trigger
[
"
parameters
"
].
get
(
"
directory
"
)
self
.
triggers
[
directory
]
=
DirectoryMonitor
(
self
.
_logger
,
trigger
)
def
remove_trigger
(
self
,
trigger
):
# This method is called when trigger is deleted
self
.
_logger
.
info
(
"
DirectorWatcher: Removing trigger
"
)
directory
=
trigger
[
"
parameters
"
].
get
(
"
directory
"
)
del
self
.
triggers
[
directory
]
class
DirectoryMonitor
:
"""
Helper class. Watches a single directory for changes.
"""
def
__init__
(
self
,
logger
,
trigger
):
self
.
_logger
=
logger
self
.
trigger
=
trigger
self
.
path
=
Path
(
self
.
trigger
[
"
parameters
"
].
get
(
"
directory
"
))
self
.
seen
=
set
()
def
check
(
self
,
sensor_service
)
->
[
Path
]:
"""
Check for new files.
:param sensor_service: used to dispatch findings if there are any
:return: nothing
"""
files_found
=
set
(
file
for
file
in
self
.
path
.
glob
(
"
*
"
))
-
self
.
seen
# do we have any files to report? if not, we can simply return here
# since we haven't triggered, there's no need to update the state value
if
not
files_found
:
self
.
_logger
.
info
(
f
"
DirectoryWatcher: No new files found in
{
self
.
path
}
"
)
return
# let's log
self
.
_logger
.
info
(
f
"
DirectoryWatcher:
{
len
(
files_found
)
}
new files found in
{
self
.
path
}
"
)
# report what we have found
result
=
dict
(
directory
=
str
(
self
.
path
),
files
=
[
str
(
file
.
absolute
())
for
file
in
files_found
])
sensor_service
.
dispatch
(
trigger
=
self
.
trigger
,
payload
=
result
)
# save what we have found
self
.
seen
|=
files_found
return
files_found
@staticmethod
def
latest_change
(
file
:
Path
)
->
float
:
"""
Return the last change time for this file, whether that
'
s a ctime or an mtime.
Return as seconds since the epoch, with nanoseconds.
:param file: file to test
:return: change timestamp as float
"""
stat
=
file
.
stat
()
return
max
(
stat
.
st_mtime_ns
,
stat
.
st_ctime_ns
)
Loading