Skip to content

Comments

Including user's login in the trigger's name#168

Merged
maikebing merged 1 commit intoIoTSharp:masterfrom
AT-WH:userlog
May 22, 2025
Merged

Including user's login in the trigger's name#168
maikebing merged 1 commit intoIoTSharp:masterfrom
AT-WH:userlog

Conversation

@AT-WH
Copy link
Contributor

@AT-WH AT-WH commented May 21, 2025

After allowing multiple users by providing custom authentication mechanism, I found it difficult to find out who triggered jobs. I think that it is possible to easily include this information in the trigger's name. E.g.

image
(In the session presented in the screen above, I'm logged in as "MyUser123")

I've come up with a suggestion for a naming convention for such triggers, but I don't insist on it.

Comment on lines -101 to -103
new Claim(ClaimTypes.Name, string.IsNullOrEmpty(password)
? "SilkierQuartzPassword"
: authenticationOptions.Password),
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

After analyzing this carefuly, I'm not sure if including user's password in Claims is needed anywhere, but it might be the case that I'm not aware of a place where it's needed in the project.

Comment on lines +117 to +120
else
{
await Scheduler.TriggerJob(jobKey, jobData);
}
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

else contains the current master code, which is used if the username can't be retrieved for whatever reason.

{
var dt = DateTime.UtcNow;
var truncatedUsername = username.Length > 30 ? username.Substring(0, 30) : username;
var triggerName = $"{truncatedUsername}-{dt.ToString("ddMMHHmmss")}";
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I searched for a naming convention that includes username as well as prevents conflicts if the same user triggers multiple Jobs. Also, I didn't want to make the name too long, so that the UI doesn't break.

As a result I came up with the following:

  • take up to 30 characters of the username
  • append a number constructed by concatenating monthDayHourMinuteSecond - I doubt that any user is able to trigger a job more frequently than once a second
  • for every time component always print it using two digits (that means: use leading zeros if needed).

Comment on lines +108 to +115
var trigger =
TriggerBuilder.Create()
.WithIdentity(triggerName, "MT")
.UsingJobData(jobData)
.StartNow()
.ForJob(jobKey)
.Build();
await Scheduler.ScheduleJob(trigger);
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In order to kick off a job with a trigger's name manually set, I define a trigger for the job which starts immediately and then use it to schedule a job.

Comment on lines -213 to +236
var history = hsl!=null? await hsl.FilterLastOfEveryJob(10):null;
var history = hsl != null ? await hsl.FilterLastOfEveryJob(10) : null;
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

My VS must have changed this formatting automatically. I can revert this if it breaks your code convention.

@maikebing maikebing merged commit 4356f79 into IoTSharp:master May 22, 2025
2 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants