Skip to content

Commit

Permalink
scheduled_task: Move OLE connection to collect function (#1451)
Browse files Browse the repository at this point in the history
  • Loading branch information
jkroepke authored May 11, 2024
1 parent 195cfa8 commit 00781db
Showing 1 changed file with 18 additions and 13 deletions.
31 changes: 18 additions & 13 deletions pkg/collector/scheduled_task/scheduled_task.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
package scheduled_task

import (
"errors"
"fmt"
"regexp"
"runtime"
Expand Down Expand Up @@ -63,7 +64,6 @@ const (
TASK_RESULT_SUCCESS TaskResult = 0x0
)

// RegisteredTask ...
type ScheduledTask struct {
Name string
Path string
Expand Down Expand Up @@ -117,18 +117,6 @@ func (c *collector) GetPerfCounter() ([]string, error) {
}

func (c *collector) Build() error {
runtime.LockOSThread()
defer runtime.UnlockOSThread()

err := ole.CoInitializeEx(0, ole.COINIT_MULTITHREADED)
if err != nil {
code := err.(*ole.OleError).Code()
if code != ole.S_OK && code != S_FALSE {
return err
}
}
defer ole.CoUninitialize()

c.LastResult = prometheus.NewDesc(
prometheus.BuildFQName(types.Namespace, Name, "last_result"),
"The result that was returned the last time the registered task was run",
Expand All @@ -150,6 +138,8 @@ func (c *collector) Build() error {
nil,
)

var err error

c.taskIncludePattern, err = regexp.Compile(fmt.Sprintf("^(?:%s)$", *c.taskInclude))
if err != nil {
return err
Expand Down Expand Up @@ -231,6 +221,21 @@ const SCHEDULED_TASK_PROGRAM_ID = "Schedule.Service.1"
const S_FALSE = 0x00000001

func getScheduledTasks() (scheduledTasks ScheduledTasks, err error) {
// The only way to run WMI queries in parallel while being thread-safe is to
// ensure the CoInitialize[Ex]() call is bound to its current OS thread.
// Otherwise, attempting to initialize and run parallel queries across
// goroutines will result in protected memory errors.
runtime.LockOSThread()
defer runtime.UnlockOSThread()

if err := ole.CoInitializeEx(0, ole.COINIT_MULTITHREADED); err != nil {
var oleCode *ole.OleError
if errors.As(err, &oleCode) && oleCode.Code() != ole.S_OK && oleCode.Code() != S_FALSE {
return nil, err
}
}
defer ole.CoUninitialize()

schedClassID, err := ole.ClassIDFrom(SCHEDULED_TASK_PROGRAM_ID)
if err != nil {
return scheduledTasks, err
Expand Down

0 comments on commit 00781db

Please sign in to comment.