Sending notifications from a custom resource

Hi,

I'm writing a custom resource to configure a service.

The service has to be stopped before reconfiguration can happen.
The brute-force solution would be to systemctl stop service (with Mixlib::ShellOut or execute).

But to send a immediate notification to the corresponding service resource would be a much more elegant solution.

Can this be done? And how?

Best

Christopher

Hi @christo, I'm not entirely sure I understand the question.

But to send a immediate notification to the corresponding service resource would be a much more elegant solution

It sounds like you're looking for notifies to let the custom resource know when the service is stopped?

Or are you asking if there is a more elegant way to send a signal to the process such as SIGHUP or SIGTERM? If this is the case, I'm not necessarily sure that I would consider systemctl stop a brute-force solution if it's meant to be managed as a SystemD service, though I might make use of the service resource using notifies as mentioned above to stop/start the resource before/after the configuration.

This is probably one of the cases that you'd want to use a before notification, but it depends on the details of how you're doing configuration of the service.

service "whatever" do
  action :nothing
end

template "/etc/whatever.conf" do
  notifies :stop, "service[whatever]", :before
  notifies :start, "service[whatever]", :immediately
end

the problem is that not all resources correctly support being the source of a before notification and they need to run correctly in why-run mode, which many resources don't, so depending on the complexity of the details of what you need to do to configure the resource this may work or not.

to be able to correctly stop the service though you need to know if the service needs to be configured or not, so one way or another you need to solve that problem.

if you can somehow have a simple test that the running service is out of date, then it becomes a lot simpler with a custom resource that does all the work of stop-reconfigure-start and then you hang an only_if off of that custom resource and things would be much simpler -- but that offloads the possible complexity onto the black box of the method that determines if the service is out of date.

things get way simpler if you can configure the service and then just bounce it without stopping it first, everything gets much more horrible due to that requirement being there.

Cool!
That's exactly what I had in mind but I totally missed notifies …, :before.

Btw. the service in question is mattermost. It can be configured with mattermost config set foo.bar baz. This command truncates the config and rewrites the whole JSON tree.

But the developers messed up file lockíng and the server, that is periodically checking its config for changes, will write out the default config when it is reading right in the moment of truncation.
Therefore I have to make sure the server is stopped before calling mattermost config ….

Thanks

Christopher