Maybe there's a similar kind of task in your job that disrupts your normal 'flow'. Do you have a strategy for getting round that? What do you do to manage distractions and stay productive at work, while minimising stress and saving cognitive energy?
I'm a writer, which is surprisingly similar to being a software developer, work-wise.
There are structures I am writing within, floating around in my head (paragraph structure, story structure, sentence structure), variables (keywords) I need to monitor and consider and call out at the proper moments, and I need to consider what the inputs and outputs are going to be (who is going to read this), and then run it by them for testing (editing).
I have many windows open at a time. My main writing app. Other documents I have written that are similar in some way. Research or reference materials. A list of keywords I need to integrate. And email, Slack, etc, which is just a normal part of life.
So if you squint a bit, I think our workflows are pretty similar.
What I do is actually turn off the distractions. I make sure I don't ever log into FB with my work computer, for example. While I am at work, I limit myself to the FB app on my personal phone. The app sucks, so I don't use it much. (And, I don't like to be seen on my phone while at work, so the implied social pressure helps, too.)
I close my email from time to time. Log out of it for a couple of hours, and just work.
I set my desktop images to wide expanses and landscapes, to inspire broader thinking, and bring a little bit of nature in.
And when I really need to just crack down and power out a bunch of work, I have a specific playlist that I cue up. (Mine is primarily non-vocal hardcore prog rock. Really technical shit with lots of time signature changes, and other craziness. For some reason this is better than caffeine or anything else, and just turns on my productivity.)
And finally, I adhere to Stephen King's general guidelines he laid out in his book, "On Writing". He makes the case that creativity is a muscle that needs to be trained like any other. So he sits down at the exact same time every day, and writes. I think it is a 4-hour block, from 6:AM to 10:AM, IIRC. And then he stops. He gets up and doesn't think about his story again until the next day at 6:AM.
What this does over time is train your mind to be focused and creative during a specific part of the day. It becomes a tap you can turn on and off at will.
And it works REALLY well for a lot of people. Me included.
So I front-load my day. I get in early, before everyone else, and pound out a shitload of work before 11:AM, because I have trained my brain to be turned on and ready to go. After that period of ridiculous productivity, I go to lunch. Get some good calories in my body. And in the afternoon I do all my meetings, reading, organizing, planning, research, etc.
And when I get up from my desk, I leave everything there. I walk out of the building and do not think about work again until I am at my desk again the next day.
It takes discipline to do it, but it works amazingly well.
I recommend it.