The things that both of them have in common is that they're sustainable. If you do not like your current position, make it sustainable for the time being. I was unable to do that, twice, so I think I'm at a point where I cannot do what I need to do.
Even the fun work (FOSS) is something I cannot sustain for 40 hrs a week, despite how much I enjoy it.
Time will tell, though
my personal riff on that is 'stop assigning self worth to the quality that others assign to your work'. being able to internally generate self-worth through the act of producing high quality work is a super power.
I like Paul Graham's "keep your identity small" piece: https://www.paulgraham.com/identity.html
When you're not personally identified with your work, it stings a whole lot less when someone rips it up. And keep in mind we're imperfect people working with other (often very) imperfect people. It's a mess, and always will be.