Dr. Martin Seligman wrote about the structure of "learned optimism". He said keeping three things in mind can help you maintain optimism when meeting challenging situations.
It is not about me
What is happening is not personal. When things do not go your way, remember, "It is not about me."
It is not about everything in my life
This issue is not pervasive. When something does not work out the way you want, though you may not be pleased about this one part of your life, in truth many other parts of your life are working very well. Often the vast majority of your life is going fine! "It is not about everything in my life."
In hundred years, who will care about this?
This situation is not permanent. With challenges you may be facing at any particular moment, always know it will end in time. "It will not last forever." It may be helpful to ask yourself something like: "In hundred years, who will care about this?"